I'm writing a C program to do various stuff on a Linux system, then shut it down. This program will be started with the command line option
execl("/sbin/poweroff", "poweroff", NULL);
A number of programs assume the kernel has booted with
init as PID 1. On many systems
init is a symbolic link to the
systemd program; similarly on these systems,
poweroff is often a symbolic link to the
In your setup,
systemd is never started since you set your custom
init=/path/to/program kernel parameter line. This is why the
poweroff command doesn't work:
systemctl is trying to contact a
systemd instance which was never created.
reboot function is described in the Linux Programmer's Manual. Under glibc, you can pass the
RB_POWER_OFF macro constant to perform the reboot.
Note that if
reboot is not preceded by a call to
sync, data may be lost.
Using glibc in Linux:
#include <unistd.h> #include <sys/reboot.h> sync(); reboot(RB_POWER_OFF);