QProcess with External script that kills the main process.



  • So for my job i'm implementing an automatic update ability to the application.

    Our application versions are controlled via the RPM system. When i run the RPM update command ("rpm -U {rpm file}"), the RPM file kills the current running application process, corrects the applications files / directories / configuration, and moves the new executable and an autostart script restarts the application.

    My current implementation is the following:

    MainWindow.cpp:

    • Receives message that an update is needed
    • Waits for the application to be idle before updating
    • Once idle, sanity checks where the new code is, and that it is applicable
    • Emits a signal caught by a controller to execute the file when idle

    Controller.cpp:

    • Receives the signal for update
    • Start script to do the update.

    Controller.cpp:
    @
    //Function to call the update script
    void Controller::updateCode(){
    DEBUG("CTRL UPDATE CODE");

    /*
    system("/home/app/updateScript.sh");
    */

    QProcess *process = new QProcess(this);
    
    QString file = "/home/app/updateScript.sh";
    

    // bool x = process->startDetached(file, QStringList());
    // bool x = process->startDetached(file);
    int x = process->execute(file);

    DEBUG("START (%d)", x);
    
    QObject::connect(process, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
    
    DEBUG("PROCESS STATE (%d)", process->state());
    

    }
    @

    updateScript.sh:

    @
    if [ ! -d "/home/app/installedCode" ];
    then
    echo "Creating /home/app/installedCode directory"
    mkdir /home/app/installedCode
    fi

    FILE="$(ls -G /home/app/newCode/)"
    echo $FILE

    if [ -f "/home/app/newCode/$FILE" ];
    then

        MYPATH="$(readlink -f newCode/$FILE)"
    
        echo $MYPATH
    
        eval "rpm -U " $MYPATH
    
        mv "/home/app/newCode/$FILE" "/home/app/installedCode/"
    

    else
    echo "No file to update"
    fi
    @

    Here is the log for the currently attempted process:
    @
    01-19 12:31:07.079 - [controller.cpp:2240] - Simulating SC Update Message
    01-19 12:31:07.079 - [mainwindow.cpp:521] - Got request to update code. Waiting for idle
    01-19 12:31:10.978 - [mainwindow.cpp:514] - System optimal for update
    01-19 12:31:10.979 - [mainwindow.cpp:477] - System idle. Updating code
    01-19 12:31:10.979 - [controller.cpp:1211] - CTRL UPDATE CODE
    01-19 12:31:11.163 - [controller.cpp:1223] - START (0)
    01-19 12:31:11.163 - [controller.cpp:1227] - PROCESS STATE (0)
    @

    The .rpm file is moved from the newCode/ directory to the installedCode/ directory, so i know the script is getting called. But the running application is not getting killed like it should be.

    If i call the updateScript.sh file from the command line on the unit instead of the application, it does exactly what i expect.

    I also tried the process->startDetached() function as you can see where it is commented out. It does the same thing that execute() does.

    Any help is appreciated, thanks!


  • Moderators

    Hi,

    If I understood you correctly, you can emit a custom signal from Controller.cpp once the process completes, catch it in MainWindow.cpp and call close() of MainWindow.cpp.


  • Lifetime Qt Champion

    Hi,

    Depending on the application connecting to QApplication::quit will ensure that the application is closing. The quitOnLastWindowClosed flag might be unset so closing windows might not be enough


Log in to reply
 

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