Yah, you've got the syntax of the brackets around if wrong. You can use [ or [[ but both require a space after them (see man [ for more info on the classic test program).
But, I assume you want this to work for more than just crond so I made it into a script with an arg.
$ cat is_cmd_running.sh
#!/bin/bash
psout=$(ps -ef | grep -v grep | grep -v $0 | grep $1)
if [[ -z ${psout:-} ]]; then
echo "$1 is not running"
else
echo "$1 is running"
fi
Note that in addition to the filter -v grep, I also had to filter -v $0 or else the ps of the program it self spuriously matches. This program works on my system:
$ bash is_cmd_running.sh firefox
firefox is running
$ bash is_cmd_running.sh konqueror
konqueror is not running
There's also a simpler version where you don't test a variable, but just evaluate the return code from the last grep. The normal thing to do is to use the -q flag to make grep quiet then just evaluate the return code. This is more traditional, I think, in shell scripts:
$ cat is_cmd_running.sh
#!/bin/bash
if ps -ef | grep -v grep | grep -v $0 | grep -q $1 ; then
echo "$1 is running"
else
echo "$1 is not running"
fi
And this also works:
$ bash is_cmd_running.sh konqueror
konqueror is not running
$ bash is_cmd_running.sh firefox
firefox is running
EDIT:
@tripleee makes a good point about the reimplementation of standard commands and the importance of linters (thankfully, I have shellcheck installed). So, I present a final version, accepted by shellcheck, which demonstrates how to use if directly on a process' return code without assigning output to a variable:
$ cat is_cmd_running.sh
#!/bin/bash
if pgrep "$1" >/dev/null ; then
echo "$1 is running"
else
echo "$1 is not running"
fi
$ shellcheck is_cmd_running.sh
$ echo $?
0
$
:)