Basic Crontab
Crontab is used to run a command or script that has been scheduled at a certain time. Crontab can be used to run tasks that require periodic intervals, examples of time synchronization or backups, so the admin does not have to do it manually. Every user on linux has their own crontab file.
See the ongoing crontab
To see the running crontab using the command
# crontab -l
Edit Cronjobs
To edit the cronjobs list use the command
# crontab -e
Then it will open the editor, in Debian 9 that I use, the default editor use nano.
Crontab writing format
* * * * * command to be executed
The meaning of its asterisks
# + ---------------- minute (0 - 59)
# | + -------------- hour (0 - 23)
# | | + ------------ day of month (1 - 31)
# | | | + ---------- month (1 - 12)
# | | | | + -------- day of week (0 - 6) (Sunday = 0 or 7)
# | | | | |
* * * * * command to be executed
Usage examples for time synchronization with the ntpdate command
Execution every minute:
* * * * * ntpdate 192.168.1.1
This means running the command in:
> every minute
> in every hour
> in the whole day of the month
> every month
> and every day of the week.
Execution every 10 minutes:
* / 10 * * * * ntpdate 192.168.1.1
Execution every 5 hours:
* * / 5 * * * ntpdate 192.168.1.1
Execution every Friday at 1:
0 1 * * 5 ntpdate 192.168.1.1
Execution every 8 am during weekdays (Monday - Friday):
0 8 * * 1-5 ntpdate 192.168.1.1
Example run backup every Saturday at 1:
0 1 * * 6 root /usr/bin/scp /var/lib/vz/dump/dump/vzdump-qemu-108* root@192.168.1.1:/var/lib/vz/vmbackup/dump/
Note: Cron only allows for a minimum of one minute, if you want to run your script/command on every second you need to create an example shell script like this:
#/bin/bash
while true;
do./your-script &
sleep 5;
done
See the ongoing crontab
To see the running crontab using the command
# crontab -l
Edit Cronjobs
To edit the cronjobs list use the command
# crontab -e
Then it will open the editor, in Debian 9 that I use, the default editor use nano.
Crontab writing format
* * * * * command to be executed
The meaning of its asterisks
# + ---------------- minute (0 - 59)
# | + -------------- hour (0 - 23)
# | | + ------------ day of month (1 - 31)
# | | | + ---------- month (1 - 12)
# | | | | + -------- day of week (0 - 6) (Sunday = 0 or 7)
# | | | | |
* * * * * command to be executed
Usage examples for time synchronization with the ntpdate command
Execution every minute:
* * * * * ntpdate 192.168.1.1
This means running the command in:
> every minute
> in every hour
> in the whole day of the month
> every month
> and every day of the week.
Execution every 10 minutes:
* / 10 * * * * ntpdate 192.168.1.1
Execution every 5 hours:
* * / 5 * * * ntpdate 192.168.1.1
Execution every Friday at 1:
0 1 * * 5 ntpdate 192.168.1.1
Execution every 8 am during weekdays (Monday - Friday):
0 8 * * 1-5 ntpdate 192.168.1.1
Example run backup every Saturday at 1:
0 1 * * 6 root /usr/bin/scp /var/lib/vz/dump/dump/vzdump-qemu-108* root@192.168.1.1:/var/lib/vz/vmbackup/dump/
Note: Cron only allows for a minimum of one minute, if you want to run your script/command on every second you need to create an example shell script like this:
#/bin/bash
while true;
do./your-script &
sleep 5;
done
Komentar