LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Getting the last background pid (https://www.linuxquestions.org/questions/linux-newbie-8/getting-the-last-background-pid-937486/)

Fabio Paolini 03-31-2012 08:53 PM

Getting the last background pid
 
Hi, I know that it is possible to get the last background pid using the $! command, however when I use it in my QT application I got a wrong result. Below follows an example where this procedure works
Code:

[linux@localhost ~]$ (sleep 6 && echo "A test") &
[1] 3312
[linux@localhost ~]$ echo $!
3312
[linux@localhost ~]$ A test

Now my Qt application, where I got a wrong result:
Code:

./dbsync &>/dev/null &
[1] 5047
[linux@localhost comm5]$ echo $!
5047

but using the ps command it is possible to verify that the pid related to the right process is not the 5047 but 5050.
Generally it is the process that comes with the command
Code:

echo $!
added of two.

I use QCoreApplication in my program. I do not know if it has something to do with this error.

Any advice is welcome

suicidaleggroll 03-31-2012 10:49 PM

Quote:

but using the ps command it is possible to verify that the pid related to the right process is not the 5047 but 5050.
What is the "right process"? Is it not dbsync? That's the process with a PID of 5047. If it's another process you need (one that is being called by dbsync), that will have a different PID.

Fabio Paolini 03-31-2012 11:38 PM

The dbsync is the right process.
This time a better log is shown below. Probably de program calls another one, but I cannot see how it is done.
Below is the log
Code:

[linux@localhost ~]$ /home/linux/comm5/dbsync  &> /dev/null  &
[1] 5313
[linux@localhost ~]$ ps aux|grep dbsync |grep -v grep
linux    5316 12.3  1.0 119768 11072 ?        Sl  01:26  0:00 /home/linux/comm5/dbsync
[1]+  concluded              /home/linux/comm5/dbsync &>/dev/null
[linux@localhost ~]$

See that the code that would return from "echo $!" is 5313, put the code that comes from a call to the process is 5316.
At this time the 5313 does not exist any more.

Below the part of the program where it is started
Code:

class Service : public QtService<QCoreApplication>
{
public:
        Service( int argc, char** argv ) : QtService<QCoreApplication>(argc, argv, "Program")
        {
                setServiceDescription(("Name of the program"));
                setServiceFlags(QtServiceBase::Default);
        }

protected:
        void start()
        {
               
                quint16 listenPort = Settings::instance()->value("server/listen", xxxx).toUInt();
                if ( !listener.listen( QHostAddress::Any, listenPort ) )
                        applog.out(QString("Error to listen port %1").arg(listenPort), Logfile::ERROR );

        }

This code is called server instead of dbsync, but it has the same problem.

Fabio Paolini 04-26-2015 02:44 PM

More
 
Hi, I had already given up this problem, but recently I've got a similar problem and so I returned to think about it. I got some advances although I am far from really solving all my doubts in this issue. It follows below some considerations.

The main method to be studied is
Code:

QtServiceBase::exec()
that we may see details here
http://doc.qt.digia.com/solutions/4/...base.html#exec

When this method is called without arguments it calls the method
Code:


char* env = getenv("QTSERVICE_RUN")

If
Code:

getenv(char*)
returns a null pointer then the method
Code:

QProcess::startDetached(const QString)
is called which create a new process and the first one is killed. On the other hand if
Code:

getenv(char*)
does not return a null pointer then the method
Code:

int QtServiceBasePrivate::run(bool asService, const QStringList &argList)
is called and the service realy starts.

If someone could explains a bit better how the QtService class works or point some link I would thanks.
Nowadays I am using a Debian distribution.


All times are GMT -5. The time now is 09:19 AM.