0

Background

  • I am using this script in order to make my script running as service.
  • I am using Linux Mint 17.3 Rosa.

The problem

Sometimes the start/stop works sometimes not. Sometimes, I have to manually kill the process because the service stop command doesn't do that.

Notice that the script uses the daemon tool.

#!/bin/sh

name="TEST"  
# The path of the client executable
command="${HOME}/myscript.sh"   
# Any command line arguments for the client executable
command_args=""  
# The path of the daemon executable
daemon="/usr/bin/daemon"

[ -x "$daemon" ] || exit 0
[ -x "$command" ] || exit 0

# Note: The following daemon option arguments could be in /etc/daemon.conf


# Any command line arguments for the daemon executable (when starting)
daemon_start_args="" # e.g. --inherit --env="ENV=VAR" --unsafe  

# The pidfile directory (need to force this so status works for normal users)
pidfiles="/var/run"  
# The user[:group] to run as (if not to be run as root)
user=""  
# The path to chroot to (otherwise /)
chroot=""  
# The path to chdir to (otherwise /)
chdir=""  
# The umask to adopt, if any
umask=""  
# The syslog facility or filename for the client's stdout (otherwise discarded)
stdout="daemon.info"  
# The syslog facility or filename for the client's stderr (otherwise discarded)
stderr="daemon.err"

case "$1" in  
    start)
        # This if statement isn't strictly necessary but it's user friendly
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo "$name is already running."
        else
            echo -n "Starting $name..."
            "$daemon" --respawn $daemon_start_args \
                --name "$name" --pidfiles "$pidfiles" \
                ${user:+--user $user} ${chroot:+--chroot $chroot} \
                ${chdir:+--chdir $chdir} ${umask:+--umask $umask} \
                ${stdout:+--stdout $stdout} ${stderr:+--stderr $stderr} \
                -- \
                "$command" $command_args
            echo done.
        fi
        ;;

    stop)
        # This if statement isn't strictly necessary but it's user friendly
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo -n "Stopping $name..."
            "$daemon" --stop --name "$name" --pidfiles "$pidfiles"
            echo done.
        else
            echo "$name is not running."
        fi
        ;;

    restart|reload)
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo -n "Restarting $name..."
            "$daemon" --restart --name "$name" --pidfiles "$pidfiles"
            echo done.
        else
            echo "$name is not running."
            exit 1
        fi
        ;;

    status)
        "$daemon" --running --name "$name" --pidfiles "$pidfiles" --verbose
        ;;

    *)
        echo "usage: $0 <start|stop|restart|reload|status>" >&2
        exit 1
esac

exit 0  
myscript.sh:
#!/bin/bash

cd $HOME/Stuff/
$(which node) app.js 

Attempts to fix this

1. Use start-stop-daemon

As an alternative, I tried to use this which uses the start-stop-daemon but it that doesn't run at all.

2. Debugging daemon script

To try and debug this I enabled debugging within the daemon command. I then attempted to start and stop my service.

When I start the service:

Starting TEST...daemon: debug: handle_stdout_option(spec = daemon.info)
daemon: debug: store_syslog(spec = daemon.info)
daemon: debug: handle_stderr_option(spec = daemon.err)
daemon: debug: store_syslog(spec = daemon.err)
daemon: debug: config()
daemon: debug: config_load(configfile = /etc/daemon.conf)
daemon: debug: config_load(configfile = /root/.daemonrc)
daemon: debug: config_process(target = *)
daemon: debug: config_process(target = TEST)
daemon: debug: handle_name_option(spec = TEST)
daemon: debug: handle_pidfiles_option(spec = /var/run)
daemon: debug: handle_stdout_option(spec = daemon.info)
daemon: debug: store_syslog(spec = daemon.info)
daemon: debug: handle_stderr_option(spec = daemon.err)
daemon: debug: store_syslog(spec = daemon.err)
daemon: debug: sanity_check()
daemon: debug:  preventing core files
daemon: debug:  constructing pidfile path
daemon: debug:  constructing command line arguments for the client
daemon: debug:  checking the client command
daemon: debug: safety_check("/home/myPC/myscript.sh")
daemon: debug:  checking "/home/myPC/myscript.sh"
daemon: debug:  checking #! interpreter: /bin/bash
TEST: debug:  becoming a daemon and locking pidfile
done.

And the service has been started.

When I stop the service:

Stopping TEST...daemon: debug: config()
daemon: debug: config_load(configfile = /etc/daemon.conf)
daemon: debug: config_load(configfile = /root/.daemonrc)
daemon: debug: config_process(target = *)
daemon: debug: config_process(target = TEST)
daemon: debug: handle_name_option(spec = TEST)
daemon: debug: handle_pidfiles_option(spec = /var/run)
daemon: debug: sanity_check()
daemon: debug:  preventing core files
daemon: debug:  constructing pidfile path
daemon: debug: show()
daemon: debug:  options:
daemon: debug:   config <none>, noconfig 0, name TEST, command "<none>", uid 0, gid 0, init_groups 0, chroot <none>, chdir <none>, umask 22, inherit no, respawn no, acceptable 300, attempts 5, delay 300, limit 0, idiot 0, foreground no, pty no, noecho no, stdout <none>, stderr <none>, errlog daemon.err, dbglog daemon.debug, core no, unsafe no, safe no, stop yes, running no, verbose 0, debug 3
daemon: debug:  command line:
daemon: debug:   environment:
daemon: debug:    SUDO_GID=1000
daemon: debug:    MAIL=/var/mail/root
daemon: debug:    USER=root
daemon: debug:    HOME=/home/myPC
daemon: debug:    LC_MONETARY=en_US.UTF-8
daemon: debug:    COLORTERM=mate-terminal
daemon: debug:    SUDO_UID=1000
daemon: debug:    LOGNAME=root
daemon: debug:    TERM=xterm
daemon: debug:    USERNAME=root
daemon: debug:    PATH=.../home/myPC/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
daemon: debug:    LC_ADDRESS=en_US.UTF-8
daemon: debug:    DISPLAY=:0.0
daemon: debug:    LC_TELEPHONE=en_US.UTF-8
daemon: debug:    LANG=en_US.UTF-8
daemon: debug:    LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
daemon: debug:    XAUTHORITY=/home/myPC/.Xauthority
daemon: debug:    SUDO_COMMAND=/usr/bin/env PATH=.../home/myPC/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /etc/init.d/moda stop
daemon: debug:    LC_NAME=en_US.UTF-8
daemon: debug:    SHELL=/bin/bash
daemon: debug:    SUDO_USER=myPC
daemon: debug:    LC_MEASUREMENT=en_US.UTF-8
daemon: debug:    LC_IDENTIFICATION=en_US.UTF-8
daemon: debug:    PWD=/home/myPC
daemon: debug:    LC_NUMERIC=en_US.UTF-8
daemon: debug:    LC_PAPER=en_US.UTF-8
daemon: debug:  stopping daemon /var/run/TEST.pid
done.

But the service never stops.

Even if I try sudo kill -9 PID it doesn't stop always (occasionally it will stop using this, but not consistently).

My debugging process

I am running the service like:

 sudo /etc/init.d/test start

I then watch for it like this using ps and lsof.

ps
$ ps aux |grep TEST
root      4393  0.0  0.1  20036  1348 ?        S    13:41   0:00 /usr/bin/daemon --respawn --name TEST --pidfiles /var/run --debug=3 --stdout daemon.info --stderr daemon.err -- /home/myPC/myscript.sh
root      4394  0.0  0.0      0     0 ?        Z    13:41   0:00 [myscript.sh] <defunct>
lsof
$ sudo lsof -p 4393
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
daemon  4393 root  cwd    DIR                8,2     4096      2 /
daemon  4393 root  rtd    DIR                8,2     4096      2 /
daemon  4393 root  txt    REG                8,2   190280 923071 /usr/bin/daemon
daemon  4393 root  mem    REG                8,2    43616 667389 /lib/x86_64-linux-gnu/libnss_files-2.19.so
daemon  4393 root  mem    REG                8,2    47760 667228 /lib/x86_64-linux-gnu/libnss_nis-2.19.so
daemon  4393 root  mem    REG                8,2    97296 667121 /lib/x86_64-linux-gnu/libnsl-2.19.so
daemon  4393 root  mem    REG                8,2    39824 667115 /lib/x86_64-linux-gnu/libnss_compat-2.19.so
daemon  4393 root  mem    REG                8,2  2924352 927252 /usr/lib/locale/locale-archive
daemon  4393 root  mem    REG                8,2  1840928 667355 /lib/x86_64-linux-gnu/libc-2.19.so
daemon  4393 root  mem    REG                8,2    10680 667382 /lib/x86_64-linux-gnu/libutil-2.19.so
daemon  4393 root  mem    REG                8,2   141574 667155 /lib/x86_64-linux-gnu/libpthread-2.19.so
daemon  4393 root  mem    REG                8,2   149120 667163 /lib/x86_64-linux-gnu/ld-2.19.so
daemon  4393 root    0u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    1u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    2u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    3uW  REG               0,17        5  22031 /run/TEST.pid
daemon  4393 root    4u  unix               0xf..     0t0  22032 socket
daemon  4393 root    7r  FIFO                0,9      0t0  22034 pipe
daemon  4393 root    9r  FIFO                0,9      0t0  22035 pipe

Question

The one thing I noticed is that in the above info, I can see /run/TEST.pid but this also shows up here too:

/var/run/TEST.pid

The daemon script uses only the /var/run directory. I am not sure if this is suppose to happen or not?

  • I'm not sure if there's enough to go on here; under what circumstances does it not start or not stop? I would suggest changing all of the $daemon calls to add --debug=3 and if you don't want to look at syslog, use the --dbglog option to point to a log file. – Jeff Schaller Jun 30 '16 at 18:05
  • @JeffSchaller:I updated! – George Jul 1 '16 at 11:26
  • If your app is not allowing itself to be killed, even with the sudo kill -9 then this would seem to indicate an issue with your app.js application, IMO. – slm Jul 1 '16 at 11:57
  • @slm:Hmm.I added a section for SIGTERM in my app.js and now I can kill it with sudo kill -9.But still the service doesn't stop when I do sudo /etc/init.d/test stop – George Jul 1 '16 at 12:22
  • The next thing to watch is the use of those .pid files, make sure they've got the right PID in them after starting, and debug to make sure daemon is reading the correct one and is using it and deleting the pid once it's done. I fine a lot of corner cases in this area that just are really hard to handle with SysV scripts like this. – slm Jul 1 '16 at 13:38

Your Answer

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

Browse other questions tagged or ask your own question.