Linux servers are designed to remain active all the time; instead of manually starting important programs and leaving them in a
tmux session, you should add them to
systemd as a service that starts automatically and reboots if errors occur.
Working with Systemd and Services
Systemd stores the configuration for services in two places. The first is
/ lib / systemd / system / where you will find the configuration for many services on your system. Most software installations install services here. The second is
/ etc / systemd / system / which overwrites the directory
/ lib / systemd and is generally used to host user-created services. There is also
/ etc / systemd / users / that performs services for individual logged in users, such as mail retrieval.
Many of these services don't always run like nginx or MySQL. You can print a list of services currently in use:
Services with a "+" symbol are running and services with a "-" symbol are currently being stopped. You can view more detailed information with:
service status nginx
Since services run in the background, they do not log their output to your console, but log the output to the systemd journal. The "status" command shows the last few lines of this magazine, but you can read it directly with:
journalctl -fn 50 -u nginx
This command prints the most recent 50 log entries (
- n ) from the nginx service (
-u ). It is configured to print everything and start at the bottom, according to new log entries as they were created (
Of course, many applications will still write most things to their access or error logs, so make sure to check those as well. The journal keeps track of things logged directly to the console, which can be useful for debugging your own services
Simple Service Setup
Systemd is used for many things in Linux; each object it manages is called a unit and has an associated "unit file" that defines what they are. These can be simple services like nginx or MySQL, but they can also be things like mountpoints, devices, sockets and lots of other things under the hood, all managed by systemd. Units can also be targets, which are used to determine when other services are running (i.e. after the network is initialized).
For this use case, you probably only want to configure your application as a base service. To do this, you need to create a new unit file, which you want to put in
/ etc / systemd / system / and name with the extension
hit / etc /systemd/system/myapp.service%19659006 Unit files have a few different sections, but generally look something like this:[Unit] Description = Sample service After = network.target StartLimitIntervalSec = 0 [Service] Type = simple Restart = always RestartSec = 1 User = service user ExecStartPre = ExecStart = / path / to / executable [options] ExecStartPost ExecStop = ExecReload = [Install] WantedBy = multi-user.target
First the section
[Unit]that defines a lot of metadata about the unit. The
After =directive can be used to delay the activation of units until another unit is started, such as
networkor another service, such as
mysql.serviceThis does not & # 39; Don't make it difficult depending on that service, although you can do that with guidelines
Wants =. This section also configures the maximum number of times the unit will attempt to start before systemd gives up completely; since you probably want to keep trying, you can set it to 0 to disable this behavior.
Following is the section
[Service]specific to service unit files. Here you configure the Exec options.
Userwill run the service as a specific user. You can set this to your personal user account, root or a custom service account. Make sure that the user has sufficient rights to do his job.
There are a few different guidelines here for specifying programs to run.
ExecStartPreis the first to run, so you can make all the settings necessary before the service actually starts.
ExecStartis the main executable file.
ExecStartPostis executed afterwards and
ExecStopis executed when the service is closed.
ExecReloadis a special guideline and is used when you call "reload" instead of restarting. This allows you to reload the configuration, provided your application has the capability.
Finally, the section
[Install]that defines some more behavior regarding how systemd treats the unit. This is usually used to specify the
WantedBy =directive, which is used to tell systemd when to start your service, creating symlinks between targets and their dependent units. If you are not sure what purpose to use,
multi-user.targetwill run services at startup after almost everything loads.
In general, the configuration is quite simple and everything you really need to do is put in your executable as an argument in the section
[Service]. Once your service is created you will need to reload the systemctl daemon to update with your changes:sudo systemctl daemon-reload
And enable it (which is run at startup, according to the unit configuration):  sudo systemctl enable myapp
And then start the service:sudo service myapp start
The service should now run in the background, and you can view
journalctlto view the view output. If it shuts down,
systemdwill restart it automatically and it should run alongside your other services on your system during startup.
If you need to restart your service, you can use the following:sudo service myapp restart
ExecStop =directive, shuts down the unit and then restarts.