Tweetegy On the edge of chaos with Blockchain, Dapps, DeFi, JavaScript

| About | Search | Archive | Github | RSS |

Setting up a scheduled task (cron job) to run a rake script in Ubuntu

To schedule a task in Ubuntu you need to understand cron and crontab. Both these concepts are easy to grasp. However, nothing is always as smooth as you would want! The issues I had to resolve are:

For the syntax, I recommend checking out this Wikipedia article on cron. I will explain each of the above areas, starting with adding a crontab for a user.

Adding a crontab for a user

In order to edit a crontab for a user, you must type the following in the terminal:

sudo crontab -e

Note that I prefix the command with “sudo” which means it will modify the cron file for the user root. When I first tried this there was the following error: “no crontab for root”. I solved this problem with the following script which simply creates an empty file and associates that as a crontab for user root.

touch rootcron
sudo crontab -u root rootcron

Now you will be able to edit the file using sudo crontab -e and it will work just fine.

Creating an executable shell script which calls rake

Let’s say you have a rake script in /var/www/someproject/rakefile. You can execute the rake script using the following script:

#!/bin/sh
date
echo "Executing rake"
cd /var/www/someproject/
rake

You must make this script an executable before cron can start it up. Here’s how:

sudo chmod +x runrake.sh

You can now execute this every 24hrs at midnight (for example) by adding the following line to your cron. Note that the full path to the script is included and there is a “./” before the name of the script, which is how executable files can be run in the terminal. Next issue. Sometimes the cron job would run but not complete or just did not appear to run at all. How can I get more information about what is happening? Let’s check out logging!

0 0 * * * sudo /var/www/someproject/./runrake.sh

Logging the output

It turns out to be really straight forward to log the output of a cron job. Simply use the redirect output syntax to send the standard output to a file. Use single > to create a new file each time the cron is run or double » to append to your log file. You can put the file anywhere you like, however, it is recommended to put inside the standard log folder /var/log. So now the line in the cron file looks as follows:

0 0 * * * sudo /var/www/someproject/./runrake.sh > /var/log/runrake.log