1

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
  • this answer doesn't require a cron job: https://stackoverflow.com/questions/43670224/python-to-run-a-piece-of-code-at-a-defined-time-every-day – Tom Logan May 09 '18 at 16:10

2 Answers2

2

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

2

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.

YOBA
  • 2,759
  • 1
  • 14
  • 29
  • I {heart} cron too, but how does this fit the question stating that "the catch is I only want it to execute once." – chicks Sep 19 '15 at 23:45
  • Oh it doesn't only execute once when I use crontab? –  Sep 20 '15 at 03:49
  • @JohnnyBoy it will execute once every chosen day, see http://www.crontab-generator.org/ to understand better crontab expressions. – YOBA Sep 20 '15 at 12:26