how to run rake task in background in rails

2020-06-17 15:26发布

问题:

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.

回答1:

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.



回答2:

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