Linux CronJobs - scheduled tasks | Debian crontab [explained]
Scheduled tasks can be started in Linux using crontab.
To set up the tasks, use the crontab -e command in the terminal.
Format of the crontab entries
m h dom mon dow command
* * * * * command
m h dom mon dow command
| | | | |
| | | | ----- day of the week (0 - 7) (Sunday is 0 and 7; 1: Monday; 2: Tuesday; .)
| | | |
| | | ------ month (1 - 12)
| | |
| | --------- Day (1 - 31)
| |
| ----------- Hour (0 - 23)
|
------------- Minute (0 - 59)
m
... Minute
h
... hour
mon
... month
dow
... Day of the week
command
... Shell command
A * means to each unit:
* * * every minute
1 2 * * hour 2, minute 1: thus 2:01 o'clock at night
1 2 3 * * hour 2, minute 1: day: 3 so always on the 3rd of the month 2:01 at night.
1 2 * * 3 every Wednesday at 2:01 a.m.
*/10 * * * every 10 minutes
start as root
crontab can be configured as current user or as root:
sudo crontab -e
Depending on which editor is installed, the vi editor or nano is usually used at this point. Especially for beginners the handling of vi takes some getting used to.
Examples
Execute when starting the PC
Here as an example additional entries in the iptables to block a network area:
@reboot sudo iptables -A INPUT -m iprange --src-range 192.168.1.100-192.168.1.253 -j DROP
@reboot always executes the command when the PC boots.
Linux version: uname -a:
Linux soxn 4.17.0-0.bpo.1-amd64 #1 SMP Debian 4.17.8-1~bpo9+1 (2018-07-23) x86_64 GNU/Linux
Starting a bash script
Repeatedly starting a script: here every minute:
* * * * * . /var/bashscript.sh > /dev/null 2>&1
(tested Linux version: uname -a
Linux raspberrypi 4.14.76-v7+ #1150 SMP Mon Oct 15 15:19:23 BST 2018 armv7l GNU/Linux)
restart a service
5 19 * * * sudo service hostapd restart
(tested Linux version: uname -a
Linux raspberrypi 4.14.76-v7+ #1150 SMP Mon Oct 15 15:19:23 BST 2018 armv7l GNU/Linux)
restart the server: reboot
as an example reboot at 3am, daily:
<span class="hljs-number">0</span> <span class="hljs-number">3</span> * * * <span class="hljs-regexp">/sbin/</span>shutdown -r now<br>
alternatively:
1 5 * * * sudo reboot
(tested Linux version: uname -a
Linux raspberrypi 4.14.76-v7+ #1150 SMP Mon Oct 15 15:19:23 BST 2018 armv7l GNU/Linux)
For details on the reboot / shutdown command, see: Linux reboot
every 10 minutes
*/10 * * * * . /nextcloud/check.sh > /dev/null 2>&1
(tested Linux version : uname -a:
Linux soxn 4.17.0-0.bpo.1-amd64 #1 SMP Debian 4.17.8-1~bpo9+1 (2018-07-23) x86_64 GNU/Linux)
as another user
crontab -e
0 2 * * * username /var/script.sh
sudo crontab -e
0 2 * * * su username -c "/var/script.sh"
Launching a Docker command
The host operating system can trigger jobs in the container using crontab:
0 3 * * * docker exec db mysqldump --user=root --password=password -h localhost nextcloud > /nextcloud/nextcloud/db/dump.sql
Here as an example: every day at 3h in the morning
(tested Linux version: uname -a:
Linux soxn 4.17.0-0.bpo.1-amd64 #1 SMP Debian 4.17.8-1~bpo9+1 (2018-07-23) x86_64 GNU/Linux)
Multiple commands in one line
Multiple commands in one line can be easily separated with a semicolon: ;.
if then in cron
From Sunday to Friday 3:01, if the btrfs device stats /data command contains an error mail it, delete it, and fix it using scrub:
1 3 * * 0,1,2,3,4,5 if [ "$(btrfs device stats /daten | grep -vE ' 0$')" != "" ];then btrfs device stats -z /daten | mail -s "Error: BTRFS Device Status" Mail@meineMail.Adresse;btrfs scrub start /daten;fi
(tested Linux version: uname -a:
Linux soxn 4.17.0-0.bpo.1-amd64 #1 SMP Debian 4.17.8-1~bpo9+1 (2018-07-23) x86_64 GNU/Linux)
Search for a text in an output and execute a command if it occurs.
* * * * * iw dev wlan1 info | grep -q 'managed' && sudo service hostapd restart
(tested version: uname -a:
Linux raspberrypi 4.14.76-v7+ #1150 SMP Mon Oct 15 15:19:23 BST 2018 armv7l GNU/Linux)
For details on "grep", see: Linux Search text in files: grep
BTRFS check and data maintenance
Mail the disk status, weekly Sunday 22:01PM:
1 22 * * 0 btrfs device stats -z /daten | mail -s "BTRFS Device Status" Mail@meineMail.Adresse
Scrub start weekly, Sunday at 22:03:
3 22 * * 0 btrfs scrub start /daten
again send the status by mail: 6:15 Monday morning:
15 6 * * 1 btrfs scrub status /daten | mail -s "BTRFS Scrub Status" Mail@meineMail.Adresse
Monday morning: 6:16 am: set the error counter to 0:
16 6 * * 1 btrfs device stats -z /daten | mail -s "BTRFS Device Status" Mail@meineMail.Adresse
(tested Linux version : uname -a:
Linux soxn 4.17.0-0.bpo.1-amd64 #1 SMP Debian 4.17.8-1~bpo9+1 (2018-07-23) x86_64 GNU/Linux)
{{percentage}} % positive