Itai Ganot
Itai Ganot

Reputation: 6305

Bash: How to capture a real process name and prevent displaying the command process?

I wrote the next script:

process="$1"
if [ -z "$1" ]; then
        echo "Please specify a process to check"
        exit 1
fi
ps -ef | grep "$process" | grep -v {grep\|$(basename $0)} > /tmp/procs

if [ "$?" -eq "0" ]; then
        stat="OK"
        exitcode="0"
        msg="Process $process is running"
else
        stat="Critical"
        exitcode="2"
        msg="There are currently no running processes of $process"
fi
echo "$stat: $msg"
exit $exitcode

The script is supposed to check if a given process is running or not and output a relevant message.

In the script, I'm using the following command to find if the process is running: ps -ef | grep $process

The problem is that while the script is running, the process name also includes the $process word and thus, it finds both the real process and the process of the script (as the $process is mentioned in the command).

Example:

[root@pnmnjs2 ~]# sh -x check_proc_nodeJS.sh dakjdhak
+ process=dakjdhak
+ '[' -z dakjdhak ']'
+ ps -ef
++ basename check_proc_nodeJS.sh
+ grep -v '{grep|check_proc_nodeJS.sh}'
+ grep dakjdhak
+ '[' 0 -eq 0 ']'
+ stat=OK
+ exitcode=0
+ msg='Process dakjdhak is running'
+ echo 'OK: Process dakjdhak is running'
OK: Process dakjdhak is running
+ exit 0
[root@pnmnjs2 ~]#

Meaning: There is no real process called "dakjdhak" but when the script is running the ps -ef command it also catches the process of the running script and then returns that the process exists and running... which is wrong.

How can I "catch" only the relevant process without the process of the running script? (the addition of command | grep -v {grep\|$(basename $0)} should have done it.. but it doesnt...

Thanks in advance

Upvotes: 1

Views: 250

Answers (3)

jfs
jfs

Reputation: 414255

You could use ps -C "$process" to find a process using its executable name (comm, not args).

Upvotes: 0

repzero
repzero

Reputation: 8412

A little pointer on using your process swtiches

ps -ef

the -e switch will show all running processes this includes when you instantly use the pipe command

grep "$process" | grep -v {grep\|$(basename $0)}

As a result you will never get an error

example

ps -ef|grep "xxx" # if xxx is not running

output (your script)

OK: Process xxxxxx is running

you just executed the process grep which ran (it does not matter if grep was successful to the process command using -ef argument). The process command see grep ran and as a result no error was caught.

What you can use is

ps -A or  ps -C

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74625

Rather than piping the output of ps to grep, you can use pgrep:

# change this
# ps -ef | grep "$process" | grep -v {grep\|$(basename $0)} > /tmp/procs

# if [ "$?" -eq "0" ]; then
# to this
if pgrep "$process" > /tmp/procs; then

If you want to know the return value of a command, there's no need to use $? with [ - just test the command directly.

Upvotes: 2

Related Questions