crontab 用来任务定时调度,在 Linux 下可以通过创建文件 /etc/cron.allow 或者

/etc/cron.deny

来控制权限,如果 /etc/cron.allow 文件存在,那么只有这个文件中列出的用户可以使用 cron, 同时

/etc/cron.deny文件被忽略; 如果 /etc/cron.allow 文件不存在,那么文件 /cron.deny

中列出的用户

将不能用使用 cron。 下面简单演示下限制用户使用 crontab 命令的情况。

--1 修改文件

/etc/cron.deny[root@pgb etc]# ll

/etc/cron.deny

-rw-r--r-- 1 root root 10 Feb 12 21:52 /etc/cron.deny

备注:

添加要限制的用户,只需要写入用户名即可。

--2 /etc/cron.deny

文件内容[root@pgb etc]# cat /etc/cron.deny

postgres

--3 重启 crond

服务[root@pgb cron]# service crond status

crond (pid 3834) is running...

[root@pgb etc]# service crond restart

Stopping crond: [ OK ]

Starting crond: [ OK ]

--4 切换到 postgres

用户测试[root@pgb ~]# su - postgres

[postgres@pgb ~]$ crontab -l

You (postgres) are not allowed to use

this program (crontab)See crontab(1) for more

information

备注:切换到 postgres 用户后,就不能使用

crontab 命令了。

--5 /var/log/cron

日志信息Feb 12 21:46:25 pgb crontab[6209]:

(postgres) AUTH (crontab command not allowed)

--6 crontab

内容存放目录[root@pgb etc]# ll

/var/spool/cron

total 8

-rw------- 1 postgres root 15 Feb 12 21:52

postgres

-rw------- 1

root root 111 Feb 12 15:18 root

--7 root 用户的

crontab[root@pgb etc]# crontab -l

###ntpdate

5 * * * * /usr/sbin/ntpdate asia.pool.ntp.org ;/sbin/hwclock

--systohc >> /root/sync_date.log

2>&1

[root@pgb etc]# cat

/var/spool/cron/root###ntpdate

5 * * * * /usr/sbin/ntpdate asia.pool.ntp.org ;/sbin/hwclock

--systohc >> /root/sync_date.log

2>&1

[root@pgb etc]#

--8

其它说明 如果用户创建 crontab 任务,在脚本中应指定 SHELL,PATH,HOME 变量, 否则很容易出现脚本,命令找

不到的情况。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐