Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

c++ command to shutdown ubuntu computer?

i'm using c++ but don't know the command to shutdown my ubuntu lucid computer.

How would I shutdown or logout the ubuntu from my .exe i made from c++?

Update:

if u have nothing useful to say don't post

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Okay ...

    First to shutdown Linux you have to invoke a command to the shell

    Many commands can do like: $ init 0

    So we look for a way to call a terminal shell from inside our c++ application then send this command to that shell.

    Many functions in c++ allow this. Lets use popen():

    #include <stdio.h>

    int main() {

    extern FILE *popen();

    /* popen creates a pipe so we can read the output

    of the program we are invoking in terminal*/

    if (!(popen("init 0", "r"))) {

    exit(1);

    }

    Here we create a pointer to a file that will hold the output of the command (we don't need the output for the purpose in our hand). Then we use the method popen() to send the command as the first parameter as you see.

    Notes:

    1.This command (init) may need a root privilege to be executed. I don't know how to do this maybe you need to run your c++ application as root?

    2.I didn't try this myself, i just did some search for my idea and this is what i came up with.

    I hope this help you.

  • 4 years ago

    Shutdown Ubuntu Command

  • Anonymous
    1 decade ago

    I don't believe there is one... What you want to do is call the program shutdown with root privileges and attribute now (sudo shutdown now) or hook into GDM and get it to do it. But I personally have no idea how to hook into GDM to spare you the sudo so I'm afraid you will have to ask someone from gnome...

Still have questions? Get your answers by asking now.