linux Schedule System Updates

Viewing All Cron jobs

Using the command below, one can view all existing Cron jobs. The command display jobs for the current user you are logged in as, which in this example is root.

crontab -l

Screenshot showing the result of the crontab -l
 command.

To view other user Cron jobs, use the command crontab -u USERNAME -l while ensuring to change the “USERNAME” part with the actual username.

Editing and Adding Cron jobs 

To add and schedule a cron job, use the command crontab -e to open the editor and begin entering a task.

Depending on your editor, the file will open for editing as shown below.

Screenshot showing the crontab editor.

Now you can enter a new entry. For example, to have the system check for updates and update all available packages automatically every Wednesday at 2AM, then the following command would be used.

* 2 * * 3 sudo yum update -y > /dev/null 2>&1

screenshot showing the result of the command: * 2 * * 3 sudo yum update -y > /dev/null 2>&1
.

Redirecting the Scheduled Task’s Output to a File

You can enter and schedule most commands and tasks including sending the output to a file using > to overwrite a file each time or append to it using >> . For example,

* 2 * * 3 sudo yum update -y > /home/hivelocity/update_status.txt – this command will send the output to the file we listed while overwriting the file each time the task runs.

* 2 * * 3 sudo yum update -y >> /home/hivelocity/update_status.txt – this command will send the output to the file we listed while adding the output to the existing file each time the task runs, which will have a file with all the outputs from every instance that the file ran.

Redirecting the Scheduled Task’s Output to an Email Address

To have the output of all the commands being run in the crontab file sent to an email address, ensure that at the top of the file the following line is added.

MAILTO=”your@emailaddress.com”

Note, make sure to change “your@emailaddress.com” to your email address within the double quotes, for example “pascal@hivelocity.net”.

Common cron job time structures

The listed items below can assist with the most common times used to run various tasks.

  • * * * * * – This runs the command every minute.
  • 0 * * * * – This runs the command every hour.
  • 0 0 * * * – This runs the command every day at midnight.
  • 0 0 * * 0 – This runs the command every Sunday at midnight.
  • 0 0 1 * * – This runs the command on the first day of every month at midnight.

As well as the following,

  • @reboot – Run once, at startup.
  • @yearly – Run once a year
  • @annually – (same as @yearly)
  • @monthly – Run once a month
  • @weekly – Run once a week
  • @daily – Run once a day
  • @midnight – (same as @daily)
  • @hourly – Run once an hour

Useful Resources

Creating a task can be complicated at first, so to make things easier, there are multiple sources online that will generate crontab lines based on your needs.

Two common generators are listed below.

Leave a Comment