I am trying to check if a process (assume it is called some_process
) is running on a server. If it is, then echo 1, otherwise echo 0.
This is the command that I am using but it only works partially (more info below). Note that I need to write the script in one line.
ps aux | grep some_proces[s] > /tmp/test.txt && if [ $? -eq 0 ]; then echo 1; else echo 0; fi
Note: The [s]
in some_proces[s]
is to prevent grep
from returning itself.
If some_process
is running, then "1"
gets echoed, which is fine. However, if some_process
is not running, nothing gets echoed.
ps -Ccmd
to find processes whose command name is "cmd", which can completely eliminate thegrep
.ps
will set the exit code to some non-zero value if it fails to find a matching process.ps -C php && echo 1 || php d.php
this means if a php is running print an echo if not execute that