Thursday, April 25, 2024
 Popular · Latest · Hot · Upcoming
233
rated 0 times [  233] [ 0]  / answers: 1 / hits: 400449  / 3 Years ago, wed, september 8, 2021, 9:26:54

How can I determine if a process is running or not and then have a bash script execute some stuff based on that condition?



For example:




  • if process abc is running, do this


  • if it is not running, do that.



More From » bash

 Answers
1

A bash script to do something like that would look something like this:



#!/bin/bash

# Check if gedit is running
# -x flag only match processes whose name (or command line if -f is
# specified) exactly match the pattern.

if pgrep -x "gedit" > /dev/null
then
echo "Running"
else
echo "Stopped"
fi


This script is just checking to see if the program "gedit" is running.



Or you can only check if the program is not running like this:



if ! pgrep -x "gedit" > /dev/null
then
echo "Stopped"
fi

[#37189] Wednesday, September 8, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
uccase

Total Points: 473
Total Questions: 100
Total Answers: 110

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;