How can I create a crontab in a Shell script

冷暖自知 提交于 2020-01-25 09:03:29

问题


I have to automate my Crontab script and would like to insert some things in my Crontab file, I want to do that without interactive query and it should look like command:

crontab_admin.sh -add -s "15 9 * * *" -c "check_backup_include_list.sh" -u "USERNAME" -t "CRQ000000000000"

crontab_admin.sh -remove -s "15 9 * * *" -c "check_backup_include_list.sh" -u "USERNAME" -t "CRQ000000000000"

and it should look like this in crontab afterwards:

15 9 * * * $HOME/scripts/check_backup_include_list.sh

sry for my bad english


回答1:


Its not wise to operate directly with cron file. But you can add record with script like this:

crontab -l >/tmp/c1
echo '15 9 * * * /full/path/to/scripts/check_backup_include_list.sh' >>/tmp/c1
crontab /tmp/c1

If you want to remove particular record you can do for example with something like this:

crontab -l >/tmp/c1
grep -v check_backup_include_list.sh' /tmp/c1 > /tmp/c2
crontab /tmp/c2


来源:https://stackoverflow.com/questions/58873399/how-can-i-create-a-crontab-in-a-shell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!