A crontab file is a simple text file containing a list of commands meant to be run at specified times. Each user (including root) has one. It is commonly edited in a terminal using the crontab command.
The commands in the crontab file (and their run times) are checked by the cron daemon, whether the user is actually logged into the system or not, and executed at the indicated time in the system background.
MX Linux uses anacron as well, giving you cron.daily, cron.monthly, cron.hourly, and cron.weekly folders under /etc/. Anacron is scheduled by cron, but using anacron has the advantage that if an app missed a schedule, it will run at the next opportunity instead of waiting for the next scheduled time. It is especially intended for laptop users and for people who only turn their computer on for a few hours a day.
Each job is recorded in /etc/crontab on a single line that contains values in a specific order:
[Minute] [Hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
So when you see a job like this:
0 2 * * * [path to script]
You would understand that whatever is in that script will be executed
- at the zero minute
- of the second hour
- of every day
- of every month
- on every weekday
Note: 1) asterisk is interpreted as “every;” 2) zero is interpreted as “at the beginning” for [Minute] and [Hour], but as “Sunday” in [Day of week].
Minute 0 – 59
Hour 0 – 23
Day of month 1 – 31
Month of year 1 – 12
Day of week 0 – 7
These special symbols can be used as well:
|*||= “every one”|
|,||used to give multiple values|
|–||Indicate a range between two numbers|
|/||Specify a periodicity/frequency using a slash|
Cron jobs are used on servers for many jobs, and examples can be found in a websearch. For individual users, here is a common use. NOTE: if you want to schedule a job for a time when the machine is usually not running, anacron would be the better choice–consult Links, below.
- Create a backup script (see below) and make it executable. Let’s call it “work-backup.sh” and save it in a Scripts folder in the Home directory of your user name
- Decide when you want that script to run. Let’s say at the end of every workday.
- In a terminal, enter “crontab -i” as regular user
- Enter this line: 0 17 * * 1-5 home/username/Scripts/work-backup.sh
- Now your backup script will run at 5 pm/17:00 every Monday through Friday
This simple backup script was created by user kmathern. This uses three different external drives, so adjust the number and label of drives to your own situation before using.
# This Rdiff-Backup script backs up to a second hard drive
# It must be run as root in order to mount the second hard drive
(( EUID != 0 )) && exec sudo -- "$0" "[email protected]"
# To restore files, issue the command: cp -a /mnt/sda1/username /home
# To restore, but not overwrite:
# cp -a --no-clobber /mnt/sda1/username /home
# Mount the external devices
# Execute the backup
rdiff-backup /home/newbie/Documents /mnt/sdb2/Documents
rdiff-backup /home/newbie/Music /mnt/sdb1/Music
rdiff-backup /home/newbie/Mail /mnt/sdb2/Mail
rdiff-backup /home/newbie/Pictures /mnt/sdb3/Pictures
# Unmount the external devices
- Beginner’s guide. Excellent overview.
- Online crontab generator. Generates the line to paste in crontab, very handy
- Anacron use. Cron for ordinary mortals. How-to for laptops and desktops that are not running all the time the way that servers are.