LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2012, 08:53 PM   #1
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Rep: Reputation: 17
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
 
Old 03-31-2012, 10:49 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
Old 03-31-2012, 11:38 PM   #3
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Original Poster
Rep: Reputation: 17
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.
 
Old 04-26-2015, 02:44 PM   #4
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Original Poster
Rep: Reputation: 17
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.
 
  


Reply

Tags
pid


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I assign a Command to a specific PID so I can kill a predetermined PID johnmccarthy Linux - Newbie 1 11-03-2011 08:41 AM
scripting, perl or bash; run a background process, get pid 2ck Programming 6 04-02-2010 12:16 AM
Newbie (Send PID to background problem) Phonon Linux - Newbie 3 03-12-2005 10:50 PM
/var/run/[XXX].pid - Tcl pid code liguorir Linux - Software 1 05-20-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration