Daemon vs Upstart for python script

2024/11/18 9:41:28

I have written a module in Python and want it to run continuously once started and need to stop it when I need to update other modules. I will likely be using monit to restart it, if module has crashed or is otherwise not running.

I was going through different techniques like Daemon, Upstart and many others.

Which is the best way to go so that I use that approach through out my all new modules to keep running them forever?

Answer

From your mention of Upstart I will assume that this question is for a service being run on an Ubuntu server.

On an Ubuntu server an upstart job is really the simplest and most convenient option for creating an always on service that starts up at the right time and can be stopped or reloaded with familiar commands.

To create an upstart service you need to add a single file to /etc/init. Called <service-name>.conf. An example script looks like this:

description "My chat server"
author "[email protected]"start on runlevel [2345]
stop on runlevel [!2345]env AN_ENVIRONMENTAL_VARIABLE=i-want-to-setrespawnexec /srv/applications/chat.py

This means that everytime the machine is started it will start the chat.py program. If it dies for whatever reason it will restart it. You don't have to worry about double forking or otherwise daemonizing your code. That's handled for you by upstart.

If you want to stop or start your process you can do so with

service chat start 
service chat stop

The name chat is automatically found from the name of the .conf file inside /etc/init

I'm only covering the basics of upstart here. There are lots of other features to make it even more useful. All available by running man upstart.

This method is much more convenient, than writing your own daemonization code. A 4-8 line config file for a built in Ubuntu component is much less error prone than making your code safely double fork and then having another process monitor it to make sure it doesn't go away.

Monit is a bit of a red herring. If you want downtime alerts you will need to run a monitoring program on a separate server anyway. Rely on upstart to keep the process always running on a server. Then have a different service that makes sure the server is actually running. Downtime happens for many different reasons. A process running on the same server will tell you precisely nothing if the server itself goes down. You need a separate machine (or a third party provider like pingdom) to alert you about that condition.

https://en.xdnf.cn/q/26503.html

Related Q&A

Comparison of Python modes for Emacs

So I have Emacs 24.3 and with it comes a quite recent python.el file providing a Python mode for editing.But I keep reading that there is a python-mode.el on Launchpad, and comparing the two files it j…

Python best formatting practice for lists, dictionary, etc

I have been looking over the Python documentation for code formatting best practice for large lists and dictionaries, for example,something = {foo : bar, foo2 : bar2, foo3 : bar3..... 200 chars wide, e…

TypeError: string indices must be integers, not str // working with dict [duplicate]

This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 28 days ago.I am trying to define a procedure, involved(courses, person…

Pandas: create dataframe from list of namedtuple

Im new to pandas, therefore perhaps Im asking a very stupid question. Normally initialization of data frame in pandas would be column-wise, where I put in dict with key of column names and values of li…

Closest equivalent of a factor variable in Python Pandas

What is the closest equivalent to an R Factor variable in Python pandas?

Temporarily Disabling Django Caching

How do you disable Django caching on a per checkout basis?Back before Django 1.3, I could disable caching for my local development checkout by specifying CACHE_BACKEND = None, in a settings_local.py i…

How to get a complete exception stack trace in Python

The following snippet:import tracebackdef a():b()def b():try:c()except:traceback.print_exc()def c():assert Falsea()Produces this output:Traceback (most recent call last):File "test.py", line …

python - should I use static methods or top-level functions

I come from a Java background and Im new to python. I have a couple scripts that share some helper functions unique to the application related to reading and writing files. Some functions associated …

Draw graph in NetworkX

Im trying to draw any graph in NetworkX, but get nothing, not even errors:import networkx as nx import matplotlib.pyplot as plt g1=nx.petersen_graph() nx.draw(g1)

Django 1.7 migrations wont recreate a dropped table, why?

Using Django 1.7 migrations.I accidentally dropped a table in my database. I assumed that by running migration again this would recreate the table but no, Django states "No migrations to apply&quo…