Scheduling tasks using Python's Schedule module - python

From the docs:
import schedule
import time
def job():
print("I'm working...")
schedule.every(10).minutes.do(job)
schedule.every().hour.do(job)
schedule.every().day.at("10:30").do(job)
while True:
schedule.run_pending()
time.sleep(1)
I understand that while the program is running, it will do the function you tell it to run. What I don't understand is how you would go about making this an automated task for every day. Is the idea that you would call this from the command line and always leave that open? If I shut off my computer, I would have to re-enable that again wouldn't I?
I feel there is something I am missing when creating an automated Python task in this case. I am on a windows environment.

Here is the overview: Running tasks as startup items means different things on each OS which has nothing to do with python specifically.
On windows you could set it up as a windows service by wrapping it using the python library Pyinstaller (which changes your script to an .exe file then running your.exe install --startup='auto'
On Linux based OS's you would need to check where to put the script because the startup sequence has changed in the last few years. There are even management software packages to make it easier.
On mac there is the GUI tools for controlling startup services as well as launchctl http://www.macworld.com/article/2047747/take-control-of-startup-and-login-items.html
You can take a look at the process currently on your computer by going to:
Windows: Task manager (press ctrl-alt-delete and select Task manager)
(depending on your windows version) click the Details tab. You will see the User name be blank or have "System" if it's run as a system process.
Linux or Mac: in a terminal type ps -Al
responding to comments:
System level - if nobody is logged in what is your computer doing? (your script?, web server?, protein folding?, dreaming of electric sheep?)
Yes, Python would be taking up resources each time you run a separate script. I have Gigs of RAM and Python takes <30 MB to run each script (depending on the size of libraries + size of program+ io bound + cpu bound problems). Your system is running >100 processes currently and it able to run 1000's. Don't worry about optimizing your program on the system till it's a problem.

Related

Is there a way to stop and restart a self hosted Python script using Github Actions?

I have a project in which one of the tests consists of running a process indefinitely in order to collect data on the program execution.
It's a Python script that runs locally on a Linux machine, but I'd like for other people in my team to have access to it as well because there are specific moments where the process needs to be restarted.
Is there a way to set up a workflow on this machine that when dispatched, stops and restarts the process?
You can execute commands on your Linux host via GH Actions and SSH. Take a look at this action.

Is there a way to leave python code running when the computer is shutdown?

I have a python script that checks the temperature every 24 hours, is there a way to leave it running if I shut the computer down/log off.
Shutdown - no.
Logoff - potentially, yes.
If you want to the script to automatically start when you turn the computer back on, then you can add the script to your startup folder (Windows) or schedule the script (Windows tasks, cron job, systemd timer).
If you really want a temperature tracker that is permanently available, you can use a low-power solution like the Raspberry Pi rather than leaving your pc on.
The best way to accomplish this is to have your program run on some type of server that your computer can connect to. A server could be anything from a raspberry pi to an old disused computer or a web server or cloud server. You would have to build a program that can be accessed from your computer, and depending on the server and you would access it in a lot of different ways depending the way you build your program and your server.
Doing things this way means your script will always be able to check the temperature because it will be running on a system that stays on.
Scripts are unable to run while your computer is powered off. What operating system are you running? How are you collecting the temperature? It is hard to give much more help without this information.
One thing I might suggest is powering on the system remotely at a scheduled time, using another networked machine.
You can take a look at the following pages
http://www.wikihow.com/Automatically-Turn-on-a-Computer-at-a-Specified-Time
http://lifehacker.com/5831504/how-can-i-start-and-shut-down-my-computer-automatically-every-morning
Additionally once it turn on, you can perform a cronjob, for execute your python code by a console command >> python yourfile.py . What is the Windows version of cron?

scheduler for running python scripts

I have a python script which installs a build from server to a node.
Now I want to schedule the task to a specific time, so that it triggers automatically.
One way is the windows scheduler to trigger batch file which internally calls runs the python script.
Do python have built-in support for this.
It should be OS independent, runs on both windows and linux platform.
Any ideas?
I've used this lightweight scheduler before to handle cron tasks -> https://github.com/mrhwick/schedule
I'd highly recommend it.

how to identify source of frequent process startup

Some months ago I used to play around with Python and Django, finally setting up a Django web service running python manage.py ... on a RaspberryPi. Now, I'd like to use the Linux device for other things. Unfortunately, there seems to be a frequent startup of some process (every couple of seconds) that eats up the available processing power. And I can not remember or see, WHO is starting this process or WHERE it is started.
The following picture shows a htop output. The process shown right below the title row uses 83% of CPU power and seems to be invoked by the following command line (run_gunicorn seems to be part of the Python / Django environment):
/home/pi/.virtualenvs/ENV_python27/bin/python /home/pi/examples/django__test/manage.py run_gunicorn -w 4 .
The fact that the PID of the odd process changes every couple of seconds makes it impossible for me as a linux novice to further invest its source and details. In the picture the process has the PID 24296.
Is there a way to find the place within the linux file system and its files where this process is frequently started? Can I somehow remove the respective command in order to not waste so much processing power? Are there a handful of possible places from where Linux can startup processes automatically (like CRON, which I have already checked)?
Please ask for more details and I will try to provide them.
Thanks.
The gunicorn process is probably being run by supervisor. Look at your /etc/supervisor/supervisord.conf file or /etc/supervisor/supervisord.conf.d directory.

How can I monitor a python scrypt and restart it in the event of a crash? (Windows)

I have a simple python script to send data from a Windows 7 box to a remote computer via SFTP. The script is set to continuously send a single file every 5 minutes. This all works fine but I'm worried about the off chance that the process stops or fails and the customer doesn't notice the data files have stopped coming in. I've found several ways to monitor python processes in a ubuntu/unix environment but nothing for Windows.
If there are no other mitigating factors in your design or requirements, my suggestion would be to simplify the script so that it doesn't do the polling; it simply sends the file when invoked, and use Windows Scheduler to invoke the script on whatever schedule you need. By relying on a core Windows service, you can factor that complexity out of your script.
You can check out restartme the following link shows how you can use it
http://www.howtogeek.com/130665/quickly-and-automatically-restart-a-windows-program-when-it-crashes/

Categories

Resources