I’ve noticed that there are a lot of faulty or non properly working start/stop services out there in the wild. That’s why I decided to build and explain some example services for the most common distributions and operating systems (like Windows).
In this case, I assume that our example service is Python’s SimpleHTTPServer which starts a web server on port 80 and serves the current working directory. The server can be easily executed by:
$ python -m SimpleHTTPServer 8000
(Caution: this command will probably serve the root directory of your machine. Make sure to stop the service again!)
Debian and Ubuntu (sysvinit)
- Create an user for the desired service
Ensure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script (as root) to
/etc/init.d/:wget https://raw.github.com/frdmn/service-daemons/master/debian -O /etc/init.d/exampleAdjust the variables:
sudo vi /etc/init.d/exampleMake sure the script is executable:
chmod +x /etc/init.d/exampleEnable the daemon with:
update-rc.d example defaultsStart the service with:
service example start
Ubuntu (upstart)
- Create an user for the desired service
Ensure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script to
/etc/init/:sudo wget https://raw.github.com/frdmn/service-daemons/master/ubuntu -O /etc/init/example.confAdjust the variables:
sudo vi /etc/init/example.confStart the service with:
sudo start example
CentOS 6 (sysvinit)
- Create an user for the desired service
Ensure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script (as root) to
/etc/init.d/:wget https://raw.github.com/frdmn/service-daemons/master/centos -O /etc/init.d/exampleAdjust the variables (as root):
vi /etc/init.d/exampleMake sure the script is marked as executable:
chmod +x /etc/init.d/exampleEnable the config in in runlevels 2, 3, 4, and 5:
chkconfig example onStart the service with:
service example start
Arch Linux (systemd)
- Create an user for the desired service
Ensure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script (as root) to
/etc/systemd/system/:wget https://raw.github.com/frdmn/service-daemons/master/arch -O /etc/systemd/system/example.serviceAdjust the variables (as root):
/etc/systemd/system/example.serviceMake sure the script is executable:
chmod +x /etc/systemd/system/example.serviceEnable the script on boot with:
systemctl enable exampleTo start the script:
systemctl start example
Gentoo (runscript)
- Create an user for the desired service
Ensure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script (as root) to
/etc/init.d/:wget https://raw.github.com/frdmn/service-daemons/master/gentoo-script -O /etc/init.d/exampleCopy the following configuration file (as root) to
/etc/conf.d/:wget https://raw.github.com/frdmn/service-daemons/master/gentoo-conf -O /etc/conf.d/exampleMake those files executable:
chmod +x /etc/init.d/example /etc/conf.d/exampleLoad the deamon:
rc-update add etherpad-lite defaultStart the deamon with:
rc-service etherpad-lite start
Mac OS X (launchd)
Note: For some reasons the SimpleHTTPServer python module didn’t work via launchd, so I included this tiny web server python script in the repo.
- Create an user for the desired service
Create a log folder for the service:
mkdir /var/log/exampleEnsure the created user has full access to the binary you want to set up:
/usr/bin/pythonCopy the following script (as root) to /Library/LaunchDaemons/:
wget https://raw.github.com/frdmn/service-daemons/master/macosx -O /Library/LaunchDaemons/mn.frd.example.plistCopy the python script (as root) to /tmp/:
wget https://raw.github.com/frdmn/service-daemons/master/macosx-httpd.py -O /tmp/httpd.pyLaunch the daemon with:
sudo launchctl load /Library/LaunchDaemons/mn.frd.example.plist
Windows (nssm)
- Download and install
nssm:
non sucking service manager - Move it into your
%PATH% - Open an administrative terminal window
- Load the service: nssm install "C:/Python27/python" -m SimpleHTTPServer 8000
- Reboot your machine
Please let me know in case you found some typos or errors above. You can also fork the repo and send a Pull Request via GitHub if you have some improvements for the start/stop daemons.
Comments