Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rake check if already running

Is it possible somehow to execute rake task only if it is not running already, I want to use cron for executing some rake tasks but rake task shouldn't start if previous call is not finished Thanks

like image 900
Fivell Avatar asked Sep 06 '11 13:09

Fivell


People also ask

How do I check if a Rails server is running?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias “s” to start the server: rails s. The server can be run on a different port using the -p option.

What is rake test?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

How do you stop a server is already running in Rails?

This normally happens when you stop the server with ctrl+z instead of ctrl+c to exit the Rails server. ctrl+z suspends the process but doesn't close the server running on port 3000 meaning the server is still running on background. This can also happen when you close the terminal that the rails server was running on.


1 Answers

I use lockrun to prevent cron tasks from running multiple times (this only works when invoking the command through the same lockrun invocation, so if you need to protect from various invocation paths, then you'll need to look for other methods).

In your crontab, you invoke it like this:

*/5 * * * * /usr/local/bin/lockrun --lockfile=/var/run/this_task.lockrun -- cd /my/path && RAILS_ENV=production bundle exec rake this:task
like image 126
Jeremy Weathers Avatar answered Sep 25 '22 20:09

Jeremy Weathers