11

capistrano task

namespace :service do
  desc "start daemontools (svscan/supervise/svscanboot)"
  task :start, :roles => :app do
    sudo "svscanboot&"
  end
end

Now this doesn't work: the svscanboot process simply doesn't run. This helped me find sleep: https://github.com/defunkt/resque/issues/284 other sources pointed me to nohup, redirection, and pty => true, so I tried all these.

run "nohup svscanboot >/tmp/svscanboot.log 2>&1 &"   # NO
run "(svscanboot&) && sleep 1"                       # NO
run "(nohup svscanboot&) && sleep 1"                 # YES!

Now, could anyone explain to me why i need the sleep statement and what difference does nohup make? For the record all the above run equally well if run from user shell, problem is only in the context of capistrano.

thanks

Viktor Trón
  • 8,774
  • 4
  • 45
  • 48
  • `nohup` ensures that you your command doesn't terminate when it receives the `SIGHUP` signal after the shell terminates. However, I am also curious about why the sleep command makes a difference. – Graeme Jun 09 '13 at 04:59
  • Sleep makes a difference in my world as well. It's mystifying why such a simple task is so amazingly difficult with Capistrano. – cbmanica Sep 15 '13 at 00:21
  • I see the same behavior. And wondering about the same thing as well. – aslakjo Oct 16 '13 at 07:29

5 Answers5

1

Try forking the process as explained here: Spawn a background process in Ruby

You should be able to do something like this:

job1 = fork do
  run "svscanboot"
end

Process.detach(job1)

As well, checkout this: Starting background tasks with Capistrano

Community
  • 1
  • 1
1

My simple solution would be make svscanboot.sh file at remote server with whatever code you want to run. In your case

svscanboot >/tmp/svscanboot.log 2>&1

In cap rake task add this

run "sh +x somefile.sh &"

this works well for me.

0

I think nohup just launches the process in background, so you don't need to explicitly set the last &.

Did you try

run "nohup svscanboot >/tmp/svscanboot.log 2>&1"

(without the ending & to send it to the background).

That should work and remain running when your current capistrano session is closed.

Fer
  • 3,247
  • 1
  • 22
  • 33
0

Try this

run "nohup svscanboot >/tmp/svscanboot.log 2>&1 & sleep 5", pty: false
0

I'd like to share my solution which also works when executing multiple commands. I tried many other variants found online, including the "sleep N" hack.

run("nohup sh -c 'cd #{release_path} && bundle exec rake task_namespace:task_name RAILS_ENV=production > ~/shared/log/<rakelog>.log &' > /dev/null 2>&1", :pty => true)
okor
  • 666
  • 8
  • 13