python


Forking, daemonizing and monitoring a python script


I have a python script sc.py on my debian server.
I would like :
To run it as a daemon and keep it running indefinitely
To run it N times (as sub processes, children, forks) in //
To monitor the whole processes (in order to restart any dying process)
Do you know software that would enable me to do so?
I don't know if I have to look for a solution on the python side (any python module or configuration) or if there is a debian package somewhere which does that job?
You can use supervisord for this.
It daemonizes Python processes for you, and also handles subprocesses.
If this is a one-time task (that is, you are not creating a software product) I would quick-and-dirty use a combination of shell scripting and a terminal multiplexer like screen. For restarting processes that died (that is, they emitted an exit code other than 0), just use the shell.
Start your processes for example like this:
for i in n/*; do
screen -d -m -L -S $i -t $i until python sc.py $i; do echo "Crashed with exit code $?. Respawning.." >&2 ; sleep 1 ; done
done
This would
Create a new screen session for every file in the subdirectory n/ running your script,
Restart your python script unless it exited successfully (using Bash's until)
Set the title (-t) and session name (-S) to the input file name,
And turn on logging of all the output for later inspection if something went wrong (-L).
You can then use normal screen commands like screen -list to list all running tasks and screen -r <session name> to view the running session output.

Related Links

How to check if list1 contains some elements of list2?
Python logging from multiple modules to same log file
Python drag and drop, get filenames [closed]
Upload multiple files with Tornado Web Server and Nginx
How to read a simple binary file
How to run mysql in Django 1.5.5 with Python 3.3.4 in Windows 7
How can I do maps involving string fields in rethinkdb?
Scrollbar not functioning properly Tkinter
Is there a way to quickly create many files many sequential files (ex1 . . . ex 50) in Python?
Pygame playing sound with pygame.mixer.music.load(file) gives NoneType error
How to update multiple values in Mongodb using pymongo?
Manually return an error result and status failure for a celery task?
Python backtrace from C++ embedded in Python
Find nth smallest element in numpy array [duplicate]
How to extract array from the first element of triples in 2d array of triples using numpy
Simulate user clicking in QSystemTrayIcon

Categories

HOME
json
plsql
redis
xamarin.android
wms
ruby-on-rails-3
concourse
mainframe
wampserver
value
pygame
httpclient
internet-explorer-11
actionscript-2
onenote-api
wordpress-theming
game-maker-studio-1.4
mongodb-query
activesync
future
ssms-2016
yeoman-generator
quartus
ada
anychart
styles
aws-cognito
cruisecontrol.net
apple-tv
piwik
profile
java-ee-7
osmdroid
excel-2013
edge-detection
data-conversion
datadog
bxslider
smart-mobile-studio
read-write
pdflatex
superpowered
css-counter
lego
readfile
common.logging
ddms
react-native-router-flux
control-flow-graph
tango
z3py
python-idle
dojox.grid.datagrid
chown
number-theory
xmgrace
dwarf
chrome-remote-desktop
ruby-on-rails-2
jexl
search-box
python-hypothesis
jcreator
iron.io
build-process
libtiff.net
sql-import-wizard
windows-azure-pack
freetype2
nofollow
scriptlet
mpeg-4
asynccallback
qt-linguist
handlebars.java
except
hateoas
unity5.3
abas
wif
pyopengl
findcontrol
mdm-zinc
google-plus-one
low-level
hana-xs
proximity
dmp
google-admin-audit-api
ember-addon
emma
coin-flipping
cppdepend
jstack
logentries
layered
leptonica
vmware-server
argb
nuspec
shortcuts
linkedhashset
web-safe-fonts
autostart
getusermedia
nintendo
v4l
objectbrowser
electronic-signature
dcpu-16
phpcrawl
.app
msn
scala-2.8
openwysiwyg
weborb
self-reference

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App