Check if process does or does not exist

I want to write script that accepts process name (I ask the user for process name each time followed by a prompt on the same line) if process exists it will print "EXISTS" or if it doesn't then "DOES NOT EXIST" depending on whether such a process is currently on the system.

Example:

Enter the name of process: bash
exists
Enter the name of process: bluesky
does not exist

My code:

#!/bin/bash
while true
do
read -p "Enter the name of process: " if [ pidof -x > 0 ]; then echo "exists" else echo "does not exist" fi
done 

The error message I receive.

pidof: unary operator expected

2 Answers

There's a few things to correct here.

First, although you ask for user-input via read, you don't actually use the result anywhere. By default, read assigns what's read to a variable named REPLY so you'd want pidof -x "$REPLY"

Second, inside [ ... ] POSIX test brackets, > has its ordinary meaning as a redirection operator1. If you check your current directory, you will likely see it now contains a file named 0. The integer "greater than" test is -gt.

Third, your pidof -x inside the [ ... ] test is just a string; to execute it as a command and capture the result you need command substitution

[ "$(pidof -x "$REPLY")" -gt 0 ]

However, pidof may return multiple integers if multiple matching processes are found - which will cause yet another error. So I'd recommend instead using the exit status of pidof directly:

EXIT STATUS 0 At least one program was found with the requested name. 1 No program was found with the requested name.

So you could do

#!/bin/bash
while true
do
read -p "Enter the name of process: " if pidof -qx "$REPLY"; then echo "exists" else echo "does not exist" fi
done

1 inside a bash [[ ... ]] extended test, > is a comparison operator - however it performs lexicographical rather than numerical comparison, so you'd still want -gt there. You could use > for integer comparison inside an arithmetic construct (( ... )) though.

0

That's the wrong test. I mean you are asking (well, you want to ask, but have syntax issues) "is the PID greater than 0", and you should be asking "is there at least one PID". You can use pidof -q to just check for the existence:

$ pidof -q bash && echo "exists" || echo "does not exist"
exists
$ pidof -q randomthing && echo "exists" || echo "does not exist"
does not exist

Your script has quite a few problems. First, when you use read without a variable, the value is stored in the special variable $REPLY but you aren't using it. Instead, you are running pidox -x with no argument and that will always return nothing. You wanted something like:

read -p "Enter the name of process: "
if pidof -x "$REPLY"; then ...

But in any case, avoid asking users for input after launching the script. That just means your script cannot be automated, users are likely to enter bad values and the whole thing is harder to use for no benefit. Instead, read the process name as an argument.

Putting all this together, you want something like this:

#!/bin/bash
if pidof -qx "$1"; then echo "There is at least one running process named '$1'" else echo "There are no running processes named '$1'." fi 

And run it like this:

script.sh processName

For example:

script.sh bash
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like