how to run rake task in background in rails
Asked Answered
H

2

9

This is my command

bundle exec rake resque:work QUEUE="*" --trace

I want to run this command on my server as a background process.

please help me.

Hustle answered 29/5, 2013 at 14:27 Comment(0)
B
34

A method I often use is:

nohup bundle exec rake resque:work QUEUE="*" --trace > rake.out 2>&1 &

This will keep the task running even if you exit your shell. Then if I want to just observe trace output live, I do:

tail -f rake.out

And you can examine rake.out at any time.

If you need to kill it before completion, you can find it with ps and kill the pid.

Bonnard answered 29/5, 2013 at 14:34 Comment(5)
tail -f rake.out does not show me the console output from the rake process. How to see the console output?Jeffereyjefferies
@Jeffereyjefferies when I use teh above technique, I do get the rake console output. What does your command line look like?Bonnard
So I execute nohup bundle exec rake pr:monitor --trace > monitor.out 2>&1 &. When I tail -f monitor.out, I get ** Invoke pr:monitor (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute pr:monitorJeffereyjefferies
@Jeffereyjefferies if you run that without the nohup and without the > monitor.out 2>&1 & do you get any more output than that? Maybe it's an issue with the rake task? Perhaps you should open a stackoverflow.com question for the issue.Bonnard
Assume after run the daemon task you have closed the terminal(console) . Now reopen and go the folder and ps doesn't give the background task. So how to kill it?Meninges
D
8

Just in case somebody finds this 4 years later, bundle has an elegant way of doing this now. For example if you want to run sidekiq in the background you can do:

bundle exec sidekiq -e production -d -L ./log/sidekiq.log 

The -d daemonizes to run in the background, but you will also need to use the -L to provide a logfile, else bundler will refuse to run your command in the background (deamonize). Tested with bundler version 1.15.4

Update Oct 2019. While the command still works in general, the specific command above will no longer work for sidekiq 6.0+, you'll need to use Upstart or Systemd if you use Linux: https://github.com/mperham/sidekiq/wiki/Deployment#running-your-own-process

Dietsche answered 14/10, 2017 at 13:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.