How to write a cron that will run a script every day at midnight?

前端 未结 6 1296
予麋鹿
予麋鹿 2020-11-28 02:21

I have heard crontab is a good choice, but how do I write the line and where do I put it on the server?

6条回答
  •  萌比男神i
    2020-11-28 02:47

    You can execute shell script in two ways,either by using cron job or by writing a shell script

    Lets assume your script name is "yourscript.sh"

    First check the user permission of the script. use below command to check user permission of the script

    ll script.sh

    If the script is in root,then use below command

    sudo crontab -e

    Second if the script holds the user "ubuntu", then use below command

    crontab -e

    Add the following line in your crontab:-

    55 23 * * * /path/to/yourscript.sh

    Another way of doing this is to write a script and run it in the backgroud

    Here is the script where you have to put your script name(eg:- youscript.sh) which is going to run at 23:55pm everyday

    #!/bin/bash while true do /home/modassir/yourscript.sh sleep 1d done

    save it in a file (lets name it "every-day.sh")

    sleep 1d - means it waits for one day and then it runs again.

    now give the permission to your script.use below command:-

    chmod +x every-day.sh

    now, execute this shell script in the background by using "nohup". This will keep executing the script even after you logout from your session.

    use below command to execute the script.

    nohup ./every-day.sh &

    Note:- to run "yourscript.sh" at 23:55pm everyday,you have to execute "every-day.sh" script at exactly 23:55pm.

提交回复
热议问题