systemctl --Managing server applications managed by systemd

You can use the systemctl command to manage server applications managed by systemd.

Here, we will explain various commands using apache2.

Start application

Use the start command to start the application.

sudo systemctl start apache2

Stop application

Use the stop command to stop the application.

sudo systemctl stop apache2

Restart the application

Use the restart command to restart the application.

sudo systemctl restart apache2

After stop, start is executed.

Application status

Use the status command to see the status of the application.

sudo systemctl status apache2

If the application is running, you should see something like this:

● apache2.service --The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           mqapache2-systemd.conf
   Active: active (running) since Fri 2020-06-05 10:27:15 JST; 3h 32min ago
 Main PID: 1624 (apache2)
    Tasks: 55 (limit: 9479)
   CGroup: /system.slice/apache2.service
           tq1624 / usr / sbin / apache2 -k start
           tq1628 / usr / sbin / apache2 -k start
           mq1629 / usr / sbin / apache2 -k start

Application reload

If your application defines reload, you can use reload.

sudo systemctl reload apache2

For apache2, graceful start is defined, and for postfix, reloading of the configuration file is defined.

View the systemd configuration file for each application

The systemd configuration file for each application is included in the "systemctl status" content.

sudo systemctl status apache2

The following "/lib/systemd/system/apache2.service" is the systemd apache configuration file.

● apache2.service --The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           mqapache2-systemd.conf
   Active: active (running) since Fri 2020-06-05 10:27:15 JST; 3h 32min ago
 Main PID: 1624 (apache2)
    Tasks: 55 (limit: 9479)
   CGroup: /system.slice/apache2.service
           tq1624 / usr / sbin / apache2 -k start
           tq1628 / usr / sbin / apache2 -k start
           mq1629 / usr / sbin / apache2 -k start

Let's use the cat command to look at the contents of "/lib/systemd/system/apache2.service".

[Unit]
Description = The Apache HTTP Server
After = network.target remote-fs.target nss-lookup.target

[Service]
Type = forking
Environment = APACHE_STARTED_BY_SYSTEMD = true
ExecStart = / usr / sbin / apachectl start
ExecStop = / usr / sbin / apachectl stop
ExecReload = / usr / sbin / apachectl graceful
PrivateTmp = true
Restart = on-abort

[Install]
WantedBy = multi-user.target

systemd configuration file

The systemd configuration file is located in "/ lib / systemd / system". The following is the Apache configuration file.

/lib/systemd/system/apache2.service

Added as a systemd service

To add a new application managed by systemd, add the configuration file in the directory where the systemd configuration file is located.

Sample official documentation for creating a web application in Mojolicious and running it in hypnotoad with systemd looks like this:

[Unit]
Description = My Mojolicious application
After = network.target

[Service]
Type = forking
PIDFile = / home / sri / myapp / script / hypnotoad.pid
ExecStart = / path / to / hypnotoad / home / sri / myapp / script / my_app
ExecReload = / path / to / hypnotoad / home / sri / myapp / script / my_app
KillMode = process

[Install]
WantedBy = multi-user.target

The file name should be as follows. The "mywebapp" part can be any name. Use vi to edit and add content.

sudo vi /lib/systemd/system/mywebapp.service

If you register it as a service in systemd, you can automatically start the Web application when the OS is restarted.

You can check whether the configuration file is written correctly by checking the status of the systemctl command.

systemctl status mywebapp

Associated Information