/etc/crontab文件和crontab -e命令区别
1、格式不同
前者
# 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
后者
#50 1 * * * command
2、使用范围
修改/etc/crontab这种方法只有root用户能用,这种方法更加方便与直接,直接给其他用户设置计划任务,而且还可以指定执行shell等等,
crontab -e这种所有用户都可以使用,普通用户也只能为自己设置计划任务。然后自动写入/var/spool/cron/usename
1、crontab 会进行语法检查,vi 不会
2、听说有些 os 的 crond 不会重读配置,所以用 service 重启
crontab -e是某个用户的周期计划任务;
/etc/crontab是系统的周期任务
1. crontab -e 与 /etc/crontab 修改语法格式不一样,后者多一个 user 指定
2. 不管用 crontab -e or 改 /etc/crontab 都不需要重新启动 crond 服务
01 22 * * * /sbin/shutdown -h now
执行的结果都是一样, 一般LINUX 都用shutdown -h now 也可以init 0,init 0在UNIX用得比较多,关机需要root的身份在可以执行:
方法1:换到root 的身份,su - root,crontab -e (按a增加下),01 22 * * * /sbin/shutdown -h now (或01 22 * * * /sbin/init 0),保存退出即可
方法2:vi /etc/crontab 在里面插入,01 22 * * * root /sbin/shutdown -h now或 01 22 * * * root /sbin/init 0
crontab -e 是针对用户的cron来设计的,如果是系统的例行性任务,则不需要以crontab -e来管理例行性命令,只需要编辑/etc/crontab文件就可以了。需要注意的是:crontab -e的作用其实是/usr/bin/crontab这个执行文件,但是/etc/crontab是个纯文本文件,可以root的身份编辑这个文件。
基本上,cron服务的最低检测时间单位是分钟,所以cron会每分钟读取一次/etc/crontab与/var/spool/cron中的数据内容,因此,只要编辑完/etc/crontab文件并且保存之后,crontab时设定就会自动执行。
注意:在linux下的crontab会自动每分钟重新读取一次/etc/crontab的例行工作事项,但是某些原因或在其他的unix系统中,由于crontab是读到内存中,所以在您修改完/etc/crontab之后可能并不会马上执行,这时请重新启动crond服务:/etc/rc.d/init.d/crond restart
下面看看/etc/crontab文件的内容:
vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root nice -n 19 run-parts /etc/cron.hourly
02 4 * * * root nice -n 19 run-parts /etc/cron.daily
22 4 * * * root nice -n 19 run-parts /etc/cron.weekly
42 4 1 * * root nice -n 19 run-parts /etc/cron.monthly