PDA

View Full Version : start the mysql service and run commands



cbarmpar
9th September 2008, 19:52
Hi all,

I can start the mysql service by typing : /etc/init.d/mysqld start on the command line.

How can I do that through my code? is there a command for that?
Additionally is there any way to execute commands from my code?

Many thanks in advance.
Christos

spirit
10th September 2008, 07:57
you can use QProcess.

cbarmpar
10th September 2008, 12:32
Can you please provide me with a sample code?

I am sorry but i am new to c++ and linux and i cant understand the concept.

Many thanks

jacek
10th September 2008, 12:46
Can you please provide me with a sample code?

I am sorry but i am new to c++ and linux and i cant understand the concept.
You won't learn anything if you just keep copying examples. Read the QProcess docs and tell me which of its methods you could invoke to start external process.

cbarmpar
10th September 2008, 14:01
Hi ,
Many thanks for the reply.

According to the documentation:



#include <QProcess>

QProcess process;
process.start("/etc/init.d/mysqld ");




Is that enough to start the process?

Many thanks in advance

spirit
10th September 2008, 14:06
you need to pass start command also.
you should do everything like you do in command promt, e.g. if you start a programm with parameters you need use these parameters in QProcess also.

jacek
10th September 2008, 14:44
Is that enough to start the process?
It wasn't that hard, wasn't it? But there's one problem --- you create the QProcess object on the stack and when it goes out of scope the process will be killed. You might consider using QProcess::startDetached().

cbarmpar
10th September 2008, 14:48
many thanks friend.

So far so good.

I have a more complex question now which I cant solve at the moment.

How can i check if the process is already running before i opening it?
And how can I read the output of the command?

Kindest regards,

jacek
10th September 2008, 17:44
How can i check if the process is already running before i opening it?
In case of init.d scripts, you can run them with "status" parameter.



And how can I read the output of the command?
If you want to do that, you can't use startDetached() and you have to connect to one of the signals that QProcess offers.

cbarmpar
10th September 2008, 17:57
Thank you.

Can you please write a small script to demonstrate that?

Regards.

cbarmpar
10th September 2008, 22:55
Many thanks for the reply,

The question is how these command are executed. Are they executed as root or just as a normal user?
How can I execute the command as a root?

Here is the code showing how I am using the commands through the QProcess.



void kentriko::xekina(){
process.start("/etc/init.d/mysqld start");
}
void kentriko::dixe(){
ui.dixe_l->setText(process.readAllStandardOutput());
}
void kentriko::stamata(){
process.execute("/etc/init.d/mysqld stop");
ui.dixe_l->setText(process.readAllStandardError());
}

What I mainly want to do is to control the mysqld process. The problem that I have is that my code cannot actually open the database unless i have opened it from the command line before as a Super user.
I believe that the problem is that i am not executing the command as a superuser. How can i do that?
Generally how do we handle processes? any short example with the mysqld process?
How can we detect weather a process is already running before making a new qprocess object?

Many thanks in advance,
Kindest regards.

jacek
12th September 2008, 23:02
The question is how these command are executed. Are they executed as root or just as a normal user?
How can I execute the command as a root?
The command will be executed by the same user as your application. Use sudo or su.

cbarmpar
12th September 2008, 23:53
I cant use the su command.
only the sudo if i have configured for this command.
I am not sure but a programm started from a normal user cannot execute commands as a root.
But how about if my programm asked for a su password from the user and the execute the command as a root?

Regards

jacek
13th September 2008, 00:20
But how about if my programm asked for a su password from the user and the execute the command as a root?
That's what "use su" means.

cbarmpar
13th September 2008, 00:24
Yes but the question is not what su means. The question is if i can run a command as a su (and hence root).

I ment to say that if it is possible for a user to enter the su/root password and then run a command as a su/root user from my program

Regards.

jacek
14th September 2008, 22:01
I ment to say that if it is possible for a user to enter the su/root password and then run a command as a su/root user from my program
Yes, you can. :rolleyes:

cbarmpar
14th September 2008, 22:03
Many thanks for the reply friend.

Can you please provide me with a sample code to demonstrate that?

Regards.

jacek
15th September 2008, 00:08
Take a look at kdesu sources.