user5021468
user5021468

Reputation:

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

Upvotes: 1

Views: 2384

Answers (2)

YOBA
YOBA

Reputation: 2807

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.

Upvotes: 2

user2983378
user2983378

Reputation:

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

Upvotes: 2

Related Questions