Skip to content

Automatically Run the Server

Systemd can be used to run the server automatically at startup as a background service (daemon).

Service File

Systemd uses a service file for each process. Service files are located in /etc/systemd/system and have the extension .service (e.g. taurec.service).

After the file is created, it can be activated with systemctl enable and started with systemctl start. In this example, the service name is taurec.

When the server runs as a service, relative paths for files and directories should not be used. Absolute paths should be used for database directories, web server directory, TLS files, log directory, and control file.

Example service file (/etc/systemd/system/taurec.service):

[Unit]
Description=Taurec Server
After=network-online.target
Wants=network-online.target
[Service]
User=taurec
Type=forking
ExecStart=/home/taurec/application/server daemon
[Install]
WantedBy=multi-user.target

Description

Description of the service.

After/Wants

network-online.target waits until the network is up.

User

User to run the process.

Type

The forking type is used for services that daemonize themselves. This means that the service process forks a child process and then exits, leaving the child process running in the background.

ExecStart

Command to start the server. The daemon parameter indicates that the server should be started as a daemon in background.

WantedBy

multi-user.target defines the system state where all network services are up but a local GUI must not be started. This is the typical default system state for server systems.

Permission

The permission of the service file can be changed if needed:

shell
chmod -c 0644 /etc/systemd/system/taurec.service

Control the Process

Automatic startup can be enabled or disabled:

shell
systemctl enable taurec.service
systemctl disable taurec.service

The process can be started or stopped:

shell
systemctl start taurec.service
systemctl stop taurec.service

The status of the process can be shown (shows the process number in Main PID):

shell
systemctl status taurec.service

End the Process Immediately

The process can be ended immediately with the kill command. This is not recommended.

Kill by Name

The process of the application server can be ended:

shell
pkill -term -x server

Kill by Number

The process number (PID) of the application server can be determined:

shell
ps -A | grep server

The process can be ended using the process number:

shell
kill -term <ProcessNumber>