Exported upstart script doesn't seem to always stop correctly?
For example, if there is a procfile line 'clock: source ../.bash_profile; ruby ./scheduler.rb'. (The sourcing is done to use RVM). 'rvmsudo foreman export upstart /etc/init -u brainstem' is used to export. The export command generates a file, brainstem-clock-1.conf, that contains:
start on starting brainstem-clock stop on stopping brainstem-clock respawn
exec su - brainstem -c 'cd /home/brainstem/brainstem; export PORT=5200; source ../.bash_profile; ruby ./scheduler.rb >> /var/log/brainstem/clock-1.log 2>&1
When 'stop brainstem' is run upstart just stops the shell session that is started by su but the child thin processes stick around.
I'm having the same problem with this. It's not exactly a Foreman bug, but the way that Foreman formats Upstart jobs can't be reliably killed, as far as I can tell.
It seems that killing a process started with su -c that uses command expansion fails to kill all processes spawned within the quotes:
[user@server ~]$ sudo su -c 'ruby -e "sleep 25"' - user2
^C
Session terminated, killing shell... ...killed.
[ec2-user@server ~]$ ps ax | grep ruby
2208 pts/0 S+ 0:00 grep ruby
[user@server ~]$ sudo su -c 'pwd; ruby -e "sleep 25"' - user2
/home/user2
^C
Session terminated, killing shell... ...killed.
[ec2-user@server ~]$ ps ax | grep ruby
2240 ? Sl 0:00 ruby -e sleep 25
2243 pts/0 S+ 0:00 grep ruby
As you can see, commands that follow a semicolon (or && for that matter) aren't killed following a SIGTERM.
I'm not sure I know a way around this; it seems like Upstart's features for fork detection expect fork and expect daemon won't work in this situation. I've tried wrapping the command line up in a shell script, without much luck.
Upstart expects the use of sudo su -c to run as a non-root user. Perhaps a better solution might be to use Upstart's env directive for environmental variables, and a single command after sudo su -c?
Woo! It's not just me losing my mind. Ran into this earlier tonight and spent a while digging into it for a fix.
As has been pointed out above, the way foreman writes out the su command means it doesn't kill the process properly when upstart stops the job. From a bit of searching around I found a thread about running upstart jobs as non priviledged users properly. From the answer given there you need to be careful in the order of the arguments passed to su within the upstart job definition.
For example, foreman generated me the following exec command to start my rake task, events:get_tweets:
exec su - rails -c 'cd /home/rails/app_name/releases/20111204193641; export PORT=5000; bundle exec rake events:get_tweets RAILS_ENV=production >> /var/log/tweet/web-1.log 2>&1'
That has the problem that when I run stop tweet it leaves a zombie rake task process around. Every time I use upstart to stop the running job it leaves a rake task around in fact. The rake task has been killed in some fashion because it's not doing any work (if I strace it, it's spinning in epoll_wait() forever). i can clean those zombie processes up with a simple kill <pid>, but it's kinda annoying that I have to.
Anyway, from putting together what foreman generated for me and what I found in that superuser post I managed to figure out an su series of arguments that runs my rake task as it should, and stops it when it's supposed to as well.
exec su -s /bin/bash -c 'cd /home/rails/app_name/current; export PORT=5000; exec "$0" "$@"' rails -- bundle exec rake events:get_tweets RAILS_ENV=production >> /var/log/tweet/web-1.log 2>&1
I've not looked at the internals of foreman yet, but I think it should be easy enough to change the exported file to follow the above format which works, rather than the current format.
Excellent, works for me.
Resolving ancient bugs. Please reopen if this is still an issue.
I'm having this issue. Did any of the fixes make it into master?
There's a potential fix sitting in a pull requests here.
Would it be possible to create a minimal test case out of a Procfile and maybe some bash scripts that you could use to demonstrate your problem?
I've tried to come up with a minimal test case: https://gist.github.com/1708158
I'm not sure it's a really good example, but I think apps which fail to stop have a similar structure. The app will stop perfectly when run with foreman start, but will just keep running when run via the Upstart scripts.
I meant to come up with a test case for this but didn't have time but it would look much like yours anyway.
I found that Pull Request #137 didn't work - some of the processes ended but some kept running.
Pull Request #102 did work for me and the processes start and stop reliably now although there is a problem with environment variables in /etc/profile not being available to the process.
Jon, without more info on the program you were running, I can't be sure why Pull Request #137 didn't work for you.
A SIGTERM is issued to all child processes, but your program can catch it and do something other than shutdown. (eg. uWSGI interprets SIGTERM as a brutal reload, while SIGINT is required to actually kill the children.) That kind of unusual behaviour requires a (very easy to write) wrapper script.
If you can confirm that your program does die properly when issued kill <pid> then I'll take another look at Pull Request #137.
Hmmm, I am also having issues with the way upstart handles the forking of child processes. In my case with gunicorn, works locally, but doesn't work after an export with upstart. After some searching, I found that it has to do with the way upstart manages the child processes, and I found this thread on SuperUser:
http://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
Using:
exec su -s /bin/sh -c 'exec "$0" "$@"' [username] -- [command]
Seems to do the trick for me (on centos6 for sure), and I haven't had any issues with stale child processes since.
+1 This should be reopened.
This is also still happening to me with old versions of upstart (0.6.5). I found that using --session-command instead of -c fixes the issue. The man page for 'su' says the difference is that the former doesn't create a new session.
+1 Happening here too.
I'm also on version 0.6.5 of Upstart (from 'yum install', Amazon EC2 Linux image).
@waj's solution of replacing '-c' with '--session-command' fixed it for me. (Currently using Capistrano to edit the generated .conf files during deployment.) Thanks for sharing @waj!
+1 seeing this as well on CentOS 6.4 (Vagrant Box)
+1 just adding my voice to this. Centos 6.4.
I am currently fixing this by running the following in a task after foreman:export and before foreman:restart in my config/deploy.rb for Capistrano.
for f in `ls /etc/init`; do sed -i.bak 's/-c/--session-command/' /etc/init/$f; done;
Having this problem in CentOS 6.4 too, and confirmed that @msaffitz fix works
For what it's worth, my version of the above (which fixes a few bugs just using it as-is causes) is below:
after 'foreman:export', 'foreman:fix_upstart_export'
namespace :foreman do
# See https://github.com/ddollar/foreman/issues/97
desc "Fix upstart exports to stop jobs correctly"
task :fix_upstart_export, roles: :app do
run "for f in `ls /etc/init/app-prefix*.conf`; do #{sudo} sed -i.bak 's/ -c / --session-command /' $f; done;"
end
end
where app-prefix above is a prefix all my upstart scripts used.
Unfortunately, this doesn't work for me as apparently Ubuntu's su doesn't have --session-command option, so still looking for a solution for myself.
@knewter thanks, your gist solved the issue for us on CentOS
I have the same issue on a newly configure Ubuntu 13.04 server (the exact same job run and stop fine on 12.04 and 12.10)
upstart version: 1.8-0ubuntu1 sudo version: 1.8.6p3-0ubuntu3 foreman version: 0.63.0
as @knewter said, ubuntu's su does not have --session-command option. I'm still looking into it, if you have any idea ?
After a lot of tries, I found a cleaner way to write my upstart script using upstart stanza instead or crappy su commands, so I defined my own template (may not work for you). This also fixes the orphan process issue:
start on starting <%= app %>-<%= name %>
stop on stopping <%= app %>-<%= name %>
respawn
setuid <%= user %>
setgid <%= user %>
env HOME=/home/<%= user %>
env PORT=<%= port %>
<% engine.env.each_pair do |var,env| %>
env <%= var.upcase %>=<%= shell_quote(env) %>
<% end %>
chdir <%= engine.root %>
exec bash -lc '<%= process.command %> >> <%= log %>/<%= name %>-<%= num %>.log 2>&1'
It's also much easier to read. Hope this helps.
I'm using bash -lc to load my env (especially rbenv) and I define the HOME variable because it's used in my .bashrc to load rbenv (and not defined by upstart).