Embedded Linux: shutdown from program started up with systemd?

Hi guys,

I have a program which has the possibility of doing a shutdown or reboot remotely by doing a popen("reboot") or popen("halt") call after doing some cleanup.

I have found that if I execute the application directly from the command line, this works and the shutdown or reboot takes place immediately. However if I try it when the program has been automatically executed on startup through a systemd script, there is a really long delay (a little more than one minute) before it works.

Does anybody here know why this might be, and if there is any way to work around it? Or is this a question I should ask on a Linux group somewhere rather than here?

Reply to
sbattazzo
Loading thread data ...

Am 21.04.2015 um 18:22 schrieb snipped-for-privacy@gmail.com:

Strictly speaking, that can't be entirely true, because those are not correct calls of popen() to begin with. They're lacking the "w" or "r" argument. And you shouldn't have to call popen() instead of the simpler system() if you didn't actually need to talk to those sub-programs, or receive information from them.

Generally speaking, I'm not so sure that calling another program to do those things is the right approach.

This could very well be related to the fact you're using popen(). One difference between systemd and an interactive session is that the shell runs in a console session, with access to display output and keyboard input. One end of a popen() is supposed to inherit one of those connections from the parent. If that's not there to be inherited, the subprocess may well behave differently. For a closer approximation of what happens if your program is run by systemd, you might want to run it in the background, i.e.

yourprogram &

Yes, it is, because there's really nothing embedded-specific about it.

Reply to
Hans-Bernhard Bröker

e:

Yes, I had the "r" or "w" there in the actual source code, just omitted it here. And yes, you're right that I don't need to pipe anything in or out of that call, but I think I used something like "re" so that hopefully the po pen call would wait until the program was finished executing. I wasn't sure if I could get that behavior out of system(). I could try system() anyway.

Fair enough, thanks for the help anyway!

Reply to
sbattazzo

Just to give an update, using system() instead of popen() fixed the issue for me, so it was pretty much as you suggested.

I guess I should have known to try that.. thanks for the hint though!

Reply to
sbattazzo

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.