DarkElvenAngel
Posts: 1040
Joined: Tue Mar 20, 2018 9:53 pm

Systemd and the daemon

Fri Jun 19, 2020 1:18 am

Hello everyone,

I wrote a daemon that will fork itself but when I try to run it from systemd the demonizing code doesn't run and the daemon will not run correctly.

I don't understand why this is the case I used

Code: Select all

Type=forking
in the service but looking at my daemons log it is skipping the demonize function because it already thinks it's forked?

What can I do to correct this?

Thanks

pfletch101
Posts: 630
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: Systemd and the daemon

Sat Jun 20, 2020 7:47 pm

You can give us a lot more information! The complete unit file would be a good start. However, I would also check that neither the unit file nor the code you are running makes assumptions about where they are running - file references should be absolute.

DarkElvenAngel
Posts: 1040
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd and the daemon

Sat Jun 20, 2020 8:41 pm

pfletch101 wrote:
Sat Jun 20, 2020 7:47 pm
You can give us a lot more information! The complete unit file would be a good start. However, I would also check that neither the unit file nor the code you are running makes assumptions about where they are running - file references should be absolute.
Okay so I have a daemon I wrote in C and whenever I run it from the CLI it works perfectly, and the daemon forks thus creating the lock PID file. But when I was using the systemd service to launch it the didn't fork and doesn't create the lock PID file. I didn't include a service file because none of them would work. From the very simple to most complex.

I traced the issue to this line in my code

Code: Select all

if(getppid() == 1) return;
i = fork();
Since systemd is PID 1 it would return and not fork and finish setup. So I had to change that line to

Code: Select all

if(getpid() == 1) return;
It might have been a typo in the example code or just something old that doesn't work anymore, but now the daemon forks and runs properly. So I sorted it out.

Thanks for replying

User avatar
RamaSpaceShip
Posts: 121
Joined: Sun Apr 26, 2020 12:19 pm

Re: Systemd and the daemon

Sun Jun 21, 2020 7:06 am

DarkElvenAngel wrote:
Sat Jun 20, 2020 8:41 pm
if(getpid() == 1) return;
Since your process cannot have pid 1, this line is useless.

DarkElvenAngel
Posts: 1040
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd and the daemon

Sun Jun 21, 2020 1:24 pm

RamaSpaceShip wrote:
Sun Jun 21, 2020 7:06 am
DarkElvenAngel wrote:
Sat Jun 20, 2020 8:41 pm
if(getpid() == 1) return;
Since your process cannot have pid 1, this line is useless.
That's what I thought, so why does it work? What is a better way to get the process to fork reliable?

User avatar
RamaSpaceShip
Posts: 121
Joined: Sun Apr 26, 2020 12:19 pm

Re: Systemd and the daemon

Sun Jun 21, 2020 3:38 pm

Does it stop forking if you remove that line (if(getpid() == 1) return;) ?

DarkElvenAngel
Posts: 1040
Joined: Tue Mar 20, 2018 9:53 pm

Re: Systemd and the daemon

Mon Jun 22, 2020 2:50 pm

RamaSpaceShip wrote:
Sun Jun 21, 2020 3:38 pm
Does it stop forking if you remove that line (if(getpid() == 1) return;) ?
No it still works just fine. Since it wasn't documented I was under the impression that line was to prevent the daemon becoming a fork bomb.

So I will just leave it out.

Return to “Troubleshooting”