Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run rake task in background in rails

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.

like image 468
manish nautiyal Avatar asked May 29 '13 14:05

manish nautiyal


2 Answers

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.

like image 186
lurker Avatar answered Nov 13 '22 07:11

lurker


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

like image 36
brod Avatar answered Nov 13 '22 08:11

brod