How to constantly run Python script in the background on Windows?
I have created a script that moves files from one folder to another. But since the original folder is the Downloads folder I need it to always run in the background.
I also have a standard Batch file that looks something like this:
I’m using Windows 10. I have found info for Linux and OS on how to use nohup in the batch file. Is there a Windows version?
If there is do you need to execute the script every time you restart or switch the PC on?
Also, how do you terminate the process when you do manage to make it permanent?
3 Answers 3
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 by python.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 by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
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
Now in order to kill a background process on Windows, you simply need to run:
Where processId is the ID of the process you want to kill.
How to start a background process in Python?
I’m trying to port a shell script to the much more readable python version. The original shell script starts several processes (utilities, monitors, etc.) in the background with «&». How can I achieve the same effect in python? I’d like these processes not to die when the python scripts complete. I am sure it’s related to the concept of a daemon somehow, but I couldn’t find how to do this easily.
8 Answers 8
Note: This answer is less current than it was when posted in 2009. Using the subprocess module shown in other answers is now recommended in the docs
(Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions.)
If you want your process to start in the background you can either use system() and call it in the same way your shell script did, or you can spawn it:
(or, alternatively, you may try the less portable os.P_NOWAIT flag).
While jkp’s solution works, the newer way of doing things (and the way the documentation recommends) is to use the subprocess module. For simple commands its equivalent, but it offers more options if you want to do something complicated.
Example for your case:
This will run rm -r some.file in the background. Note that calling .communicate() on the object returned from Popen will block until it completes, so don’t do that if you want it to run in the background:
See the documentation here.
Also, a point of clarification: «Background» as you use it here is purely a shell concept; technically, what you mean is that you want to spawn a process without blocking while you wait for it to complete. However, I’ve used «background» here to refer to shell-background-like behavior.
How can I change my desktop background with python?
How can I change my desktop background with python?
I want to do it in both Windows and Linux.
12 Answers 12
On Windows with python2.5 or higher, use ctypes to load user32.dll and call SystemParametersInfo() with SPI_SETDESKWALLPAPER action.
For Python3.5, SystemParametersInfoA doesn’t work. Use SystemParametersInfoW.
I use the following method in one of my initial projects:
The get_desktop_environment method has been posted in another thread.
‘) – user5413945 Apr 8 ’16 at 12:21
On a gnome desktop, you usually do this with gconf, either directly calling gconftool or using the gconf python module. The latter is in the link given by unutbu. The first method could be done like this.
In gnome, it is probably preferable to use the python binding of gconf directly:
Firstly, import ctypes : it gives you access to windows components such as the screensaver, wallpapers, etc.
Then call ctypes.windll.user32.SystemParametersInfoA(a,b,c,d) :
On windows, you will need some trickery with pywin32, and the windows API, on ‘linux’ the answer will depend on which desktop is running — KDE, Gnome, or something more exotic. Under KDE (and maybe Gnome) you can probably send a message using D-Bus, which you could do without including any new libraries by using the command line tool dbus-send.
The other option would be to set the desktop wallpaper to a file which you then edit / replace from python — but this will probably only result in a change when the user logs in.
There is a difference what SystemParametersInfo method to be called based on what if you are running on 64 bit or 32 bit OS. For 64 bit you have to use SystemParametersInfoW (Unicode) and for 32 bit SystemParametersInfoA (ANSI)
Alternatively: (with SystemParametersInfoA )
Arguments are:
SystemParametersInfo(SetOrGet, GetBufferSize, SetBufferOrGetBuffer, SetChange)
The path has to be absolute, so if you’re using something relative to your script, do:
path = os.path.abspath(path)
To see more stuff you can do with SystemParametersInfo , see the docs.
(near the bottom there’s an example to change the mouse speed)
P.S. There are many answers already here, but they’re leaving out the broadcasting you’re supposed to do. Sure it works without it, but it’s bad practice not to use it properly.
P.P.S And they only gave hard coded values, rather than the variables they come from.
Also note, i use 512 characters for the buffer size when getting the path, just to be more safe since paths might exceed 256. I doubt anyone will have paths as long as that though.
One more note. I’ve only tested the above examples in Python 3, but i don’t think SystemParametersInfoA needs the .encode() in Python 2. (they updated strings in Python 3 to unicode i believe) The string in SystemParametersInfoW may need converting for Python 2.
Windows python in background
Let us see how to run a Python program or project in the background i.e. the program will start running from the moment device is on and stop at shut down or when you close it. Just run one time to assure the program is error-bug free
One way is to use pythonw, pythonw is the concatenation of python+without terminal window, i.e. run python without terminal window. You can use it by running the following line on the terminal:
Here’s the background.py is the file:
In Linux and mac, for running py files in the background you just need to add & sign after using command it will tell the interpreter to run the program in the background
It will run the program in the background also simultaneously you can use a terminal. There will process id for the background process, if you want you can kill process also by using as you can’t just kill it by CTRL+C , To kill it, open another terminal session and use the command
kill is abbreviated for killing process while -9 used to tell to kill it immediately, the corresponding status will be updated. In order to get your output, you can flush it up in a file by using
python filename.py > filenameToFlush &
It will be generating output i.e. flushing output in the file but it’s updated in the buffer memory, you have to wait for the termination of the program to reflect output in a hard disk file. To resolve this, you just need to tell python interpreter that don’t use buffered memory steps:
End/kill currently running the file
Now use utility
python -u filename.py > FileToFlush &
It will directly put the output in the file you have selected.
If you close terminal before the end of the program, all processes executed by the terminal will stop, A hangup situation is arising in order to counter problem you need to use nohup command as shown below nohup will assure that process is running till the end whether you close parent terminal. nohup stands for no hangup
Now you don’t need to flush output in any file as nohup utility generates a file named nohup.out while executing. It will be like log file. The name of the output fill generated by nohup will reflect on. To terminate this execution you will need ID process, There is no problem if you can remember or if you can’t, you need to search for file Just use following command
grep is for pattern searching, it will be reflecting process id on terminal just kill it by using kill -9 ID. Now the process is terminated
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
How to start a background process in Python?
I’m trying to port a shell script to the much more readable python version. The original shell script starts several processes (utilities, monitors, etc.) in the background with «&». How can I achieve the same effect in python? I’d like these processes not to die when the python scripts complete. I am sure it’s related to the concept of a daemon somehow, but I couldn’t find how to do this easily.
8 Answers 8
Note: This answer is less current than it was when posted in 2009. Using the subprocess module shown in other answers is now recommended in the docs
(Note that the subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using these functions.)
If you want your process to start in the background you can either use system() and call it in the same way your shell script did, or you can spawn it:
(or, alternatively, you may try the less portable os.P_NOWAIT flag).
While jkp’s solution works, the newer way of doing things (and the way the documentation recommends) is to use the subprocess module. For simple commands its equivalent, but it offers more options if you want to do something complicated.
Example for your case:
This will run rm -r some.file in the background. Note that calling .communicate() on the object returned from Popen will block until it completes, so don’t do that if you want it to run in the background:
See the documentation here.
Also, a point of clarification: «Background» as you use it here is purely a shell concept; technically, what you mean is that you want to spawn a process without blocking while you wait for it to complete. However, I’ve used «background» here to refer to shell-background-like behavior.