Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to check running process [duplicate]

Tags:

bash

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)

like image 604
elasticsecurity Avatar asked May 25 '10 09:05

elasticsecurity


1 Answers

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 
like image 189
Andor Avatar answered Oct 05 '22 23:10

Andor