Ubuntu - Ruby Daemon script creates two processes - sh and ruby - PID file points at sh, not ruby
Posted
by
Jonathan Scoles
on Super User
See other posts from Super User
or by Jonathan Scoles
Published on 2011-10-14T18:32:41Z
Indexed on
2012/06/22
9:18 UTC
Read the original article
Hit count: 606
The PID file for a ruby process I have running as a daemon is getting the wrong PID. It appears that running /etc/init.d/sinatra start
creates two processes - sh
and ruby
, and the PID that ends up in the PID file is that of the sh
process. This means that when I then run /etc/init.d/sinatra stop
or /etc/init.d/sinatra restart
, it is killing sh and leaving the ruby process still running.
I'd like to know a) why is my script launching two processes - sh and ruby, and not just ruby, and b) how do I fix it to just launch ruby?
Details of the setup:
I have a small Sinatra server set up on an ubuntu server, running as a daemon. It is set to automatically at server startup run a script named sinatra
in /etc/init.d
that launches the a control script control.rb
, which then runs a ruby daemon command to start the server. The script is run under the 'sinatrauser' account, which has permissions for the directories the script needs.
contents of /etc/init.d/sinatra
#!/bin/bash
# sinatra Startup script for Sinatra server.
sudo -u sinatrauser ruby /var/www/sinatra/control.rb $1
RETVAL=$?
exit $RETVAL
To install this script, I simply copied it to /etc/init.d/
and ran
sudo update-rc.d sinatra defaults
contents of /var/www/sinatra/control.rb
require 'rubygems'
require 'daemons'
pwd = Dir.pwd
Daemons.run_proc('sinatraserver.rb', {:dir_mode => :normal, :dir => "/opt/pids/sinatra"}) do
Dir.chdir(pwd)
exec 'ruby /var/www/sinatra/sintraserver.rb >> /var/log/sinatra/sinatraOutput.log 2>&1'
end
portion of output from ps -A
6967 ? 00:00:00 apache2
10181 ? 00:00:00 sh <--- PID file gets this PID
10182 ? 00:00:02 ruby <--- Actual ruby process running Sinatra
12172 ? 00:00:00 sshd
The PID file gets created in /opt/pids/sinatra/sinatraserver.rb.pid
, and always contains the PID of the sh instance, which is always one less than the PID of the ruby process
EDIT: I tried micke's solution, but it had no effect on the behavior I am seeing.
This is the output from ps -A f
. This output looks the same whether I use sudo -u sinatrauser ...
or su sinatrauser -c ...
in the service start script in /etc/init.d.
1146 ? S 0:00 sh -c ruby /var/www/sinatra/sinatraserver.rb >> /var/log/sinatra/sinatraOutput.log 2>&1
1147 ? S 0:00 \_ ruby /var/www/sinatra/sinatraserver.rb
© Super User or respective owner