Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

The reason I'm doing this script is because I want to deamonize 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 
SUPERVISORD=/usr/local/bin/supervisord
PIDFILE=/home/user/supervisor/supervisord.pid
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
 start)
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
;;
stop)
log_begin_msg "Stopping Supervisor daemon manager..."
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE || log_end_msg 1
log_end_msg 0
;;

restart|reload|force-reload) 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 esac

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 running multiple processes if I do "script start" 5 times, it should complain saying that the process is already running. Please help

share|improve this question
    
Your question isn't clear enough, what are you trying to do? –  i08in May 6 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 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. –  i08in May 6 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 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 at 1:36

1 Answer 1

up vote 0 down vote accepted

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 
    fi

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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