2

I have a folder "Automation" that contains Python scripts which handle web automation tasks and extract text and Excel files. Those text and Excel files are temporary stored in "Automation".

Currently I am running the scripts 3 times a week from a bash wrapper file located in the same directory, but I would like that it runs itself automatically from a server so it's executed when I'm not using my computer.

How would you make this work? Thanks

7
  • Do you have jenkines? what operation system do you use? Commented Dec 19, 2019 at 12:14
  • 1
    Have you tried crontabs? Commented Dec 19, 2019 at 12:15
  • If its only time dependent - a cron job. More involved solutions (with more bells and whistles) are possible if there are other constraints as well - from using celery (celeryproject.org) to writing your own systemd service. Commented Dec 19, 2019 at 12:16
  • 2
    Try googling for setting up a cron job, thats prolly all you need. Commented Dec 19, 2019 at 12:24
  • 1
    here is a video on cronjobs by Luke Smith. I believe he explains these things quite good. Good Luck. Commented Dec 19, 2019 at 12:42

2 Answers 2

1

(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).

Sign up to request clarification or add additional context in comments.

Comments

1

i think you want to make a file that launches the bash file at specific times that you wish.I think that this script would help you:

from datetime import datetime
import os
# the list below should be supplied with the times when you want to launch your bash script
times = [ "write times in the format(no whitespaces, only colons): day(lowercase):hour:minutes",  "day(lowercase):hour:minutes"]
path = r"Path/of/executable/bash/file"      # this is the bash file that runs your other python scripts
while(True):
    now=datetime.now()
    nowtime=now.strftime("%H:%M")
    today=now.strftime("%A")
    today=today.lower()
    time=today+":"+nowtime   # this line would bring it into the correct format

    for eachtime in times:      # These three lines would check if the current time is correct to launch the bash script
        if time = eachtime:
            os.system("cd "+path+"\r\n <command to execute bash>")      # If it is correct then execute the bash file.

You can start this script and let it run forever.. it would automatically launch the bash files at the correct times..

I hope i could solve your problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.