The reason I'm doing this script is because I want to daemonize supervisord: right now I'm in a cloud host in which I don't have access to the /etc or the sudo command; this script is my best attempt to do everything from my /home. I have this bash script with the start-stop-daemon command but it allows me to run it multiple times, and I would like it to just run just once. So far here is what I got:

#! /bin/bash 
OPTS="-c /home/user/supervisor/supervisord.conf -j $PIDFILE"

test -x $SUPERVISORD || exit 0

. /lib/lsb/init-functions

export PATH="${PATH:+$PATH:}/usr/local/bin:/usr/sbin:/sbin"

case "$1" in
    log_begin_msg "Starting Supervisor daemon manager..."
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $SUPERVISORD -- $OPTS || log_end_msg 1
    log_end_msg 0

    log_begin_msg "Stopping Supervisor daemon manager..."
    start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE || log_end_msg 1
    log_end_msg 0

    log_begin_msg "Restarting Supervisor daemon manager..."
    start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDFILE
    start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $SUPERVISORD -- $OPTS || log_end_msg 1
    log_end_msg 0

    log_success_msg "Usage: {start|stop|reload|force-reload|restart}"
    exit 1

exit 0

I assume when the pid file is created it overwrites the previous one, so doing multiple stops doesn't work either. How can I make this script just run once, and not run multiple processes if I do "script start" 5 times? It should complain saying that the process is already running.

  • Your question isn't clear enough, what are you trying to do? – jobin May 6 '14 at 21:10
  • I'll edit my question but the basic idea is "not to start supervisord if it is already running", because if I run script start 5 time it will create 5 processes of supervisord – ocespedes May 6 '14 at 21:13
  • If you start a daemon using sudo service start <daemon> it will report it is already running if it is, and not restart it. – jobin May 6 '14 at 21:16
  • Yeah that could work but the problem is I have my server in a cloud host and the /etc or sudo are locked that's why I did it this way – ocespedes May 6 '14 at 21:26
  • 1
    What is the content of /home/user/supervisor/supervisord.pid after you start supervisord? What is a content of the same file after you start a second instance of supervisord? – sмurf May 9 '14 at 1:36

I find out what was the problem, my script was not verifying if the process was already running so it just started as many process as you run it, but adding:

    if [ -f $PIDFILE ]; then
      echo "supervisord already running: $PIDFILE" 
      exit 1 

in the start) case solved my problem, this verifies if supervisord has generated its respective .pid file and if it exists it does nothing.

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.