I have written a simple daemon, pls suggest improvements (bash/python-Ubuntu)

826 views Asked by At

I have a bash script that basically starts an audio stream using mplayer:

#!/bin/bash
# startmusic.sh
/usr/bin/mplayer http://www.audiostream.com

what I want to make sure is that if mplayer chrashes or ends of any reason, it gets automatically restarted. I therefore have a cron job running every minute which also is a bash script:

#!/bin/bash
# interval.sh
if [ -z "$(pgrep mplayer)" ]; then
 #restart music
 (
  exec </dev/null
  exec >/dev/null
  exec 2>/dev/null
  umask 0
  cd /
 bash /home/user/startmusic.sh
 ) &
else
 echo "music already playing, no need to restart startmusic.sh"         
fi

One odd thing that makes this script not very useful is that it seems that mplayer is starting 2 instances, and if the player stops (as it e.g. does when the connection is lost), only 1 mplayer instance quits, still leaving one left. Since my programming skills are quite rudimentary, I would greatly appreciate any (easy-to-implement) solutions. Thanks in advance/J

1

There are 1 answers

2
thkala On

How about using an infinite loop:

#!/bin/bash
# startmusic.sh
while :; do
    /usr/bin/mplayer http://www.audiostream.com
done

If mplayer crashes, it will be simply restarted by the launcher script ad infinitum.

Of course, this means that you would have to kill the launcher script first and mplayer afterwards, if you ever wish to stop it permanently.

EDIT:

A script as simple as this should never terminate, except if one of the following happens:

  • It is terminated by you. I assume that you take reasonable precautions not to do that by mistake - and if you do, you will probably know and fix it yourself. If it is that critical, then you should run the script as a different user to protect it from most random mistakes.

  • The shell script crashes. Bash has had a few bugs, much like any other software, but I highly doubt that something as simple as a loop would cause a new one to show itself.

  • Something system-wide happens. An out-of-memory condition, a hardware issue or even the more common power-loss. In that case:

    1. Whatever system you may have to watch the script will be probably affected too.

    2. You have bigger fish to fry - like getting the system back online.

    3. Do you really want anything automated mucking around on a box with such issues?

Using cron for monitoring a process has two issues:

  • It is not always easy to get right - especially if, for example, there can be multiple instances of the same program (such as mplayer) running at the same time.

  • You have to disable the cron entry before the process of interest, if you want to shut it down for whatever reason.

If you really are serious about restarting the launcher script in case of <insert-favourite-catastrophe>, then you should probably use a proper process monitoring daemon such as:

http://ps-watcher.sourceforge.net/

http://mmonit.com/monit/

And if you think that there is no such thing as overkill:

http://www.nagios.com/

http://www.opennms.org/