3

I created a pretty simple script called main_start_script that will run 2 python programs in background, code is:

python /opt/cuckoo/cuckoo-2/cuckoo.py 1>cuckoo_script/cuckoo_start.txt 2>&1 &
python /opt/cuckoo/cuckoo-2/utils/web.py 1>cuckoo_script/web_start.txt 2>&1 &

Now I wonder how can I check if these two programs are running, and if not, run the script to start them.

I heard people recommend using "Puppet" to check, but I haven't found a useful tutorial so far.

3
  • Puppet is useful if you already have it or you have more than just this to do . If it's just this you might look at supervisord
    – Bratchley
    Jun 28, 2016 at 20:39
  • Oh I just need to do this. So for using supervisord, can it do "ps aux | grep" to check if the program I started is running?
    – Xiechen
    Jun 28, 2016 at 20:43
  • @Bratchley A update for using supervisord. I tried to add the command for running the script file inside the configuration. However after I start supervisord, it all exited, and I received error message saying "too many start retries too quickly"
    – Xiechen
    Jun 28, 2016 at 21:04

2 Answers 2

3

See this similar stackoverflow question. The pgrep command can check the existance of a running process.

The comman pgrep -f <file> will return the number of current instances of the specified file. For example:

[user@localhost ~]$ pgrep -f /sbin/init
1
[user@localhost ~]$

It will return nothing if the file is not associated with a process.

This can be incorporated into a bash script that will execute the script if it is not already running:

#!/bin/bash
if [[ ! $(pgrep -f script.sh) ]]; then
    script.sh
fi
1
  • Thanks for your reply! If I run the script, and I do <ps aux | grep> I shall see a process related to <cuckoo.py> and a process related to <web.py>. But if I do <pgrep -f main_start_script>, I get <3495 30132 30136>
    – Xiechen
    Jun 28, 2016 at 21:12
2

Save each command's PID number using bash's special shell var '$!' which is the process pid of the last command, then just check '/proc/PID' existence before trying to run the command again.

#!/bin/bash

do_cuckoo() {
  python /opt/cuckoo/cuckoo-2/cuckoo.py 1>cuckoo_script/cuckoo_start.txt 2>&1 & echo $! >/tmp/cuckoo.pid
}
do_web() {
  python /opt/cuckoo/cuckoo-2/utils/web.py 1>cuckoo_script/web_start.txt 2>&1 & echo $! >/tmp/web.pid
}

do_cuckoo && do_web # First time run

# You should add a proper sleep timer below.
while :; do
  [ ! -d /proc/$(</tmp/cuckoo.pid) ] && do_cuckoo
  [ ! -d /proc/$(</tmp/web.pid) ] && do_web
done

Perhaps instead of that while loop it would be best to use 'until'. I'll leave you to that tasks ;)

3
  • Thanks for the reply! Sorry I'm still a noob to Linux so I have some questions on this code. Since I already include those two python codes in one script, should I just first modify the script to add the echo PID, and then modify the function call to make it just one function to run the script? And where should I put the sleep timer, say 1 minute? One more is that the [ ! -d ...] in the loop means if PID DNE?
    – Xiechen
    Jun 28, 2016 at 21:49
  • You can merge it into one function but you'll have to add if statements. w.r.t sleep timer, add that in the while loop i.e while :; do sleep 5m; ...;done
    – likewhoa
    Jun 28, 2016 at 22:24
  • 1
    For the record, this is a Linux-specific solution. I don't think there's a /proc directory on Mac. Instead, you should do kill -0 $(<pidfile) 2>/dev/null and check the error status. Jun 29, 2016 at 2:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .