Results 1 to 6 of 6

Thread: how to use sudo command in Qprocess

  1. #1
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default how to use sudo command in Qprocess

    i want to execute this shell command in qt
    Qt Code:
    1. sudo ./filename.sh
    To copy to clipboard, switch view to plain text mode 
    this code executng in terminal,,while in Qprocess i getting error
    Qt Code:
    1. /bin/sh: 0: Can't open sudo
    To copy to clipboard, switch view to plain text mode 
    my code below:
    Qt Code:
    1. QProcess process;
    2. bool x;
    3. qint8 status;
    4. x=process.startDetached("/bin/sh", QStringList()<< "sudo "<<"./samp.sh");
    5. qDebug()<<process.readAllStandardOutput();
    6. status=process.waitForFinished();
    7. if(!x)
    8. {
    9. qDebug()<<process.errorString();
    10. }
    11. else
    12. {
    13. qDebug()<<"success";
    14. }
    To copy to clipboard, switch view to plain text mode 
    after searching i got one solution for sudo which wysota said
    http://www.qtcentre.org/threads/3962...commands-linux
    Qt Code:
    1. QString apath,program;
    2. apath=QDir("/bin/bash").absolutePath());
    3. program="gksudo";//i used gksudo i getting no answer
    4. args << "apt-get install";
    5. process->start(program,args);
    To copy to clipboard, switch view to plain text mode 
    how to use sudo commang in Qprocess please give me suggestion for this
    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use sudo command in Qprocess

    Quote Originally Posted by iswaryasenthilkumar View Post
    i want to execute this shell command in qt
    Qt Code:
    1. sudo ./filename.sh
    To copy to clipboard, switch view to plain text mode 
    If this is the command you want to execute, why is your startDetached() executing /bin/sh?

    Quote Originally Posted by iswaryasenthilkumar View Post
    Qt Code:
    1. x=process.startDetached("/bin/sh", QStringList()<< "sudo "<<"./samp.sh");
    2. qDebug()<<process.readAllStandardOutput();
    3. status=process.waitForFinished();
    To copy to clipboard, switch view to plain text mode 
    I would recomend to have a look at the documentation of QProcess, especially the section startDetached() is in (static public members).
    You should then come to the right conclusion why this code above will never do what you seem to expect it would.

    Cheers,
    _
    Last edited by anda_skoa; 24th June 2015 at 09:55.

  3. #3
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use sudo command in Qprocess

    thanks anda_skoa ..i read qprocess documentation and understand my mistake.and i changed my code,
    p
    Qt Code:
    1. bool success;
    2. QProcess gunzip;
    3. QStringList arguments;
    4. gunzip.setProcessChannelMode(QProcess::MergedChannels);
    5. QString program = "gksudo";
    6. arguments<<"./omxtx.sh";
    7. gunzip.start(program, arguments);
    8. success = gunzip.waitForStarted();
    9. if (!success)
    10. {
    11. qDebug("waitForStarted() error: %s", gunzip.errorString().toUtf8().constData());
    12. return;
    13. }
    14. success = gunzip.waitForFinished();
    15. if (!success)
    16. {
    17. qDebug("waitForFinished() error: %s", gunzip.errorString().toUtf8().constData());
    18. return;
    19. }
    20. QByteArray buffer = gunzip.readAll();
    21. QString output = buffer;
    22. qDebug("%s", output.toUtf8().constData());
    To copy to clipboard, switch view to plain text mode 
    the above code executing but i dont get proper result it getting password from user after that it not showing the the file output actualy inside the file i wrote echo hello i need output as hello i dont get this result i used this command "gksudo ./omxtx.sh" in terminal it get password and showing hello but in qt it getting password but it not showing hello what mistake am doing please guide me to get proper result[QUOTE=anda_skoa;278544]
    Quote Originally Posted by iswaryasenthilkumar View Post
    i want to execute this shell command in qt
    Qt Code:
    1. sudo ./filename.sh
    To copy to clipboard, switch view to plain text mode 
    If this is the command you want to execute, why is your startDetached() executing /bin/sh?


    I would recomend to have a look at the documentation of QProcess, especially the section startDetached() is in (static public members).
    You should then come to the right conclusion why this code above will never do what you seem to expect it would.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use sudo command in Qprocess

    Have you verified that the omxtx.sh is actually being executed?
    Does it do what it is supposed to do and you are just not getting its output?

    Cheers,
    _

  5. #5
    Join Date
    Nov 2014
    Location
    Chennai
    Posts
    160
    Thanks
    65
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: how to use sudo command in Qprocess

    yes it executing in terminal i getting output "hello".bt in Qprocess am not getting that
    Quote Originally Posted by anda_skoa View Post
    Have you verified that the omxtx.sh is actually being executed?
    Does it do what it is supposed to do and you are just not getting its output?

    Cheers,
    _

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: how to use sudo command in Qprocess

    Quote Originally Posted by iswaryasenthilkumar View Post
    yes it executing in terminal i getting output "hello".bt in Qprocess am not getting that
    I am not sure what the relevance of that would be.

    You already stated that it works if called manually, so that is obviously not what I meant when I wrote whether you verified that the script got executed.

    But to make it more clear: check that the script actually gets executed when you run gksudo through QProcess.
    I.e. to determine whether your problem with then output is the script not running or the text not being visible to QProcess.

    Cheers,
    _

Similar Threads

  1. Run sudo comand with QProcess, how to handle password prompt?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 4th August 2011, 14:47
  2. Replies: 3
    Last Post: 23rd February 2011, 09:50
  3. QProcess::start() failed when application runs from sudo
    By alenyashka in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2010, 07:35
  4. QProcess and the command line
    By auba in forum Qt Programming
    Replies: 17
    Last Post: 27th May 2009, 11:39
  5. how to use cat command in QProcess
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 27th January 2006, 19:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.