Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Run .bat file using QProcess in a Windows Service.



  • I am developing a Qt based Windows Process which is intended to execute a .bat file.

    // command to execute the batch script
    QString command = "myscript.bat " + "hello world";
    
    QProcess process;
    process.setProgram("cmd.exe");
    process.setArguments({ "/C", command });
    process.startDetached();
    

    This is the simple code I wrote to execute the batch script that takes a string as argument. I have created an Qt executable and the above program does the job perfectly.

    When I put the same piece of code inside a windows service, I see that the service started successfully but the batch script did not run. Is there any restricted privileges from the Windows that blocks the .bat file to be executed from the service ?

    Is there any other method that Qt supports which basically needs to execute a .bat file or set of cmd commands inside a windows service.


  • Lifetime Qt Champion

    Hi
    I would hook up QProcess errors singals and see what it says about the situation.
    Might a rights issue. or it simply dont find the file.


  • Qt Champions 2019

    @chaithubk To add to @mrjj : all parameters (including myscript.bat " + "hello world) have to be strings in a QStringList.



  • @jsulm The same program when I built as an executable it works. If I use it inside a windows service worker method there is a problem.



  • Hi, perhaps the windows service is run as another user, like Administrator? Try specifying the complete path to your myscript.bat file.



  • @chaithubk
    Before you start worrying about a possible permission error, where do you expect the service to pick up your myscript.bat file from, compared to when you run it yourself? Chances are it's not on the PATH being used by the service nor its current directory. As @hskoglund says, first thing try full path to that file. And as @mrjj says, you really need to hook up stdout & stderr to see if anything reported. Plus, how do you know your batch script is not being run, given that running as a service/detached its output will not go anywhere, you had better not be relying on that to see anything.


Log in to reply