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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With