解决crontab 定时任务加载失败

在CentOS7系统中,由于SELinux的限制,crontab任务未能按预期执行。系统日志显示UnauthorizedSELinuxcontext相关错误。为了解决这个问题,需要执行`chcon -R -u system_u -r object_r -t system_cron_spool_t /etc/crontab`命令,并重启crond服务。通过这个方法,可以确保crontab任务在SELinux环境下正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 computer's 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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值