LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-22-2012, 03:10 PM   #1
kritikool
LQ Newbie
 
Registered: Oct 2012
Distribution: redhat
Posts: 3

Rep: Reputation: Disabled
To check if process is complete or not


Hello,
I have a variable "pid1" in which I have stored the process id of a job that I have run in the background.

I would like to check if this "pid1" is complete or not, so that I can execute the next process.

Any ideas ?

Thanks in advance !
 
Old 10-22-2012, 03:19 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Try the wait built-in:
Code:
wait $pid1
should wait for the job completion and return its exit status.
 
Old 10-22-2012, 03:22 PM   #3
kritikool
LQ Newbie
 
Registered: Oct 2012
Distribution: redhat
Posts: 3

Original Poster
Rep: Reputation: Disabled
I have tried wait - the problem with "wait" is that sometimes i have to wait forever.
I believe when the process completes, it status will be 0 ? If there is some way to check if the status of the process is 0, I think that might work.
 
Old 10-22-2012, 03:25 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Code:
wim@i3-2120:~$ ps --pid 9999
  PID TTY          TIME CMD
wim@i3-2120:~$ echo $?
1
wim@i3-2120:~$ ps --pid 2777
  PID TTY          TIME CMD
 2777 pts/0    00:00:00 bash
wim@i3-2120:~$ echo $?
0
wim@i3-2120:~$
In the above examples, ps will return 0 if a pid was found and 1 if not.
 
1 members found this post helpful.
Old 10-22-2012, 03:26 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Nope. The exit status tells if the command was sucessfull or not. An exit status of 0 means "success", 1 or any other value means the command was unsuccessful. I would investigate why the command doesn't terminate: maybe an endless loop or a zombie?
 
Old 10-22-2012, 03:30 PM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by kritikool View Post
I have tried wait - the problem with "wait" is that sometimes i have to wait forever.
I believe when the process completes, it status will be 0 ? If there is some way to check if the status of the process is 0, I think that might work.
Status returned by the process usually depends on the success of the process. I'm not sure if the result of a background process can be checked.

And it will not work; the status is only returned once the process is completed so you might wait forever as well
 
Old 10-22-2012, 03:59 PM   #7
kritikool
LQ Newbie
 
Registered: Oct 2012
Distribution: redhat
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks much for your responses. I tried both ways - it looks like I have no choice but to wait. If I only check whether the process is complete or not, it does not wait for my process to complete.

As a follow up question. If I write code:

wait $pid1

Does it mean that the program will wait till this process is complete before moving to the next line in code ?
 
Old 10-22-2012, 04:33 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by kritikool View Post
If I only check whether the process is complete or not, it does not wait for my process to complete.
You have to insert it in a loop and use the sleep command to wait, e.g.
Code:
while ps -p $pid1 > /dev/null
do
  sleep 300
done
Quote:
Originally Posted by kritikool View Post
Does it mean that the program will wait till this process is complete before moving to the next line in code ?
Exactly.
 
  


Reply


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
Script to check PID from file and check whether process is running or not rajkiran183 Linux - Newbie 5 10-19-2012 12:28 PM
perl- check size to see if file is complete michael.barnes Programming 2 02-13-2008 07:20 PM
RHEL7 Cannot complete boot process Mithuna Red Hat 1 12-09-2007 02:21 PM
Perl to get the complete process info for all process alix123 Programming 4 08-16-2007 02:36 AM
nohup process dosen't complete JeffCrew Linux - Newbie 1 10-13-2005 11:38 PM


All times are GMT -5. The time now is 09:05 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration