How to restart or shutdown the whole computer with a command inside a pthreads-process?
exec ("reboot"); ??
exec("shutdown now");??
Printable View
How to restart or shutdown the whole computer with a command inside a pthreads-process?
exec ("reboot"); ??
exec("shutdown now");??
The Windows API to do stuff like this is ExitWidnowsEx.
It is not, normally considered polite to restart/shut down the machine programmatically tho'.
--
Mats
Be sure to have the user's permission first. I would be quite annoyed if a program rebooted my machine without my permission.
thanks but I forgot to specify I need it for Linux!! I need a "clean" shutdown of course!!
I am talkinng of an industrial PC in an industrial application, not for the use of several users so SW shutdown and restart are needed!
The main process (multipthread) is written by me and a colleague and only a few other server daemons will be running (like ssh, ftp etc.)
system("init 1") will reboot the system. See http://unixhelp.ed.ac.uk/CGI/man-cgi?init+8
--
Mats
Another (fast+forced+manual) way is (kernel must be compiled with magic sysrq keys):
So, basicly, you write 's' (sync), wait some time, write 'u' (remount ro), wait some time, and then write 'b' or 'o'. ('o' didn't test, but docs says it's for shutdown).Code:FILE *f=fopen("/proc/sysrq-trigger", "w");
if (f)
{
// s = sync
// u = remount all read-only
// b = reboot
// o = halt
fprintf(f, "s\n");
...
// sleep between steps
...
fclose(f); // reached ???
}
EDIT: assuming, of course, you have all the system under control, and can afford this approach...
I suggest doing a "man init" to be sure. IIRC, "init 0" halts the system, "init 6" reboots, and "init 1" gets the system back to single-user mode.
thank you!!