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)

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-07-20 from Bernhard | Übersetzung Deutsch |🔔 | Comments:0

Linux Search text in files: grep | Linux | Linux Command: Restart: Reboot or Shutdown

Top articles in this section


Fan control Linux Debian: Fancontrol

After I installed a fan in my NAS, it always ran at top speed. In the BIOS I could set the speed, but the automatic mode did not work properly. Using the service fancontrol the speed of the fan can be linked to any sensor and any threshold and controlled automatically.


Linux folder used space - Bash TreeSize

To find out which folder needs how much space, the command “du” can be used on Debian. For an even better overview there is the commandline tool ncdu, which can analyze all folders similar to TreeSize.


Debian or Ubuntu kernel update or change- current kernel

The currently available Linux kernel has the version: 6.8.2 (found: 2024-03-27). Source: www.kernel.org. The Linux distributions, as an example Debian or Ubuntu used new kernel versions only with some delay, accordingly the used kernel versions are usually somewhat older. Which kernel is active on a Linux system can be read out with the following command:

Questions / Comments


By continuing to browse the site, you agree to our use of cookies. More Details