Home >System Tutorial >LINUX >Scheduled tasks under Linux--crontab

Scheduled tasks under Linux--crontab

PHPz
PHPzOriginal
2024-06-07 20:24:401074browse
1. cron service

Cron is a scheduled execution tool under Linux that can run jobs without manual intervention.

service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查看服务状态
2. Cron configuration file:

/var/spool/cron/ This directory stores the crontab tasks of each user including root. Each task is named after the creator. For example, the file corresponding to the crontab task created by tom is /var/spool/ cron/tom. Generally, a user can only have one crontab file at most.

3. /etc/crontab This file is responsible for arranging the crontab for system maintenance and other tasks specified by the system administrator.
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

MAILTO=root: It means that when an error occurs in the routine command in the /etc/crontab file, to whom will the error message or the message displayed on the screen be sent? Since root cannot receive emails from the client, I usually change this e-mail to my own account so that I can know the status of the system at any time!

01 * * * * root run-parts /etc/cron.hourly: In the commands after the #run-parts line, we can find that the five numbers are followed by root, and this line represents "Execute The level is root identity. Of course, you can also change this line to other identities! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)". That is to say, at 01 minutes of every hour, the system will log in as root. Go to the directory /etc/cron.hourly to execute all executable files! The next three lines have similar meanings! You can go to /etc/ and take a look. The system has these 4 directories preset! You can write the commands you need to execute every day directly to /etc/cron.daily, and you don’t need to use the crontab -e program!

4. /etc/cron.d/ This directory is used to store any crontab files or scripts to be executed.

Scheduled tasks under Linux--crontab

1. MAILTO=root: It means that when an error occurs in the routine command in the /etc/crontab file, to whom will the error message or the message displayed on the screen be sent? Since root cannot receive emails from the client, I usually change this e-mail to my own account so that I can know the status of the system at any time!

2. 01 * * * * root run-parts /etc/cron.hourly: In the commands after the #run-parts line, we can find that the five numbers are followed by root , this line represents "The execution level is root identity". Of course, you can also change this line to other identities! And run-parts means that the following /etc/cron.hourly is "all executable files in a directory (/etc/cron.hourly)", that is to say, at 01 minutes every hour, the system will log in as root Go to the directory /etc/cron.hourly to execute all executable files! The next three lines have similar meanings! You can go to /etc/ and take a look. The system has these 4 directories preset! You can write the commands you need to execute every day directly to /etc/cron.daily, and you don’t need to use the crontab -e program!

5. Permissions

crontab权限问题到/var/adm/cron/下一看,文件cron.allow和cron.deny是否存在
用法如下:
1、如果两个文件都不存在,则只有root用户才能使用crontab命令。
2、如果cron.allow存在但cron.deny不存在,则只有列在cron.allow文件里的用户才能使用crontab命令,如果root用户也不在里面,则root用户也不能使用crontab。
3、如果cron.allow不存在, cron.deny存在,则只有列在cron.deny文件里面的用户不能使用crontab命令,其它用户都能使用。
4、如果两个文件都存在,则列在cron.allow文件中而且没有列在cron.deny中的用户可以使用crontab,如果两个文件中都有同一个用户,以cron.allow文件里面是否有该用户为准,如果cron.allow中有该用户,则可以使用crontab命令。

AIX 中 普通用户默认都有 crontab 权限,如果要限制用户使用 crontab ,就需要编辑/var/adm/cron/cron.deny
HP-UNIX 中默认普通用户没得crontab 权限 ,要想放开普通用户的crontab 权限可以编

六、创建cron脚本

第一步:写cron脚本文件,命名为crontest.cron。

15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt 表示,每隔15分钟,执行打印一次命令

第二步:添加定时任务。执行命令 “crontab crontest.cron”。搞定
第三步:"crontab -l" 查看定时任务是否成功或者检测/var/spool/cron下是否生成对应cron脚本

注意:这操作是直接替换该用户下的crontab,而不是新增

七、crontab用法

crontab命令用于安装、删除或者列出用于驱动cron后台进程的表格。用户把需要执行的命令序列放到crontab文件中以获得执行。
每个用户都可以有自己的crontab文件。/var/spool/cron下的crontab文件不可以直接创建或者直接修改。该crontab文件是通过crontab命令创建的

在crontab文件中如何输入需要执行的命令和时间。该文件中每行都包括六个域,其中前五个域是指定命令被执行的时间,最后一个域是要被执行的命令。
每个域之间使用空格或者制表符分隔。格式如下:

minute hour day-of-month month-of-year day-of-week commands
合法值 00-59 00-23 01-31 01-12 0-6 (0 is sunday)

Scheduled tasks under Linux--crontab
除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。

-l 在标准输出上显示当前的crontab。
-r 删除当前的crontab文件。
-e 使用VISUAL或者EDITOR环境变量所指的编辑器编辑当前的crontab文件。当结束编辑离开时,编辑后的文件将自动安装。

八、例子:

每天早上6点

0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。

每两个小时

0 */2 * * * echo "Have a break now." >> /tmp/test.txt

晚上11点到早上8点之间每两个小时和早上八点

0 23-7/2,8 * * * echo "Have a good dream" >> /tmp/test.txt

每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点

0 11 4 * 1-3 command line

1月1日早上4点

0 4 1 1 * command line SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号 HOME=/

每小时执行/etc/cron.hourly内的脚本

01 * * * * root run-parts /etc/cron.hourly

每天执行/etc/cron.daily内的脚本

02 4 * * * root run-parts /etc/cron.daily

每星期执行/etc/cron.weekly内的脚本

22 4 * * 0 root run-parts /etc/cron.weekly

每月去执行/etc/cron.monthly内的脚本

42 4 1 * * root run-parts /etc/cron.monthly

注意: "run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名。

每天的下午4点、5点、6点的5 min、15 min、25 min、35 min、45 min、55 min时执行命令。

5,15,25,35,45,55 16,17,18 * * * command

每周一,三,五的下午3:00系统进入维护状态,重新启动系统。

00 15 * * 1,3,5 shutdown -r +5

每小时的10分,40分执行用户目录下的innd/bbslin这个指令:

10,40 * * * * innd/bbslink

每小时的1分执行用户目录下的bin/account这个指令:

1 * * * * bin/account

每天早晨三点二十分执行用户目录下如下所示的两个指令(每个指令以;分隔):

20 3 * * * (/bin/rm -f expire.ls logins.bad;bin/expire$#@62;expire.1st)

每年的一月和四月,4号到9号的3点12分和3点55分执行/bin/rm -f expire.1st这个指令,并把结果添加在mm.txt这个文件之后(mm.txt文件位于用户自己的目录位置)。

12,55 3 4-9 1,4 * /bin/rm -f expire.1st$#@62;$#@62;mm.txt

The above is the detailed content of Scheduled tasks under Linux--crontab. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn