To support scheduling of commands or scripts (an automated series of instructions carried out in a specific order), Unix-like systems provide a utility called cron (or the crond daemon). This utility enables the time-based running of Linux tasks or jobs.
Put simply, a job or task is a command or program, or script that your wish to execute to achieve a certain goal. A job that you schedule via cron is known as a cronjob. Cron uses what is called a crontab to store and read cronjobs.
[ You might also like: How to Run a Command Periodically in Linux using Watch ]
In this article, we will show how to run a crontab job every week on Sunday on a Linux system.
Display and View Cron Jobs
Each user on a Linux system can have their crontab. To open the crontab for the currently logged-on user, run the crontab command with the
-e (for edit) or
-l (listing) flag, as follows. If you are running this command for the very first time, it will prompt you to select the editor you will use for editing your crontab:
$ crontab -e OR $ crontab -l
To open the crontab for the root user as another user, use the sudo command. Not that the user invoking sudo must have privileges to do so:
$ sudo crontab -u root -l OR $ sudo crontab -u username -l
Schedule a Crontab Job Every Week on Sunday Midnight
To schedule a cronjob in a crontab, you have to follow the following format:
# ┌───────────── minute (0 - 59) # │ ┌───────────── hour (0 - 23) # │ │ ┌───────────── day of the month (1 - 31) # │ │ │ ┌───────────── month (1 - 12) # │ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday; # │ │ │ │ │ 7 is also Sunday on some systems) # │ │ │ │ │ # │ │ │ │ │ # * * * * * <command to execute>
So, following the above format, to schedule a cronjob to run every week on Sunday at midnight 12 am open the crontab file.
$ sudo crontab -e
enter the following entry in the crontab:
0 0 * * 0 /path_to_command OR 0 0 * * 7 /path_to_command OR 0 0 * * Sun /path_to_command
Save the changes and exit.
To display the contents of your current crontab, use the
-l flag as shown.
$ crontab -l
Note that to easily and quickly master cron schedule expressions, we recommend using a tool such as crontab.guru, an interactive online tool for scheduling cronjobs.
That’s it for now! Cron is a lifesaver for Linux system administrators and/or developers and other users, for scheduling repetitive tasks. If your want to find out more about it, read the manual pages of crontab and cron.