How do I create a crontab through a script

前端 未结 12 1087
再見小時候
再見小時候 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:18

    Well /etc/crontab just an ascii file so the simplest is to just

     echo "*/15 * * * *   root     date" >> /etc/crontab
    

    which will add a job which will email you every 15 mins. Adjust to taste, and test via grep or other means whether the line was already added to make your script idempotent.

    On Ubuntu et al, you can also drop files in /etc/cron.* which is easier to do and test for---plus you don't mess with (system) config files such as /etc/crontab.

提交回复
热议问题