Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

#!/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
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: https://github.com/sunspot/sunspot/pull/221.

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"
    CHBACK
    echo "Now I'm $USER again"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .