Search code examples
linuxinitgentoo

Should init scripts daemonize a process & are pid's essential?


I'm working on getting an init script written for the web-server shiny-server (shiny-server is a version of the R package shiny that allows you to run stand alone web-applications based on R).

I'm working through the Gentoo Handbook : Writing Init Scripts and am reading the Gentoo Developers Guide (as my ultimate aim is to develop an ebuild for installing this on Gentoo, bit of a way to go though).

I had some trouble getting the init script to start so asked on the Shiny Google Group for assistance and one of the developers provided a solution, but at the same time the question arose as to whether Gentoo expects...

a) to have a pid file for each process started.

b) whether it is "best practice" to fork and daemonize a process such as this under Gentoo.

I don't know but am seeking advice to feed back to the developer(s) who seem very open and receptive to getting their software out there and working with distributions.

Currently my init script, which works, looks like....

#!/sbin/runscript
depend(){
    after net
}
start(){
  ebegin "Starting shiny-server"
  start-stop-daemon --start --exec /usr/bin/shiny-server >> /var/log/shiny-server.log 2>&1
  local _retval=$?
  eend "${_retval}"
}
stop(){
  ebegin "Stopping shiny-server"
  start-stop-daemon --start --exec /usr/bin/shiny-server >> /var/log/shiny-server.log 2>&1  
  local _retval=$?
  eend "${_retval}"
}

Thanks in advance for your time,

slackline


Solution

  • Ok, I asked in #gentoo on IRC and was advised by bonsaikitten...

    "whatever works best" - start-stop-daemon can create pids too, but I think it's easier to use the function if the process provides it already and daemonized - well, it needs to detach from the current terminal somehow, so either let it do that by itself, or use s-s-d to do it

    So I've opted to go with the following to daemonize to background and create PID files...

    #!/sbin/runscript
    
    PIDFILE=/var/run/shiny-server.pid
    
    depend(){
        after net
    }
    start(){
      ebegin "Starting shiny-server"
      start-stop-daemon --start --make-pidfile --pidfile "${PIDFILE}" \
          --background --exec /usr/bin/shiny-server >> /var/log/shiny-server.log 2>&1
      local _retval=$?
      eend "${_retval}"
    }
    stop(){
      ebegin "Stopping shiny-server"
      start-stop-daemon --stop --pidfile "${PIDFILE}"
      local _retval=$?
      eend "${_retval}"
    }