How do I create a crontab through a script

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

    Even more simple answer to you question would be:

    echo "0 1 * * * /root/test.sh" | tee -a /var/spool/cron/root
    

    You can setup cronjobs on remote servers as below:

    #!/bin/bash
    servers="srv1 srv2 srv3 srv4 srv5"
    for i in $servers
      do
      echo "0 1 * * * /root/test.sh" | ssh $i " tee -a /var/spool/cron/root"
    done
    

    In Linux, the default location of the crontab file is /var/spool/cron/. Here you can find the crontab files of all users. You just need to append your cronjob entry to the respective user's file. In the above example, the root user's crontab file is getting appended with a cronjob to run /root/test.sh every day at 1 AM.

提交回复
热议问题