crontab -e
will start an editor to edit the crontab entries. But how to add a crontab entry from a shell script on Linux without interaction from users?
crontab -e
将启动编辑器来编辑crontab条目。 但是如何在Linux上的shell脚本中添加crontab条目而又无需用户交互?
You can try this piece of script:
您可以尝试以下脚本:
(crontab -l; echo "@reboot echo "rebooted"";) | crontab -
Note that the update by this line of script is not atomic. If some other programs edit the crontab files between the first and second invokes of crontab
, the edits will be lost. Hence, make sure there is not other programs/admins editing the crontab when you use this piece of script.
请注意,此脚本行的更新不是原子的。 如果其他一些程序编辑crontab 文件的第一和第二所调用之间crontab
中,编辑将丢失。 因此,使用此脚本时,请确保没有其他程序/管理员在编辑crontab。
翻译自: https://www.systutorials.com/how-to-add-a-crontab-entry-from-a-shell-script-on-linux/