Can't kill/terminate batch process



  • Hello,

    I'm running a batch file using QProcess.start().
    Because is a .bat file, the only way I found is executing this way:
    process.start("cmd.exe", QStringList() << "/c" << "path/to/bat/file.bat"); (this line was edited)

    The process works properly, but I am not capable to terminate the process.
    I tried with process.terminate() and process.kill().
    (It says "invalid thread identifier")

    Any idea?
    thnks!


  • Qt Champions 2016

    Hi
    But if file.bat do not contain pause or anything interactive
    are you sure its not already terminated ?

    Also the correct way is to do
    process.start("cmd.exe", QStringList() << "/c" << "path/to/bat/file.bat" );
    so each parameter is in its own index.



  • Thanks for your response @mrjj

    @mrjj said in Can't kill/terminate batch process:

    process.start("cmd.exe", QStringList() << "/c" << "path/to/bat/file.bat" );

    You are right. It is done like that, I just put it wrong in the example above. (I will edit)

    @mrjj said in Can't kill/terminate batch process:

    But if file.bat do not contain pause or anything interactive
    are you sure its not already terminated ?

    I think you are right. The batch file launches another process and that is the process I am trying to close, not the batch process itself. Lets see how can I reach that other process...

    Thanks!


  • Qt Champions 2016

    @enekow

    How do you start the second process ?
    If possible, you can return the PID from bat file and run taskkill from Qt to get rid of it depending on your
    actual use case :)



  • @mrjj said in Can't kill/terminate batch process:

    How do you start the second process ?

    I'm not very sure, since I never opened a .bat file before.
    Getting the process id would be the best way, however I'm not sure if I'm "allowed " to change that file...

    Thanks for your help!


  • Qt Champions 2016

    @enekow

    Hi. a bat file is just a text file.
    if you cant edit the bat file (change it to return pid)
    then you can see name of other process.
    taskkill can use names too.

    Is there a reason the second process must be stopped/killed ?
    it wont ever exit on its own?



  • The issue is that the .bat file together with the process it launches is an external application, and it should work as it is. (That's why I'm not "allowed" to change it)

    The second process is a database, so it should be running until I wanted it closed.


  • Qt Champions 2016

    @enekow

    If its a database server/service you want to forcefully close/kill, make sure its
    setup to not freak out :)



  • @mrjj sure! Thanks for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.