How do I create a crontab through a script

前端 未结 12 1052
再見小時候
再見小時候 2020-11-28 01:36

I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e but that will open an editor to edit the curr

12条回答
  •  爱一瞬间的悲伤
    2020-11-28 02:35

    For user crontabs (including root), you can do something like:

    crontab -l -u user | cat - filename | crontab -u user -
    

    where the file named "filename" contains items to append. You could also do text manipulation using sed or another tool in place of cat. You should use the crontab command instead of directly modifying the file.

    A similar operation would be:

    { crontab -l -u user; echo 'crontab spec'; } | crontab -u user -
    

    If you are modifying or creating system crontabs, those may be manipulated as you would ordinary text files. They are stored in the /etc/cron.d, /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly directories and in the files /etc/crontab and /etc/anacrontab.

提交回复
热议问题