Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is systemd PID file?

I want to run jar file as a daemon. So I have written a shell script to "start|stop|restart" the daemon. I didn't get a chance to its working status. Can I use this script without creating a PID file? Why do we need a PID file at all? In which case we should use PID file?

Below is my UNIT file.

[Unit]
Description=myApp
After=network.target

[Service]
Environment=JAVA_HOME=/opt/java/jdk8
Environment=CATALINA_HOME=/opt/myApp/
User=nzpap
Group=ngpap
ExecStart=/kohls/apps/myApp/myapp-scripts/myapp-deploy.sh
Restart=always

[Install]
WantedBy=multi-user.target

I did not gain info by browsing through the internet about PID concept.

like image 238
Sekhar Avatar asked Dec 15 '17 05:12

Sekhar


People also ask

What is the PID of systemd?

systemd is a system and service manager for Linux operating systems. When run as first process on boot (as PID 1), it acts as init system that brings up and maintains userspace services. Separate instances are started for logged-in users to start their services.

What does PID file do?

A PID file is a file which contains the PID of the executable which generated it. When an application terminates, that file is removed. If it is removed while the application is running, the application terminates. If the application restarts, a new PID is written to the file.

Does systemd create a PID file?

systemd will read the PID of the main process of the daemon after start-up of the service. systemd will not write to the file configured here, although it will remove the file after the service has shut down if it still exists. Save this answer. Show activity on this post.

What is a systemd file?

Systemd is a system and service manager for Linux, compatible with SysV and LSB init scripts. Systemd provides: Aggressive parallelization capabilities. Uses socket and D-Bus activation for starting services. Offers on-demand starting of daemons, keeps track of processes using Linux cgroups.


2 Answers

Below is the documentation of the PIDFile=<path-to-pid> parameter supported by systemd.

The idea is that many existing services do a fork() and you have no control over whether the service should or not fork. So in that case you want to use this option.

Since you are creating your own service, the important part to read about is the second paragraph which tells you to not use a PID file at all. As you said: It's useless. Especially useless since you can get that PID using the following command:

systemctl [--user] show --property MainPID --value <service>

Note: the --user is used to query user specific services.

And within the .service file, you can access that PID with the special variable named $MAINPID as in:

ExecStop=/bin/kill "$MAINPID"

Further, any child of that process will be in a group using that same $MAINPID, so you can get the list of children using that same number like so:

pgrep --pgroup $MAINPID

This list may also appear when you check the process status:

systemctl status <service>

If that works, see the CGroup: ... field. This doesn't work for all services.

PIDFile=

Takes a path referring to the PID file of the service. Usage of this option is recommended for services where Type= is set to forking. The path specified typically points to a file below /run/. If a relative path is specified it is hence prefixed with /run/. The service manager will read the PID of the main process of the service from this file after start-up of the service. The service manager will not write to the file configured here, although it will remove the file after the service has shut down if it still exists. The PID file does not need to be owned by a privileged user, but if it is owned by an unprivileged user additional safety restrictions are enforced: the file may not be a symlink to a file owned by a different user (neither directly nor indirectly), and the PID file must refer to a process already belonging to the service.

Note that PID files should be avoided in modern projects. Use Type=notify or Type=simple where possible, which does not require use of PID files to determine the main process of a service and avoids needless forking.

Note 1: emphasis added by me.

Note 2: if you do use a PID file with systemd, it should be under /run/... or you must specify a full path (a path starting with a /).

like image 137
Alexis Wilke Avatar answered Oct 23 '22 00:10

Alexis Wilke


A pidfile holds the main task. Because the main task is possible able to spawn more to handle requests or whatever. Two examples for that...

#pidof apache2
31583 31582 31579 31577 31576 31571 347
#pidof tinyproxy
6813 6812 6811 6810 6809 6808 6807 6806 6805 6804 6803 6802 6801 6800 6799 6798 6797 6796 6795 6794 6793
#ls /var/run/apache2/
apache2.pid  cgisock.347  
#cat /var/run/apache2/apache2.pid 
347
#cat /var/run/tinyproxy/tinyproxy.pid 
6793
like image 30
koyaanisqatsi Avatar answered Oct 23 '22 00:10

koyaanisqatsi