Results 1 to 18 of 18

Thread: start the mysql service and run commands

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default start the mysql service and run commands

    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

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: start the mysql service and run commands

    you can use QProcess.

  3. #3
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Quote Originally Posted by cbarmpar View Post
    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.

  5. The following user says thank you to jacek for this useful post:

    cbarmpar (10th September 2008)

  6. #5
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    Hi ,
    Many thanks for the reply.

    According to the documentation:

    Qt Code:
    1. #include <QProcess>
    2.  
    3. QProcess process;
    4. process.start("/etc/init.d/mysqld ");
    To copy to clipboard, switch view to plain text mode 

    Is that enough to start the process?

    Many thanks in advance
    Last edited by jacek; 10th September 2008 at 13:41. Reason: changed [qtclass] to [code]

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: start the mysql service and run commands

    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.

  8. The following user says thank you to spirit for this useful post:

    cbarmpar (10th September 2008)

  9. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Quote Originally Posted by cbarmpar View Post
    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().

  10. #8
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    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,

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Quote Originally Posted by cbarmpar View Post
    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.


    Quote Originally Posted by cbarmpar View Post
    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.

  12. #10
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    Thank you.

    Can you please write a small script to demonstrate that?

    Regards.

  13. #11
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default mysql process control problem!

    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.

    Qt Code:
    1. void kentriko::xekina(){
    2. process.start("/etc/init.d/mysqld start");
    3. }
    4. void kentriko::dixe(){
    5. ui.dixe_l->setText(process.readAllStandardOutput());
    6. }
    7. void kentriko::stamata(){
    8. process.execute("/etc/init.d/mysqld stop");
    9. ui.dixe_l->setText(process.readAllStandardError());
    10. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by cbarmpar; 10th September 2008 at 21:56. Reason: missing [code] tags

  14. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: mysql process control problem!

    Quote Originally Posted by cbarmpar View Post
    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.

  15. #13
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    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

  16. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Quote Originally Posted by cbarmpar View Post
    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.

  17. #15
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    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.

  18. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Quote Originally Posted by cbarmpar View Post
    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.

  19. #17
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: start the mysql service and run commands

    Many thanks for the reply friend.

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

    Regards.

  20. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: start the mysql service and run commands

    Take a look at kdesu sources.

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.