linux定时任务crond

[root@hecs-98663 ~]# rpm -qa | grep cron
cronie-anacron-1.4.11-24.el7_9.x86_64
crontabs-1.11-6.20121102git.el7.noarch
cronie-1.4.11-24.el7_9.x86_64
rpm -ivh 安装rpm软件
rpm -qa 查看软件是否安装
rpm -ql 查看软件详细信息s
rpm -qf 查看命令属于的安装包
rpm -e 卸载软件

检查crond服务是否运⾏

systemctl status crond.service #centos7
service crond status #centos6
Linux系统启动后,cron软件便会启动,对应的进程名字叫做crond,默认
是定期(每分钟检查⼀次)检查系统中是否有需要执⾏的任务计划,如果
有,则按计划进⾏,好⽐我们平时⽤的闹钟。
crond定时任务默认最快的频率是每分钟执⾏
若是需要以秒为单位的计划任务,则编写shell脚本更格式,crond不适⽤了
#秒级shell脚本
[root@pylinux tmp]# cat test_cron.sh
#!/bin/bash
while true
do
echo "111111"
sleep 1
done
#查看定时任务
[root@hecs-98663 ~]# ls /var/spool/cron/



[root@hecs-98663 ~]# ls -l /etc/ | grep cron*
-rw-------   1 root root      541 1月  14 2022 anacrontab
drwxr-xr-x.  2 root root     4096 2月  10 2022 cron.d
drwxr-xr-x.  2 root root     4096 6月  10 2014 cron.daily
-rw-------   1 root root        0 1月  14 2022 cron.deny
drwxr-xr-x.  2 root root     4096 2月  10 2022 cron.hourly
drwxr-xr-x.  2 root root     4096 6月  10 2014 cron.monthly
-rw-r--r--.  1 root root      451 6月  10 2014 crontab
drwxr-xr-x.  2 root root     4096 6月  10 2014 cron.weekly


##PATH路径很少,因此定时任务用绝对路径
[root@hecs-98663 ~]# cat /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

crontab命令

-l 查看
-e 编辑
-i 删除定时任务
-r 删除定时任务
-u user 指定任务执行任务,root可以管理普通用户计划任务

[root@hecs-98663 ~]# crontab -l
no crontab for root
[root@hecs-98663 ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@hecs-98663 ~]# crontab -l
* * * * * echo 123 > 123.txt

检查crond服务是否运行

[root@hecs-98663 ~]# systemctl is-active crond
active

时间案例

30 08 * * *每天上午8:30
00 00 * * *每天晚上12点

特殊符号

*     每
-     时间分割符
,     表示分隔时段
/n    n表示可以整除的数字
posted @ 2023-07-28 15:17  Bre-eZe  阅读(74)  评论(0)    收藏  举报