LinuxQuestions.org
Help answer threads with 0 replies.
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: 48

Rep: Reputation: 16
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
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,732

Rep: Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975
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: 48

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


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


All times are GMT -5. The time now is 01:44 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration