Crontab -run this command at this time and this date Is used mostly in server machines with Linux OS, machines running 24 / 7. It is used to schedule a job daily / weekly / monthly / a specific date of month / a specific day of the week. Every [code]user on Linux system has its own cron job lists, without interfering with the other users.[/code]
[code]The cron command it is executed if the cron time match the current time. The asterisk (*) means “first – last ” or it include everything. The cron fields are:
[code]minute (0-59), hour (0-23), day of month (1-31), month (1-12 or days Mon, Tue etc ), day of week ( 0 – 7 )[/code]
[code]Please find attached the user cron jobs[/code]
[code]run at 9 am on weekdays, display the date and it write it to the home folder in date.txt document[/code]
[code]0 9 * * 1-5 date > ~/date.txt[/code]
[code]0 → 0 minutes
9 → 9 am in the morning
* → it run every day
* → it run every month
1 – 5 → it run from Monday to Friday [/code]
[code]run every working hour from 09:00 to 18:00 , every day[/code]
[code]00 09-18 * * * date > ~/date.txt[/code]
[code]0 → 0 minutes
09 – 18 → from 09:00, 10:00, 11:00 … 17:00, 18:00
* → it run every day of the month
* → it run every month
* – It run every day of the week [/code]
The cron system are a bit different from the user cron jobs. At the user cron we need to add also the user executing the job
[code]0 * * * * root date > ~/date.txt[/code]
This command it is used from the root user and run every hour.
[code]* * * * paul date > ~/date.txt[/code]
In this command the user paul it is executing the command and it will be write in paul’s home directory
The cron job run in a defined timezone, so every command, user command run according to the server timezone.
A cron job need to be setup in a single line, can not add a second one for the same job
In order to add a cron job we need to run the following command from terminal: “crontab -e”, usually it is used the vi editor for adding / deleting the cron job.
Also if we need to check the cron currently running we can check with “crontab -l”. In order to check cron for a specific user we need to login for that user.
Anacron it is mostly used for Desktops and Laptops, It does not require the system to be up 24 / 7. If you use cronjob and want to run a backup script at 12:00 am but the machines it is off than the script will not run and the backup script will not be executed, if you use anacron it will start once the machine it is up, and it will execute the backup script
It can be useful from machines that are OFF during the night or during the weekend, so the scripts will run once the machine it is ON.
[code]15 30 test.daily /bin/sh /home/user/backup-database.sh[/code]
In this example the backup database script will run every 15 days, if the system is not up than this script will be executed 30 minutes after the system power on. In this way we will have a database backup.
If the system will be ON than the script will run in the time range specified from the administrator, by default this time is 03:00 am – 10:00 pm.
If with cron the minimum granularity is minutes ( run every minute ), in anacron the minimum granularity is days ( run every day ).
If we see the anacron file content (sudo cat /etc/anacrontab ) you will see there is a specific user only ( root ), this means that anacron can be executed only from root user.
Anacron is ideal for desktop and laptops.
Systemd timer unit
[code]Systemd timer unit it is a good replacement for cron. It need to use a timer script and a service script.[/code]
The service script (date.service) it is used to specify the script, if should run periodically or once.
[code]Description=Prints date into /tmp/date file[/code]
[code]ExecStart=/usr/bin/sh -c ‘/usr/bin/date >> /tmp/date'[/code]
The timer script it is used for defining the time when the file need to run, it need to be in the same directory like the service script.
[code]Description=Run date.service every 10 minutes[/code]
After setup correctly the files we need to start the service and also put it on start up.
In order to check the list of timers enabled need to run the following command
“systemctl list-timers” for the current user or “systemctl list-timers –all” for all users
In order to enable a timer need to run the command “systemctl start date.timer”.
If we have a date.service we need to create also a date.timer file.
There are some options included in the timer file for better customization
[code]OnActiveSec defines a timer relative to the moment the timer itself is activated. [/code]
[code]OnBootSec defines a timer relative to when the machine was booted up. [/code]
[code]OnStartupSec defines a timer relative to when systemd was started. [/code]
[code]OnUnitActiveSec defines a timer relative to when the unit the timer is activating was last activated. [/code]
[code]OnUnitInactiveSec defines a timer relative to when the unit the timer is activating was last deactivated. [/code]