解决crontab 定时任务加载失败

0x0

    系统:centos7 内核:3.10.0-1160.15.2.el7.x86_64

今天用crontab创建定时任务,在测试时发现任务并没有按预期执行, 使用systemctl status crond 查看crond的状态

> # systemctl status crond                                                                                        
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2021-03-08 09:25:57 CST; 7min ago
 Main PID: 1445 (crond)
    Tasks: 1
   CGroup: /system.slice/crond.service
           └─1445 /usr/sbin/crond -n

3月 08 09:25:57 A-boat systemd[1]: Started Command Scheduler.
3月 08 09:25:57 A-boat crond[1445]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 82% if used.)
3月 08 09:25:57 A-boat crond[1445]: ((null)) Unauthorized SELinux context=system_u:system_r:system_cronjob...ntab)
3月 08 09:25:57 A-boat crond[1445]: (root) FAILED (loading cron table)
3月 08 09:25:58 A-boat crond[1445]: (CRON) INFO (running with inotify support)
3月 08 09:25:58 A-boat crond[1445]: (CRON) INFO (@reboot jobs will be run at computers startup.)
3月 08 09:30:01 A-boat crond[1445]: (*system*) RELOAD (/etc/crontab)
3月 08 09:30:01 A-boat crond[1445]: ((null)) Unauthorized SELinux context=system_u:system_r:system_cronjob...ntab)
3月 08 09:30:01 A-boat crond[1445]: (root) FAILED (loading cron table)
Hint: Some lines were ellipsized, use -l to show in full.

可以看见,有一条和SElinux相关的信息:((null)) Unauthorized SELinux context=system_u:system_r:system_cronjob…ntab) 查询相关资料后发现是被SELinux阻止了

解决方案

执行以下命令

chcon -R -usystem_u -robject_r -tsystem_cron_spool_t /etc/crontab
systemctl restart crond
经验分享 程序员 微信小程序 职场和发展