Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Script to check if process is running and act on the result

I have a process that fails regularly & sometimes starts duplicate instances..

When I run: ps x |grep -v grep |grep -c "processname" I will get: 2 This is normal as the process runs with a recovery process..

If I get 0 I will want to start the process if I get: 4 I will want to stop & restart the process

What I need is a way of taking the result of ps x |grep -v grep |grep -c "processname"

Then setup a simple 3 option function

ps x |grep -v grep |grep -c "processname"
if answer = 0 (start process & write NOK & Time to log /var/processlog/check)
if answer = 2 (Do nothing & write OK & time to log /var/processlog/check)
if answer = 4 (stot & restart the process & write NOK & Time to log /var/processlog/check)

The process is stopped with killall -9 process The process is started with process -b -c /usr/local/etc

My main problem is finding a way to act on the result of ps x |grep -v grep |grep -c "processname".

Ideally, I would like to make the result of that grep a variable within the script with something like this:

process=$(ps x |grep -v grep |grep -c "processname")

If possible.

like image 543
linuxnoob Avatar asked Nov 23 '13 13:11

linuxnoob


People also ask

How do you check if a process is running on Linux?

You can list running processes using the ps command (ps means process status). The ps command displays your currently running processes in real-time.

How do I check if a process is running in Linux bash?

Bash commands to check running process: pgrep command – Looks through the currently running bash processes on Linux and lists the process IDs (PID) on screen. pidof command – Find the process ID of a running program on Linux or Unix-like system.

How check if process is running PID Linux?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.


3 Answers

Programs to monitor if a process on a system is running.

Script is stored in crontab and runs once every minute.

This works with if process is not running or process is running multiple times:

#! /bin/bash  case "$(pidof amadeus.x86 | wc -w)" in  0)  echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt     /etc/amadeus/amadeus.x86 &     ;; 1)  # all ok     ;; *)  echo "Removed double Amadeus: $(date)" >> /var/log/amadeus.txt     kill $(pidof amadeus.x86 | awk '{print $1}')     ;; esac 

0 If process is not found, restart it.
1 If process is found, all ok.
* If process running 2 or more, kill the last.


A simpler version. This just test if process is running, and if not restart it.

It just tests the exit flag $? from the pidof program. It will be 0 of process is running and 1 if not.

#!/bin/bash pidof  amadeus.x86 >/dev/null if [[ $? -ne 0 ]] ; then         echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt         /etc/amadeus/amadeus.x86 & fi 

And at last, a one liner

pidof amadeus.x86 >/dev/null ; [[ $? -ne 0 ]] && echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt && /etc/amadeus/amadeus.x86 & 

This can then be used in crontab to run every minute like this:

* * * * * pidof amadeus.x86 >/dev/null ; [[ $? -ne 0 ]] && echo "Restarting Amadeus:     $(date)" >> /var/log/amadeus.txt && /etc/amadeus/amadeus.x86 & 

cccam oscam

like image 188
Jotne Avatar answered Sep 22 '22 01:09

Jotne


I adopted the @Jotne solution and works perfectly! For example for mongodb server in my NAS

#! /bin/bash  case "$(pidof mongod | wc -w)" in  0)  echo "Restarting mongod:"     mongod --config mongodb.conf     ;; 1)  echo "mongod already running"     ;; esac 
like image 37
Tirias Avatar answered Sep 25 '22 01:09

Tirias


I have adopted your script for my situation Jotne.

#! /bin/bash

logfile="/var/oscamlog/oscam1check.log"

case "$(pidof oscam1 | wc -w)" in

0)  echo "oscam1 not running, restarting oscam1:     $(date)" >> $logfile
    /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1 &
    ;;
2)  echo "oscam1 running, all OK:     $(date)" >> $logfile
    ;;
*)  echo "multiple instances of oscam1 running. Stopping & restarting oscam1:     $(date)" >> $logfile
    kill $(pidof oscam1 | awk '{print $1}')
    ;;
esac

While I was testing, I ran into a problem.. I started 3 extra process's of oscam1 with this line: /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1 which left me with 8 process for oscam1. the problem is this.. When I run the script, It only kills 2 process's at a time, so I would have to run it 3 times to get it down to 2 process..

Other than killall -9 oscam1 followed by /usr/local/bin/oscam1 -b -c /usr/local/etc/oscam1 -t /usr/local/tmp.oscam1, in *)is there any better way to killall apart from the original process? So there would be zero downtime?

like image 45
linuxnoob Avatar answered Sep 23 '22 01:09

linuxnoob