简体   繁体   中英

How to run python script only once in a specific day of the week in bash

I want my python script to execute only Tuesdays, Fridays and Sundays, but the catch is I only want it to execute once.

while true; do 
 # %u day of week (1..7); 1 is Monday
 DATE=$(date +%u)

 # if DATE, 2 -eq tuesday, 5 -eq friday, 7 -eq sunday
 if [ $DATE -eq 2 ] || [ $DATE -eq 5 ] || [ $DATE -eq 7 ]; then
    #execute python script 
    echo "Today is $DATE"
 fi

 echo $DATE
done

You can simply do it by using the "at" command.

More information about this command at http://manpages.ubuntu.com/manpages/hardy/man1/at.1posix.html

What you need is cronjob:

Start by adding a shebang line on the very top of your python script.

#!/usr/bin/python (Depends on where your python is: check its path with: $ whereis python)

Make your script executable with chmod +x

chmod +x myscript.py

And do a crontab -e and add 0 0 * * 2,5,0 /path/to/my/script/myscript.py

2, 5, 0 for every Tuesday, Friday, Sunday.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM