execute and run script python on a server several times - python

I have a script in python that takes date as an argument in order to get the event in google agenda using google API calendar at that date. Now my problem is as follows: There are three machines: first one is doing some treatment in order to get a date for some reason, second machine is the server that the two other machines are connected to, third machine is where i have to execute the script python in order to get event of its google calendar.
So my question is how can I do such that for every 5 minutes - the first machine send the date (that means every 5 minutes i get a different date ) and each date I get I have to execute that script python to get the event on that date and send the answer to the first machine (doing it dynamically and the server is in the middle between the two other machines ).
In other words, how can I make this process dynamic such that for every 5 minutes the script python has to be executed using for each time a different date and give back the result?
I want to execute the script like this :
python script.py '2017-12-10 10:40:00' #(first time)
python script.py '2017-12-10 10:45:00' #(after 5 minutes)
python script.py '2017-12-10 10:50:00' #(after another 5 minutes)
and so on ....

With a crontab entry like
*/5 * * * * python path/to/script.py "$(date %%F %%T)"
where the path/to is relative to your home directory, and the */5 is a Vixie cron (in practice, more or less Linux only) extension which might not work on other platforms. The crontab format requires the percent signs to be doubled -- maybe put this in an external script if you need complex expressions with many percent signs.
There is no guarantee that this runs exactly on the second, or even on the minute. Maybe use %%H:%%M:00 instead of %%T to force the seconds (forcing the minutes will be slightly more challenging; again, maybe put the logic in an external script if you need this).

Related

Python script and Google Cloud Engine [duplicate]

I have two Python scripts on my machine that I want to execute two times a day on specific time period. How do I automate this task? Since I will be away from home and thus my computer for a while, I want to upload them to a site and be executed from there automatic without me doing anything.
How can I do this?
You can use cron for this if you are on a Linux machine. Cron is a system daemon used to execute specific tasks at specific times.
cron works on the principle of crontab, a text file with a list of commands to be run at specified times. It follows a specific format, which can is explained in detail in man 5 crontab
Format for crontab
Each of the sections is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within Sections 1-5, only between them. Sections 1-5 are used to indicate when and how often you want the task to be executed. This is how a cron job is laid out:
minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
01 04 1 1 1 /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on January 1st plus every Monday in January. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:
01 04 * * * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.
Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously. Code:
01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand
The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.
The "/usr/bin/somedirectory/somecommand" text in the above examples indicates the task which will be run at the specified times. It is recommended that you use the full path to the desired commands as shown in the above examples. Enter which somecommand in the terminal to find the full path to somecommand. The crontab will begin running as soon as it is properly edited and saved.
You may want to run a script some number of times per time unit. For example if you want to run it every 10 minutes use the following crontab entry (runs on minutes divisible by 10: 0, 10, 20, 30, etc.)
*/10 * * * * /usr/bin/somedirectory/somecommand
which is also equivalent to the more cumbersome
0,10,20,30,40,50 * * * * /usr/bin/somedirectory/somecommand
In Windows I have come up with two solutions.
First option: Create a .bat file.
Step 1
Create a .bat file to indicate the command you want to run and the script file that will be executed, for instance:
start C:\Users\userX\Python.exe C:\Users\userX\PycharmProjects\Automation_tasks\create_workbook.py
Step 2
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
Step 3
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not, check the option Run with highest privileges and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2.
Step 4
In the Actions tab, click on the New button and type in the following:
In Program/Scripts you need to look up for the Powershell path that the Task Scheduler will invoke to run the .bat file. In my case, my Powershell path was:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
In Add arguments (optional) you need to type the path of the file that will be executed by Powershell. In my case, the path was:
C:\Users\userX\Desktop\run_the_bat_file.bat
In Start in (optional) you need to type the path of the file but without the name of the .bat file, that is:
C:\Users\userX\Desktop\
Step 5
Click on the Triggers tab and select how often you want to execute this task.
Step 6
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
Second option: Run the .py file with the Task Scheduler
Step 1
Open the Task Scheduler and click on the Task Scheduler Library to see the current tasks that are executed. Click on the Create Task option.
Step 2
In the General tab, put the name of your new task and click on the option Run whether user is logged on or not, check the option Run with highest privileges and make sure to setup the appropriate version of you OS (in my case I picked Windows 7, Windows Server 2008 R2.
Step 3
In the Actions tab, click on the New button and type in the following:
In Program/Scripts you need to look up for the Python.exe path that the Task Scheduler will invoke to run the python script. In my case, my Python.exe path was:
C:\Users\userX\python.exe
In Add arguments (optional) you need to only type the name of your python script. In my case, the path was:
Permissions_dump.py
In Start in (optional) you need to type the path of the file but without the name of the python script, that is:
C:\Users\userX\PycharmProjects\1099_vendors_costs
Step 4
Click on the Triggers tab and select how often you want to execute this task.
Step 5
Lastly, test your task to see if it truly works by selecting it from the Task Scheduler Library and doing click on the Run option.
Another option (in case you convert a .py to a .exe)
If you use the library Cx_Freeze to convert a .py to a .exe and you want to use the task scheduler to automate this task then you need to follow these steps:
Step 1
Click on Create Task and then click on the Actions tab to type in the following:
In Program/Scripts you need to look up for the C:\Windows\explorer.exe path that the Task Scheduler will invoke to run the .exe script.
In Add arguments (optional) you need to only type the name of your .exe file: CustomerPopulation.exe
In Start in (optional) you need to type the path of the file but without the name of the .exe file, that is:
C:\Users\userX\PycharmProjects\executables
In the General tab, make sure to have selected the Run only when user is logged on and have unchecked the Run with the highest privileges.
If reports stopped working
Make sure to check if your password hasn’t expired, otherwise the reports won’t be sent.
References:
https://gis.stackexchange.com/questions/140110/running-python-script-in-task-scheduler-script-will-not-run?newreg=603bcdbc381b41a283e5d8d0561b835e
https://www.youtube.com/watch?v=oJ4nktysxnE
https://www.youtube.com/watch?v=n2Cr_YRQk7o
If you are using OSX then launchd is the preferred way to schedule tasks. There is a OSX CLI for launchd called launchctl but if you prefer a GUI my preferred one is launchcontrol.

Run python script automatically every week (windows)

I wrote a little python Script that fetches data from some website using hard coded credentials ( i know its bad but not part of this question).
The website has new data every day and im gathering data from a whole week and parse it into a single .pdf.
I've already adjusted the script to always generate a pdf off last week by default. (no params needed)
Im kinda lazy and don't want to run the script every week by hand.
So is it possible to run the script at certain times, for example every monday at 10am?
Sure, just utilize Windows' task scheduler. There you can create new tasks to your delight and let it run commands to whatever times or intervalls you want. The task schedulers' GUI should be self-explanatory, but to be concrete on your example:
Configure the run time (weekly, monday, 10am) under triggers
Add a new action and give it your Python interpreter as the command and your script to be run as the argument
Configure the rest according to your needs

Python : How can I run a program at a certain time

I want to run a program at a certain time.
For example, When I run the program, it will be executed at 5 p.m
How can I implement the above function using Python?
As pointed out in the comments this has nothing to do with python and rather with your OS.
So if you are on Linux run the command crontab -e and enter the lines:
* 9 * * 0-5 python /path/to/your/python/script
* 17 * * 6 python /path/to/your/python/script
The first line is for 9am every day except on saturday and the second one is for 17pm saturday.
If you are on Windows press the windows button + R, then type: taskschd.msc which opens the task scheduler. Here press "Action" and "Create task..." which opens a new windows which lets you create a new task, the instructions there is self explanatory.
Within Python, thus within a long/permanent running (service) process of your own, you can use sched.scheduler or threading.Timer from the standard lib or use custom code (in a thread).
For starting a python script in a timed manner ordinarly as a user use OS means (crontab [*nix], Scheduled Tasks [Windows], ...) or use 3rd party scheduling apps - as you would do for scheduling other programs.
A python program could control those schedulers via API (e.g. win32com.taskscheduler) or command-line/popen...
If you want to do it with python you could use Celery beats, although not recommended if you're not using celery.
On *nix systems you just want to use cron jobs instead and on windows use the task scheduler.

Running a Python Script every n Days in Django App

I am looking to run a Python script every n days as part of a Django app.
I need this script to add new rows to the database for each attribute that a user has.
For example a User has many Sites each that have multiple Metric data points. I need to add a new Metric data point for each Site every n days by running a .py script.
I have already written the script itself, but it just works locally.
Is it the right approach to:
1) Get something like Celery or just a simple cron task running to run the python script every n days.
2) Have the python script run through all the Metrics for each Site and add a new data point by executing a SQL command from the python script itself?
You can use Django's manage commad, then use crontab to run this command circularly.
The first step is to write the script - which you have already done. This script should run the task which is to be repeated.
The second step is to schedule the execution.
The de-facto way of doing that is to write a cron entry for the script. This will let the system's cron daemon trigger the script at the interval you select.
To do so, create an entry in the crontab file for the user that owns the script; the command to do so is crontab -e, which will load a plain text file in your preferred editor.
Next, you need to add an entry to this file. The format is:
minutes hours day-of-month month day-of-week script-to-execute
So if you want to run your script every 5 days:
0 0 */5 * * /bin/bash /home/user/path/to/python.py

Running two python scripts through cron which alters two different tables in same mysql database

I am relatively new to programming and thus have limited experience when it comes to the logic involved in getting scripts to work as I want.
In a nutshell I have an Arduino connected to a raspberry pi (Raspbian). The Arduino controls sensors while the raspberry pi acts as a web server. I have created a database in MySQL containing one database with two tables. The first table needs to receive an INSERT every 5 minutes (through script 1.py) and the second table gets an UPDATE every minute. Both tables receive values from the Arduino.
I can run each script separately, and it works fine. I can join the two scripts and that works too, BUT because I use cron, they both run at the SAME time interval (e.g., 5 minutes). If I run both scripts (5 minute interval and 1 minute interval) through cron, only one works. I think it has something to do with the timing of the open and close connections?
Are there any suggestions as to how I can get this to work? Ideally I would like 1 script to run every 5 minutes and the other every 5 seconds (cron can't do seconds).

Categories