Using Cronjobs to run scheduled tasks

Cron is a service that lets you run scheduled tasks. These tasks are called cronjobs. If you have already followed the initial course you will have already used cron when you set up certbot.

What tasks would I want to schedule?

You can schedule anything! Some examples of what you might have done already include:

Some tasks that you might want to schedule may include:

And many more, anything you can do can be turned into a cronjob.

How do I schedule things?

You can schedule tasks in the following ways:

  1. Your users crontab
  2. System wide cron directories

Your user crontab

This the preferred method for personal tasks and scripts, it's also the easiest to get started with. Run the command crontab -e to access your users crontab

Once you have figured out the command you want to run you need to figure out how often you want to run it and when. I am going to schedule my system updates once a week on at 3:30 AM on a Monday.

We now have to convert this time (Every Monday at 3:30 AM) into a cron time. Cron uses a simple but effective way of scheduling when to run things.

Crontab expressions look like this * * * * * command-to-run

So for our Monday at 3:30AM job we would do the following:

 .---------------- minute (0 - 59)
 | .------------- hour (0 - 23)
 | | .---------- day of month (1 - 31)
 | | | .------- month (1 - 12
 | | | | .---- day of week (0 - 6) 
 | | | | |
 * * * * *
30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 
			



Lets add another Job, our backup job (for the purposes of this our backup command is just called backup) We want to run backup Every evening at 11PM, once we work out the timings for this we can add the to the same file as the above by running crontab -e This would mean our full crontab would look like this:

            30 3 * * 1 apt-get -y update && apt-get -y dist-upgrade 

            0 23 * * * backup 
            

System wide cron directories

Run the command ls /etc/cron* you should see a list of directories and there contents. The directories should be something like the below

The directories cron.{hourly,daily,weekly,monthly} are where you can put scripts to run at those times. You don't put normal cron entries here. I prefer to use these directories for system wide jobs that don't relate to an individual user. For example If we wanted to put our backup job in /etc/cron.daily we would do the following:

  1. Create and edit the file /etc/cron.daily/backup/ :
    • vim /etc/cron.daily/backup
  2. Add the backup command in there, we can also add some logging because it's a full script. The full thing will look like this:
  3.  #!/bin/sh
    
     # the logger command generates system logs
     logger "starting backup daily job"
     
     # backup is hypothetical backup command
     backup 
    
     logger "finished backing up"
                                

  4. Save that file
  5. Make that executable:

It's that easy, no messing around with cron timers! A downside of this is that you can't choose when these run, most of the time they will run at ~6:30am every hour,day,week,month.

That's it! There is not much more to know about cron. Hopefully you now know the appropriate cron method and have the ability to correctly schedule your tasks.

Contribution

- Mark McNally -- website, Youtube