English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Linux crontab is a command used to execute programs regularly.
After the operating system is installed, this task scheduling command will be started by default.
The crond command checks regularly every minute to see if there are any tasks to be executed, and if there are, it will automatically execute the task.
Hinweis:Newly created cron tasks will not be executed immediately, but at least after 2 minutes can be executed, of course, you can restart cron to execute it immediately.
The main work of Linux task scheduling is divided into the following two categories:
crontab [ -u user ] file
or
crontab [ -u user ] { -l | -r | -e }
Description:
crontab is used to allow users to execute programs at fixed times or intervals, in other words, it is similar to the user's schedule.
-u user refers to setting the schedule for a specified user, which requires that you have the necessary permissions (such as root) to specify someone else's schedule. If not -u user means setting your own schedule.
Parameter description:
l : Listet den aktuellen Zeitplan auf
Zeitformat wie folgt:1 Zeitformat wie folgt:2 Zeitformat wie folgt:3 Zeitformat wie folgt:4 Zeitformat wie folgt:5 f
* * * * * - - - - - | | | | | | | | | +----- Woche des Monats (0 - 6) (Sonntag ist 0) | | | +---------- 月份 (1 - 12) | | +--------------- 一个月中的第几天 (1 - 31) | +-------------------- 小时 (0 - 23) +------------------------- 分钟 (0 - 59)
用户也可以将所有的设定先存放在文件中,用 crontab file 的方式来设定执行时间。
每一分钟执行一次 /bin/ls:
* * * * * /bin/ls
在 12 月内, 每天的早上 6 点到 12 点,每隔 3 个小时 0 分钟执行一次 /usr/bin/backup:
0 6-12/3 * 12 * /usr/bin/backup
周一到周五每天下午 5:00 发一封信给 [email protected]:
0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata
每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha":
20 0-23/2 * * * echo "haha"
下面再看看几个具体的例子:
0 */2 * * * /sbin/service httpd restart - 意思是每两个小时重启一次apache 50 7 * * * /sbin/service sshd start - 意思是每天7:50 开启ssh服务 50 22 * * * /sbin/service sshd stop - 意思是每天22:50 关闭ssh服务 0 0 1,15 * * fsck /home - 每月1号和15号检查/home 磁盘 1 * * * * /home/bruce/backup - 每小时的第一分钟执行 /home/bruce/backup diese Datei 00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \; von Montag bis Freitag3Uhrzeit, im Verzeichnis/in home, Datei mit dem Namen*Dateien mit .xxx löschen4Tages zurück. 30 6 */10 * * ls bedeutet die Dateien eines bestimmten Monats1、11、21、31Tag ist6:30 Führe den ls-Befehl einmal aus
Hinweis:Wenn das Programm nach der von Ihnen angegebenen Zeit ausgeführt wird, sendet das System eine E-Mail an den aktuellen Benutzer, die das Ausführungscontent des Programms anzeigt. Wenn Sie keine solche E-Mail erhalten möchten, fügen Sie nach jeder Leerzeile einen > hinzu. /dev/null 2>&1 kann, z.B.:
20 03 * * * . /etc/profile;/bin/sh /var/www/w3codebox/test.sh > /dev/null 2>&1
Wenn wir crontab verwenden, um Skripte zu planen, können sie nicht ausgeführt werden, aber wenn sie direkt über den Befehl (z.B.:./test.sh) kann wieder normal ausgeführt werden, was hauptsächlich darauf zurückzuführen ist, dass die Umgebungsvariablen nicht gelesen werden können.
Lösungsmethode:
1、Alle Befehle müssen in Form von absoluten Pfaden geschrieben werden, z.B.: /usr/local/bin/docker。
2、Verwenden Sie folgenden Code am Anfang von Shell-Skripten:
#!/bin/sh . /etc/profile . ~/.bash_profile
3、in /etc/crontab Um Umgebungsvariablen hinzuzufügen, fügen Sie vor den ausführbaren Befehlen den Befehl . hinzu. /etc/profile;/bin/sh, um Umgebungsvariablen zu aktivieren, z.B.:
20 03 * * * . /etc/profile;/bin/sh /var/www/w3codebox/test.sh