Ubuntu: create a Linux service (daemon)

A Linux daemon is a process that runs in the background of the operating system, like a Windows service. This tutorial explains how create and add a daemon (local service) on Linux, Ubuntu distribution. This procedure works on desktops, virtual machines, and servers running Ubuntu, whether with or without a GUI, including the minimum edition per netinstall. All versions of Ubuntu are affected by this tutorial, both annual editions and LTS versions.

This is used for example to automatically start a program or run a script each time the system is started / restarted, very useful for an httpd or web server. nginx, a DBMS such as Oracle Where PostgreSQL, etc. Added to init.d, the process will start automatically and we can monitor it with a “status” and manage it with “start”, “restart”, “stop”.

Create a daemon in Ubuntu

1. Open a local or remote console using SSH.

2. Use a text editor to create a new file (gedit, vi, vim, nano, emacs…):

sudo vi /etc/init.d/windowsfacile

3. Write or paste the content of a start-up script for the process (s) to be started.

# Provides: windowsfacile
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start windowsfacile at boot time
# Description: Enable windowsfacile service

4. Give the rights to execute on this new file:

sudo chmod +x /etc/init.d/windowsfacile

5. Activate the daemon:

sudo systemctl enable windowsfacile

6. Reload the system daemons (or restart the OS):

sudo systemctl daemon-reload