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

前端 未结 7 1865
孤独总比滥情好
孤独总比滥情好 2020-11-29 23:38

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: <

7条回答
  •  一生所求
    2020-11-30 00:35

    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?

提交回复
热议问题