How to run a script forever?
Yes, you can use a while True:
loop that never breaks to run Python code continually.
However, you will need to put the code you want to run continually inside the loop:
#!/usr/bin/python
while True:
# some python code that I want
# to keep on running
Also, time.sleep
is used to suspend the operation of a script for a period of time. So, since you want yours to run continually, I don't see why you would use it.
How to make sure that python script will run forever on windows?
I think these answer all your questions:
Since your script runs on windows, you can use the Microsoft Task Scheduler (which is installed with Windows) to start your Python script when your computer starts up.
If you do not use the cmd window, you can change your Python script extention from
.py
to.pyw
to run the script without a terminal window. A bit more on that here: Executing scripts.For opening the script after an exception has happend, have a look at this blog post: How to Restart Python Script after Exception and Run it Forever.
To restart your script once a week, you can also use the Task Scheduler mentioned in answer 1. I think this post could help you with restarting your script: Start and stop a python task
How to constantly run Python script in the background on Windows?
On Windows, you can use pythonw.exe
in order to run a python script as a background process:
Python scripts (files with the extension
.py
) will be executed bypython.exe
by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension.pyw
which will cause the script to be
executed bypythonw.exe
by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
For example,
C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py
In order to make your script run continuously, you can use sched
for event scheduling:
The sched module defines a class which implements a general purpose
event scheduler
import sched
import time
event_schedule = sched.scheduler(time.time, time.sleep)
def do_something():
print("Hello, World!")
event_schedule.enter(30, 1, do_something, (sc,))
event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()
Now in order to kill a background process on Windows, you simply need to run:
taskkill /pid processId /f
Where processId
is the ID of the process you want to kill.
Unix: Have Python script constantly running best practice?
Adding your script to rc.local would work, but 'best practice' in my opinion would be to use Upstart. See this post:
Daemon vs Upstart for python script
I have a script that runs forever, how can I run it at startup in the background on macos?
I use launch-agents to achieve similar jobs, jobs that i need to run when in my login.
In this case you should change your script so that it does not loop forever, but does the work once and schedule this script to be executed periodically every 30 mins.
Create a small plist file in ~/Library/LaunchAgents/your-service-name.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>your.namespace.goes.here</string>
<key>ProgramArguments</key>
<array>
<string>yourscript-path-goes-here</string>
<string>some-args</string>
</array>
<key>StartInterval</key>
<integer>1800</integer>
</dict>
</plist>
Load the launch agentlaunchctl load -w <path_to_the_plist_file>
Remember to set up proper environment variables in the plist file, since it does not inherit the environment variables from your shell, since it is not run within that scope.
Refer this link for more info on launchagents and daemons.
I have noticed that when the launch agent framework invokes my script and the script exits quickly, the fw thinks the script dint work properly, So it restarts the script again and again and finally marks it as not-working. So I had to give a sleep 10
in my scripts that launch agent invokes.
You can alias launchctl print user/$(id -u $(whoami)) | grep <yournamespace>
to show the list of active launch agents.
Here is where you can find my petty scripts for launch agents.
How to Run some script forever without causing cpu fan overspeed in Linux
You could do something like this that uses a while loop every 30 minutes so hardly resource hungry, no need to echo anything in my opinion, just put this in your start up script and run it.
#!/usr/bin/env bash
a=$(date +%p)
b="PM"
c="AM"
theme_change () {
if [[ "$a" == "$b" ]] ; then
lookandfeeltool -a 'org.kde.breezedark.desktop'
elif [[ "$a" == "$c" ]] ; then
lookandfeeltool -a 'org.kde.breeze.desktop'
fi
}
while true; do
theme_change;
sleep 1800;
done
How do I keep my python backend scripts running forever on Microsoft Windows?
You could use https://learn.microsoft.com/en-US/windows-server/administration/windows-commands/sc-create to create a service then use Scheduled Tasks to control it.
Related Topics
Pandas Groupby Without Turning Grouped by Column into Index
Why Doesn't List Have Safe "Get" Method Like Dictionary
Convert Structured Array to Regular Numpy Array
How to Find Out Whether a File Is at Its 'Eof'
Securely Storing Environment Variables in Gae with App.Yaml
Splitting on Last Delimiter in Python String
How to Find Duplicate Elements in Array Using for Loop in Python
Getting the Indices of Several Elements in a Numpy Array at Once
How to Make Custom Legend in Matplotlib
How to Use a Custom Comparison Function in Python 3
Rreplace - How to Replace the Last Occurrence of an Expression in a String
Interactive Pixel Information of an Image in Python
How Are Python In-Place Operator Functions Different Than the Standard Operator Functions