I have a Qt 4 GUI where I need to have a option in a drop-down menu that allows the user to choose to restart the computer. I realize this might seem redunant with the ability to restart the computer in other ways, but the choice needs to stay there. I've tried using system() to call the following:
and all of them just cause
reboot: must be superuserto be printed. Using system() to call reboot directly does the same thing. I'm not especially attached to using system() to do this, but it seemed like the most direct choice.
How can I reboot the system from the GUI?
How to Restart or Reboot Linux Server from the Command Line. Contents. Steps to Restart Linux using Command Prompt. Restarting Local Linux Operating System. Step 1: Open Terminal Window. Step 2: Use the shutdown Command. Alternative Option: Restart Linux with reboot Command. Reboot Remote Linux Server.
You can simply use terminal for starting the application. Like if you wish to start firefox simply typing in terminal will open your application. But if you wish to restart from your C++ script. This can be done by simply piping your command to shell.
When used with the -r option, the shutdown command performs a system reboot: By default, the system will be rebooted after 1 minute, but you can specify the exact time when you want the system to be rebooted. The time argument can have two different formats.
The sudo command tells Linux to run the command as an administrator, so you may need to type your password. The –r switch at the end indicates that you want the machine to restart. Note: See our article for additional Linux shutdown command options. Many Linux versions do not require administrator privileges to reboot.
The reboot
function is described in the Linux Programmer's Manual. Under glibc, you can pass the RB_AUTOBOOT
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_AUTOBOOT);
In Linux:
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
sync();
reboot(LINUX_REBOOT_CMD_POWER_OFF);
Have you tried running a shell script, using gksudo? Something like
gksudo shutdown -r
With any luck, that should pull up a modal dialogue to get user credentials.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With