How to package a Python daemon with setuptools

2019-02-09 09:13发布

问题:

How do you package a Python app with setuptools so that when it's installed (e.g. via setup.py or pip), it places a daemon script in the appropriate location, starts it, and marks it to automatically start at boot time?

In my case, my code only works with Linux, so I only care about installing the daemon in Linux environments (specifically Ubuntu).

I've found several posts describing how to easily create Python daemons, but I can't seem to find anything describing how you'd install them in a production environment so that they'd be treated as any other normal daemon or service.

I know Ubuntu and some other distros keep their daemons in /etc/init.d, and I know how to use setuptools to copy files to specific locations on the filesystem, so would it be safe to copy or symlink my script to /etc/init.d and then run chkconfig to set it's runtime, or is there a more safe distribution-neutral way of installing daemons?

回答1:

This would be better handled by creating an appropriate package for the distro (in the case of Ubuntu, a .deb) since you can't guarantee the location of the startup scripts across distros. For example, arch linux uses /etc/rc.d/. Also, copying to locations like that will require root access (which I don't believe is necessarily required to install python packages) and may not be easily reversible when uninstalling.

I would suggest that you create a normal setupttools installation and then package it into a deb with a link to /etc/init.d.