QProcess doesn't work with "reboot" command
-
Hi,
I am trying to reboot my Linux system using QProcess.start() method. QProcess.start() works with all commands I have tried except from "reboot"... This is how my code looks like:
QProcess myprocess;
myprocess.start("reboot");Any line after these two is not being executed but the system doesn't reboot either.
Any help?
Thanks.
-
@Kate_Software What happens if you execute "reboot" manually in a shell? Do you need sudo or root rights to be able to reboot?
On my Ubuntu system reboot is in /sbin which means sudo is needed to execute it. -
Hi @jsulm, from the command window everything works well. I don't need super user... (I have tried with both with sudo, and without anyway just in case it makes any difference but it doesn't).
This is in Ubuntu xenial 32 bits... -
@Kate_Software hi,
maybe reading the output of the process can give a hintQString output = QString::fromLocal8Bit(process->readAllStandardOutput());
-
@Kate_Software
As @LeLev says you should always check the output from aQProcess
. However, you need to read both stdout & stderr. Additionally, you should also slot onto https://doc.qt.io/qt-5/qprocess.html#errorOccurred (which may well be what is happening to you here).However, you have a problem if you are sure when you say:
Any line after these two is not being executed
It's hard to believe that is the case, are you truly sure about this? You might also hook onto https://doc.qt.io/qt-5/qprocess.html#started.
EDIT What is the scope of your
QProcess myprocess;
? Although I would have thought a process once started would continue to run even if it goes out of scope, it might be that destructing it kills off the sub-process, I don't know.... -
@Kate_Software said in QProcess doesn't work with "reboot" command:
Any line after these two is not being executed but the system doesn't reboot either.
Is your application running as root?
Or do you have give the right to run reboot? (eg. with sudo?) -
@Kate_Software said in QProcess doesn't work with "reboot" command:
Any line after these two is not being executed"
Can you show the code extract to see how you are calling "reboot"?
I do something similar with a linux embedded device andQProcess::execute("reboot");
do the job for me.In my case, the application is running as root.
If don't want this, you can use sudo ==>QProcess::execute("sudo", QStringList() <<"/sbin/reboot");
After according the rights to use reboot to your user withvisudo -f /etc/sudoers.d/myApp
# Allow user appUser to run reboot as root without password appUser ALL = (root) NOPASSWD: /sbin/reboot
-
I think @KroMignon's last post was aimed at @Kate_Software, not at me.
Note that you can take this approach if & only if your application is intended only for your own use on your own machine. If you are intending to distribute your program, you cannot do things this way.
-
@JonB Oh sorry, reply was to fast! And yes, this solution is not relevant for distributed application... but there are not very much application which are not embedded/dedicated for a specific HW which requires to reboot the machine.
So I supposed this is for a very custom/specific use case, not to be distribute with apt or any other packaging system.