[Apcupsd-users] apcupsd - systemd issue with pidfile
Brought to you by:
adk0212
|
From: Lorenzo B. <lor...@vd...> - 2022-04-13 10:14:50
|
- OS: Ubuntu 20.04 LTS (amd64) - UPS: Smart-UPS 1500 (but the issue is UPS independant) - apcupsd version: 3.14.14 - A clear description of the problem: Ubuntu 20.04, as many other modern Linux distributions, uses systemd for starting, stopping and monitoring daemons. The content of the apcupsd.service file bundled with the OS is the following (I highlighted the essential parts for understanding the issue): ========================================================================= [Unit] After=network-online.target Description=UPS power management daemon Documentation=man:apcupsd(8) [Service] ExecStartPre=/lib/apcupsd/prestart ExecStart=/sbin/apcupsd *Type=forking* KillMode=process *PIDFile=/var/run/apcupsd.pid* [Install] WantedBy=multi-user.target ========================================================================= If you look at the systemd documentation ( https://www.freedesktop.org/software/systemd/man/systemd.service.html) and you search for Type=forking you will find the following statement: [...] ... if the process decides to write a traditional PID file, systemd will be able to read the main PID from there. Please set PIDFile= accordingly. *Note that the daemon should write that file before finishing with its initialization. Otherwise, systemd might try to read the file before it exists.* [...] Now let's describe the issue! Although the daemon is started and works correctly, the startup log always reports the following: root@bacon:/etc/apcupsd# systemctl status apcupsd.service ● apcupsd.service - UPS power management daemon Loaded: loaded (/lib/systemd/system/apcupsd.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2022-04-01 10:08:31 CEST; 2s ago Docs: man:apcupsd(8) Process: 15683 ExecStartPre=/lib/apcupsd/prestart (code=exited, status=0/SUCCESS) Process: 15697 ExecStart=/sbin/apcupsd (code=exited, status=0/SUCCESS) Main PID: 15699 (apcupsd) Tasks: 3 (limit: 9042) Memory: 1.2M CGroup: /system.slice/apcupsd.service └─15699 /sbin/apcupsd Apr 01 10:08:31 bacon systemd[1]: Starting UPS power management daemon... Apr 01 10:08:31 bacon systemd[1]: *apcupsd.service: Can't open PID file /run/apcupsd.pid (yet?) after start: Operation not permitted* Apr 01 10:08:31 bacon apcupsd[15699]: apcupsd 3.14.14 (31 May 2016) debian startup succeeded Apr 01 10:08:31 bacon systemd[1]: Started UPS power management daemon. Apr 01 10:08:31 bacon apcupsd[15699]: NIS server startup succeeded This thing I highlighted in red, that systemd reports, is due to the fact that the parent process of the daemon exits before the child has written the PID file. I proved this statement by inserting a "sleep(1)" before the "exit(0) /* parent exits */" call in apcupsd.c:daemon_start() function. It fixed the issue by keeping the parent alive for a while, giving the child enough time to call make_pid_file(). I know that sleep(1) is not a bugfix rather a test for verifying the root cause of the issue. So could you please implement a delay in the parent before exiting (i.e. waiting for the PID file of the child to be created) in order to make apcupsd 100% compatible with systemd as used in Ubuntu? Best regards and many thanks in advance, Lorenzo Buzzi -- -- Lorenzo Buzzi VDS Rail www.vdsrail.com |