Make sure a certain number of scripts remain running on your server

Ever need to make sure a certain number of scripts remain running on your server? Here’s a quick shell script that migh help you out:

#!/bin/bash
count=0
for x in `ps ax | grep -v grep | grep my_cool_service | cut -c1-2`
do
  count=`expr $count + 1`
done

if [ $count -lt 35 ]
then
  echo $count
  echo “time to start some more services”
  while [ $count -lt 35 ]; do
    perl /home/falicon/my_cool_service.pl &
    count=`expr $count + 1`
  done
  break
fi

It’s a pretty simple script overall, and hopefully you can grok the basic idea of what it’s doing. But just to be thorough I’ll break down the few parts I find most interesting:

We start the program with a simple counter, and then we take advantage of some command line fu:

for x in `ps ax | grep -v grep | grep my_cool_service | cut -c1-2`

If you drop down to a command line and just run the ps aux | grep -v grep | grep my_cool_service part of the above command, what you’ll get is a list of the processes that are running that include the name ‘my_cool_service’…one per line. The cut -c1-2 command tells the program to trim each line down to just the first two characters and more importantly it returns the result as an iteratable set of data (so we can loop over it with our for loop).

So essentially it’s saying loop through the lines that our ps ax command returned. For each line that it returns we just increment our counter.

After we’ve looped through the list of running processes, our counter now holds the number of “running” processes. So we can do a simple if statement to check if the number is below the threshold of what we want. If it is, then we loop from our counter value to our threshold and start a new process each time.

To ensure we start the right amount, we also increment our counter each time we start a new process. Once we have started enough processes, we just break out of our if statement to reach the bottom of our script and we’re done!

Now you can simply set up a cronjob to run this script on some interval that you are comfortable with, and you’ll have a no fuss way to ensure that around 35 my_cool_service.pl scripts are running on your machine at any given time (the actual number of scripts running would depend on how often you run this shell script to keep them going and how often your my_cool_service.pl script crashed, closed, or stopped executing).

Short. Sweet. Simple…now that’s a hack that’s just my style.

This post has received 45 loves.


ARCHIVE OF POSTS



This is the personal blog of Kevin Marshall (a.k.a Falicon) where he often digs into side projects he's working on for digdownlabs.com and other random thoughts he's got on his mind.

Kevin has a day job as CTO of Veritonic and is spending nights & weekends hacking on Share Game Tape. You can also check out some of his open source code on GitHub or connect with him on Twitter @falicon or via email at kevin at falicon.com.

If you have comments, thoughts, or want to respond to something you see here I would encourage you to respond via a post on your own blog (and then let me know about the link via one of the routes mentioned above).