crontab是linux系统下的任务服务,可以实现计划任务定时执行操作。这里以centos7系统为例演示,如何重启crontab服务。
[root@CentOS7 ~]# vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
1.查询确认版本,这里是CentOS 7系统。
[root@CentOS7 ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
2.关于crontab的使用方法,可以通过man crontab查看详细说明,crontab –help 可以查看使用参数
[root@CentOS7 ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Options:
-u <user> define user
-e edit user's crontab
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-x <mask> enable debugging
Default operation is replace, per 1003.2
3. 编辑crontab设置计划任务,最常用的是crontab -e编辑
[root@CentOS7 ~]# crontab -e
30 * * * * /bin/sh /tmp/echo.sh
4. 关于crontab的任务格式,可以参考下图范例。 主要是 时间 执行人 命令
5.如何重启crontab
linux下 crontab服务对应的是crond服务。可以通过service crond restart重启
[root@CentOS7 ~]# service crond restart
Redirecting to /bin/systemctl restart crond.service
6.centos7系统下服务可以通过systemctl来重启,systemctl restart crond
[root@CentOS7 ~]# systemctl restart crond
[root@CentOS7 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2020-08-26 09:06:42 CST; 30s ago
Main PID: 1808 (crond)
CGroup: /system.slice/crond.service
└─1808 /usr/sbin/crond -n
Aug 26 09:06:42 CentOS7 systemd[1]: Started Command Scheduler.
Aug 26 09:06:42 CentOS7 crond[1808]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 89% if used.)
Aug 26 09:06:42 CentOS7 crond[1808]: (CRON) INFO (running with inotify support)
Aug 26 09:06:42 CentOS7 crond[1808]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
[root@CentOS7 ~]#
7.关于crontab的编辑方式,除了crontab -e之外。还可以通过直接编辑/etc/crontab 的配置文件来实现
[root@CentOS7 ~]# vi /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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
30 * * * * /bin/sh /tmp/echo.sh
8.针对用户的可以编辑 /var/spool/cron/root(用户)
[root@CentOS7 ~]# vi /var/spool/cron/root
30 * * * * /bin/sh /tmp/echo.sh
9.当然还有一个系统的定时任务目录,/etc/下的 cron.daily
[root@CentOS7 ~]# cd /etc/cron.daily/
[root@CentOS7 cron.daily]# pwd
/etc/cron.daily
[root@CentOS7 cron.daily]# cat logrotate
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
[root@CentOS7 cron.daily]#