作为 root,我如何在其他用户的 crontab 中创建一个条目

spo*_*rty 3 shell bash cron

我正在尝试在 shell 脚本中使用以下命令.. 任何建议以正确的方式执行此操作?

[root@testserver ~]# crontab -u oracle -e >> 0 0 * * * /usr/local/scrips/setup.sh
crontab: usage error: no arguments permitted after this 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
Run Code Online (Sandbox Code Playgroud)

bin*_*rym 5

-e开关将使 crontab 具有交互性,这不是所希望的行为。

我建议你使用crontab -u user file语法。下面是一个例子:

root@c:~# crontab -l -u user
no crontab for user
root@c:~# echo "10 10 * * * /bin/true" >> to_install
root@c:~# crontab -u user to_install
root@c:~# crontab -l -u user
10 10 * * * /bin/true
root@c:~# crontab -l -u user > temp
root@c:~# echo "12 12 * * * /bin/false" >> temp
root@c:~# crontab -u user temp
root@c:~# crontab -l -u user
10 10 * * * /bin/true
12 12 * * * /bin/false
Run Code Online (Sandbox Code Playgroud)

  • 列出 crontab 是关键,因为 `crontab ... file` 用给定文件的内容替换了整个 crontab。 (3认同)