I need to run python script and be sure that it will restart after it terminates. I know that there is UNIX solution called supervisord. But unfortunately server where my script has to be run is on Windows. Do you know what tool can be useful? Thanks
6 Answers
Despite the big fat disclaimer here, you can run Supervisor with Cygwin in Windows; it turns out that Cygwin goes a long way to simulate a Posix environment, so well that in fact supervisord runs unchanged. There is no need to learn a new tool, and you will even save quite a bit of work if you need to deploy a complicated project across multiple platforms.
Here's my recipe:
- If you have not done it yet, install Cygwin. During the installation process, select Python.
- From the Cygwin terminal, install virtualenv as usual.
Create a virtualenv for supervisord, and then install as usual:
pip install supervisord
Configure supervisord in the usual way. Keep in mind that supervisord will be running with Cygwin, so you better use paths the Cygwin way (C:\myservers\project1 translates to /cygdrive/c/myservers/project1 in Cygwin).
Now you probably want to install supervisord as a service. Here's how I do it:
cygrunsrv --install supervisord --path /home/Administrator/supervisor/venv/bin/python --args "/home/Administrator/supervisor/venv/bin/supervisord -n -c /home/Administrator/supervisor/supervisord.conf"
Go to the Windows service manager and start the service supervisord that you just installed.
Point 5 installs supervisord as a Windows service, so that you can control it (start/stop/restart) from the Windows service manager. But the things that you can do with supervisorctl
work as usual, meaning that you can simply deploy your old configuration file.
-
7A short notice: it is `pip install supervisor` now and it requires python 2.4+ but it doesn't work with any 3+ versions. – Peon May 03 '16 at 13:22
You likely want to run your script as a Windows Service
. To do so you'll need the python-win32 library. This question has a good description of how you go about doing this, as well as a bunch of links to other related resources. This question may also be of use.
A Windows Service
is how you want to wrap up any script that needs to run continuously on Windows. They can be configured to automatically start on boot, and handle failures. Nothing is going to stop anyone from killing the process itself, but to handle that potential situation, you can just create a bat
file and use the sc
command to poll the service to see if it is running and if not restart the service. Just schedule the bat
file to run every 60 seconds (or whatever is reasonable for your script to potentially be down).

- 2,286
- 3
- 34
- 46

- 5,889
- 2
- 27
- 22
-
Running python script as Windows service will make it only run in background. It is not enough. My goal is to make script running unbreakable. Script must be restarted even if someone kills it. – pss Oct 03 '11 at 12:49
-
In that case, use a bat file, the sc command and the system scheduler to handle a 'killed' process. See my edit. – Mark Gemmill Oct 03 '11 at 15:51
If you want a supervisord-like process manager that runs on most posix OS and is Python-based like supervisord, then you should look at honcho which is a Python port of foreman (Ruby-based):
http://pypi.python.org/pypi/honcho/
It works great on mac, linux but (actually) not yet windows... (editing my initial answer where I had said optimistically it was already working on Windows based on a pull request that has been discarded since)
There is a fork that provides Windows support here https://github.com/redpie/honcho and some work in progress to support Windows here https://github.com/nickstenning/honcho/issues/28 ... at least it could become a possible solution in a near future.
There is also a foreman fork to support Windows here: https://github.com/ddollar/foreman-windows that may be working for you, though I never tried it.
So for now, a Windows service might be your best short term option.

- 2,017
- 21
- 36
-
3The latest TRUNK of honcho now has windows support :) use and enjoy – Philippe Ombredanne Mar 20 '13 at 19:09
-
Hi, lil' question about Honcho: is it really supposed to restart failed tasks, like supervisor ? It doesn't when I `kill` one. So do we have to use a combination of supervisor and honcho ? Thanks. – Ehvince Sep 27 '16 at 15:59
-
Actually honcho is more like foreman than supervisor. See also https://blog.codeship.com/using-honcho-create-multi-process-docker-container/ – Philippe Ombredanne Sep 29 '16 at 06:47
supervisor for windows worked for us on python27 - 32 bit. I had to install pypiwin32 and pywin32==223.

- 191
- 1
- 7
As it is an old question with old answers I will update it with latest news:
There is a supervisor-win project that claims to support supervisor on Windows.

- 1,933
- 1
- 18
- 30
No, supervisord is not supported under Windows.
BUT what you can do is, to restart it automatically from a wrapper script:
#!/usr/bin/python
from subprocess import Popen
file_path = " script_to_be_restarted.py"
args_as_str = " --arg1=woop --arg2=woop"
while True:
print("(Re-)Start script %s %s" % (file_path, args_as_str))
p = Popen("python " + file_path + args_as_str, shell=True)
p.wait()

- 4,723
- 4
- 39
- 50