Page 1 of 1

Systemd and the daemon

Posted: Fri Jun 19, 2020 1:18 am
by DarkElvenAngel
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

Re: Systemd and the daemon

Posted: Sat Jun 20, 2020 7:47 pm
by pfletch101
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.

Re: Systemd and the daemon

Posted: Sat Jun 20, 2020 8:41 pm
by DarkElvenAngel
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

Re: Systemd and the daemon

Posted: Sun Jun 21, 2020 7:06 am
by RamaSpaceShip
DarkElvenAngel wrote:
Sat Jun 20, 2020 8:41 pm
if(getpid() == 1) return;
Since your process cannot have pid 1, this line is useless.

Re: Systemd and the daemon

Posted: Sun Jun 21, 2020 1:24 pm
by DarkElvenAngel
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?

Re: Systemd and the daemon

Posted: Sun Jun 21, 2020 3:38 pm
by RamaSpaceShip
Does it stop forking if you remove that line (if(getpid() == 1) return;) ?

Re: Systemd and the daemon

Posted: Mon Jun 22, 2020 2:50 pm
by DarkElvenAngel
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.