I am trying to write a python script which shows me the services which are running on my server.

The thing is that mongodb keeps going down on the server and each time it happens I have to manually write the command to start it up.

What I wanted to do was automate this job for some services if they go down.

Currently i have written this https://gist.github.com/prodicus/67609dd984309d0a82f9

I know its really basic guys. It has miles to go :)

Now how do I start the services mongodb and apache through this script ? Any suggestions would be really helpful guys.

  • Also asked on serverfault.com/questions/710470/… – Marco Aug 3 '15 at 13:11
  • 1
    What distribution are you using? Most modern process managers will (well, "can", if properly configured) take care of automatically restarting failed services for you. – larsks Aug 3 '15 at 13:46
  • 1
    Would be even better to see if you can find the reason why MongoDB is going down. – Marco Aug 3 '15 at 13:48
  • 1
    Do yourself a favor and get to the root of the problem. Check the logs – Creek Aug 5 '15 at 2:46

Quick and dirty way:

You can put a script on a cron to run every X minutes and restart mongodb if it is not running.

The main thing about this simple script below, it relies on the grep response code to determine if it should restart or not. However, you need that second grep in there because the first grep will always return successful, due to the grep command itself appearing in the process list. So if you say "Grep for THIS STRING" in the output of ps, the grep first will always find that because it will always find the process of the command "Grep for THIS STRING".

Certainly there are better and more pretty ways of doing this, but the logic below works. At this end of the day, it may be ugly but it works period. I borrowed from a simple script I had to write to ensure an extremely slow sync from another continent would keep running, even though the network was extremely poor and dropped the connection regularly.


# DATE var only included in case you want some rudimentary logging on when mongo drops/restarts. You will have to add that yourself.

DATE=`date +%m-%d-%H:%M:%S`

ps -ef | grep 'unique first part of unique string that you see from ps which indicates mongo is running' | grep 'second part of unique string that you see from ps when mongo is running'; 

if [ $? == "1" ]; 

command to restart mongodb; can also include other commands to make sure it runs correctly like removing stale locks etc; 


I am trying to write a python script which shows me the services which are running on my server.

Don't do that. That's exactly what a process manager like upstart or systemd is for. Using systemd, a simple unit file for mongo might look something like:

Description=High-performance, schema-free document-oriented database
After=syslog.target network.target

ExecStart=/usr/bin/mongod $OPTIONS run


If you want this to restart automatically when it fails, you can add to the [Service] section:


If you are unlucky enough to be working on a system without a modern process manager, you can use a tool like supervisor to achieve the same effect.

But for real, if your application keeps crashing, you are much better off trying to find the source of the problem. Mongodb logs information to wherever logpath in your /etc/mongod.conf file points (/var/log/mongodb/mongodb.log on my system), and this is probably the first place to look for problems.

Other applications provide similar diagnostic information that can help find the root cause of the crashes.


Does it run as root? In that case you can do service xxxx start in your script where xxxx is mongodb or apache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.