I wrote a bash-script to check if a process is running. It doesn't work since the ps command always returns exit code 1. When I run the ps command from the command-line, the $? is correctly set, but within the script it is always 1. Any idea?
#!/bin/bash SERVICE=$1  ps -a | grep -v grep | grep $1 > /dev/null result=$? echo "exit code: ${result}" if [ "${result}" -eq "0" ] ; then     echo "`date`: $SERVICE service running, everything is fine" else     echo "`date`: $SERVICE is not running" fi   Bash version: GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
There are a few really simple methods:
pgrep procname && echo Running  pgrep procname || echo Not running  killall -q -0 procname && echo Running  pidof procname && echo Running 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With