简体   繁体   中英

Trying to run a shell script from Qt with sudo rights

command line (works fine)

$ sudo chPermissions.sh

trying to do this from within a Qt program, using QProcess and have tried the following without success

code:

QString program = "/bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 0 (the script ran but without sudo rights & didn't work!)

code:

QString program = "sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 1

code:

QString program = "/usr/bin/sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 1

code:

QString program = "/bin/sh";
QStringList arguments;
arguments <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 0 (the script ran! no sudo rights)

code:

QString program = "/bin/sh";
QStringList arguments;
arguments << sudo <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 127 (command not found)

code:

QString program = "/bin/sh";
QStringList arguments;
arguments << /usr/bin/sudo <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 2 (Misuse of shell builtins)

code:

QString program = "/bin/bash";
QStringList arguments;
arguments <<  "-c" << "\"/usr/bin/sudo /usr/bin/chPermissions.sh"\";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 127

code:

QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 0 (the script ran!)

code:

QString shellCommandLine = "sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 1

code:

QString shellCommandLine = "/usr/bin/sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 1

code:

QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 0 (script ran without sudo rights)

Anyone able to let me know the secret of how to do this? (or put me out of my misery if it's not possible)

TIA

Andy

Not solved as such.

I got round this by

  1. using one of the options that actually runs the script (without su rights), and
  2. granting permission in sudoers, to user, to run the command in the script without password prompt.

hope that makes sense.

not happy, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM