The software utility cron also known as cron job is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.
10 8 * * 1-5 path/to/script
|__|___|___|__________________ Day in month
|__________________ Script to execute
A cronjob consists of six parts:
Notice; in the fifth are both 0 and 7 Sundays.
- The first is Minutes (0-59)
- The second is Hours (0-23)
- The third is the Day in the Month (1-31)
- The fourth is the Month (1-12)
- The fifth is the weekday (0-7)
- The sixth is the Path to the Script to Run
Now we will make a cronjob to run the scripts. That way we can totally automate the backup procedure on our machine.
Here is the backup-script:
pax -wzf about_cron_backup.pax .
cp about_cron_backup.pax /home/you/backup/projects/about_cron_backup.pax
And now about Cron:
15 * * * * /home/you/scripts/run_cronjob_15.sh
Here is shown how a cronjob looks if we want to execute our script every hour, 15 minutes past the hour.
15 8 * * 1-5 /home/you/scripts/run_cronjob_0815.sh
Here we execute a cronjob every workday in the week, at 15 minutes past 8 (AM).
Okay. So now we have the informations that we need for building our automated backup script. I expect to use the script as seen above.
And that we use the second example on cronjob, also as seen above.
Now we just need to write the job into crontab.
That we can do easily. Write following into your terminal prompt:
$ crontab -e
This will open Nano, or some other texteditor, with the conjob configurationfile. I have made two screenshots of the Nano editor, you can see them here:
As you can see, on the bottom line in the second screenshot; is our job in the crontab.
From now on your system will create a backup every workday at 08:15.
When you have written th eline into Nano, you end by hitting; CTRL-X. Nano will ask if you wan't to save the file. Hit Y (yes), and then ENTER.
Your cronjob should now have been added. You can check it by writing this:
Remember to see this:
Here are some completely different ways at how you can use cron.
As you can see is @reboot ment to be run at every startup of the machine. It is a little special in the way that it checks for scripts to run at every boot. The others can be exchanged by the "ordinary" cronjobs, as you can see following every explanation (meaning).
So, f.ex can the daily (@daily) be exchanged with; "0 0 * * *".
Or you can change this execution (by midnight) with f.ex “0 12 * * * ” where cron will run your script at midday instead.
Here follows a n example on use of @reboot, and thereafter the other special crontab settings.
@reboot sh path/to/file.sh
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".