(1) First you can put the following in file called wrapper.py:
import time, sys
log, flush = sys.stdout.write, sys.stdout.flush
while True:
log("I am waking up, let's run the scripts\n")
# ****************************** #
# call your python scripts here
# example:
# import script1, script2
# script1.some_function1()
# script2.some_function2()
# ****************************** #
log("Done for today, I will go to sleep for 1/3 a week (56 hours)\n")
flush()
#(60 seconds/minute)x(60 minutes/hour)x(24 hours/day) x 3 days = 259200 seconds
hibernation = 259200
time.sleep(hibernation) # sleep for 3 days = 259200 seconds
(2) Then to launch this as a daemon, run this command in your terminal:
nohup python wrapper.py >> logfile.log 2>&1 &
nohup
"no hangup", makes the python process continue running even after you close your terminal (or exit your shell)
>>
sends the output to a file named logfile.log
2>&1
sends both stdout and stderr to logfile.log
&
sends the running process to the background and gives shell control back to you
Notice you only run the nohup command once (unless the server restarts, then you you would have run it again. If this happens often, you can automate auto-restarting by putting the nohup command in an executable bash script, and then creating a cron job for example for that script).
jenkines? what operation system do you use?