I recently had the need to reboot the machine in a C++ program (more on that later). Here's what I came up with.

void rebootMachine() {
    #ifdef __linux__
        // For Linux
        system("/sbin/shutdown -r now > /dev/null 2>&1");
        system("/etc/shutdown -r now > /dev/null 2>&1");
        system("/bin/shutdown -r now > /dev/null 2>&1");
    #else
        std::cerr << "Unsupported operating system." << std::endl;
    #endif

    std::cerr << "Reboot: must be superuser." << std::endl;
}

 

I was wondering what the /etc/shutdown entry is, it turns out that's for Slackware Linux. The bit at the end with the ampersand and stuff, is for redirecting stderr to stdout, and stdout is sent to /dev/null. That way there's no noise in the terminal.

That's about it for this entry. Have fun, as always.