Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

# sinatra      Startup script for Sinatra server.

sudo -u sinatrauser ruby /var/www/sinatra/control.rb $1

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
  exec 'ruby /var/www/sinatra/sintraserver.rb >> /var/log/sinatra/sinatraOutput.log 2>&1'

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/, 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
share|improve this question
up vote 1 down vote accepted

The form of exec that you are using is the cause of this. If you separate the command from the parameters it will avoid creating a shell. See this github issue for someone fixing a similar problem:

share|improve this answer
Thanks! That did the trick. – Jonathan Scoles Aug 2 '12 at 18:53

The first process is sudo that launches ruby. Check with ps -A f. Change the sudo line to run a command.

su sinatrauser -c "ruby /var/www/sinatra/control.rb $1"

edit: I think su looks neater in scripts, like this:

    su - someuser <<CHBACK
    ls $HOME // do stuff
    echo "do more stuff as $USER"
    echo "Now I'm $USER again"
share|improve this answer

Your Answer


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.