does the pid of background process show in /proc?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting does the pid of background process show in /proc?
# 1  
Old 01-28-2011
does the pid of background process show in /proc?

Hi all,
I'm reading <advanced bash scripting> and there is a example to kill a background process in a limited time,as shown below:
Code:
#! /bin/bash

#set -n

TIMEOUT=$1
count=0

hanging_jobs & {
	while ((count < TIMEOUT));do
	   eval '[ ! -d "/proc/$!" ] && ((count = TIMEOUT))'
		((count++))
		sleep 1
		echo $!
	done

	eval '[ -d "/proc/$!" ] && kill -15 $! '
}

I don't know why it use eval (is it necessary?)and i can't understand the meaning of
Code:
[ ! -d "/proc/$!" ] && ((count = TIMEOUT))

besides,the book says that /proc is where information about running process is found,but i can't find the relative directory of my background process.why?
# 2  
Old 01-28-2011
I tried that code. I set TIMEOUT to 7 and I replaced hanging_jobs with "sleep 3" and "sleep 100". The code works fine. I removed the evals and tried it again. Still works fine. While those eval's are harmless, I cannot see any reason to use them. The code
Code:
[ ! -d "/proc/$!" ] && ((count = TIMEOUT))

tests to see the the directory is missing. If so it sets count to TIMEOUT. This makes the loop stop.

Not all systems have a /proc filesystem. Maybe yours does not. This undermines to portability of that code. A more portable way to test if a process exists is to use "kill -0 $pid". This won't kill the process but it will fail the process is not there.

If anyone has any thoughts on why those eval's are useful I hope they post. They have me beat. Smilie
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 01-28-2011
Quote:
Originally Posted by Perderabo
The code
Code:
[ ! -d "/proc/$!" ] && ((count = TIMEOUT))

tests to see the the directory is missing. If so it sets count to TIMEOUT. This makes the loop stop.
thanks so much.i thought the "=" to be a equal rather than a assign operatorSmilie
Quote:
Not all systems have a /proc filesystem. Maybe yours does not. This undermines to portability of that code.
my operating system is Linux2.6.35,it has the /proc,but the relative file(directory) of running background process can't be found in it in this example.
# 4  
Old 01-28-2011
Quote:
Originally Posted by homeboy
my operating system is Linux2.6.35,it has the /proc,but the relative file(directory) of running background process can't be found in it in this example.
What did you replace hanging_jobs with? The directory won't exist if the job finishes before you check.
# 5  
Old 01-28-2011
Quote:
Originally Posted by Corona688
What did you replace hanging_jobs with? The directory won't exist if the job finishes before you check.
yeah,i use the "sleep 10" and i printed the $_ every second and during that period,i checked the /proc and didn't find it. So,in your linux,it works fine?
# 6  
Old 01-28-2011
What's this $_? The PID is $!

The evals do nothing, yes.

Could you copy-paste your script exactly as you have it? I think there must be some slight error somewhere that's throwing it off.

background processes are no more special than any other process as far as the kernel's concerned and definitely do appear in /proc/ in any linux I can think of.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 01-28-2011
Quote:
Originally Posted by Corona688
What's this $_? The PID is $!
thanks for your reply,i'm sorry i made a careless mistake here,in my script i used $!Smilie,here is my code:
Code:
  1 #! /bin/bash
  2 
  3 #set -n
  4 
  5 TIMEOUT=$1
  6 count=0
  7 
  8 sleep $TIMEOUT & {
  9    while ((count < TIMEOUT));do
 10       eval '[ ! -d "/proc/$!" ] && ((count = TIMEOUT))'
 11       ((count++))
 12       sleep 1
 13       echo $!
 14    done
 15 
 16    eval '[ -d "/proc/$!" ] && kill -15 $! '
 17 }
~

---------- Post updated at 10:05 PM ---------- Previous update was at 10:02 PM ----------

Thanks to all above,it shows up in /proc,maybe i'm too careless to find it.....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[SOLVED] Using "$!" to get the PID of the Last Ran Background Process

Hello All, I was looking into creating a script that would be used only to start a Daemon and create a lock file... F.Y.I. It's for Nagios' NRPE Daemon Plugin... Anyway when I run the command to start the Daemon (below): /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d And... (14 Replies)
Discussion started by: mrm5102
14 Replies

2. UNIX for Dummies Questions & Answers

/proc/pid/maps

I think the libc.so is shared between processes, because it is a shared library and OS is engaged for saving memory. But, below, the maps of bash, shows r-xp and r--p rw-p attributes to libc.so which mean private memory space. Can anybody explain this for me? :)cat /proc/$$/maps... (4 Replies)
Discussion started by: vistastar
4 Replies

3. Shell Programming and Scripting

Closing open file descriptors from /proc/pid/fd

Hi guys, i need to write a shell script that will close file descriptors from /proc/pid/fd will calling exec 4<&- solve the problem ? thanks in advance :) (15 Replies)
Discussion started by: alpha_romeo
15 Replies

4. Shell Programming and Scripting

Background process, return code and pid.

Hey all, Okay, this one is tricky and I'm not sure there is a niec way to do it, or indeed anyway to do it. The main issue revolves around timing out a hung ssh. I am doing this by creating a wrapper script for the ssh with the following requirements. My requirements are: Defineable... (5 Replies)
Discussion started by: RECrerar
5 Replies

5. Shell Programming and Scripting

PID from background ssh

Hello. I was wondering if someone can help me out with something. To simplify my life, I have written a tiny script to open an ssh tunnel through another linux host so that I can access the esxi hosts on that network using the client. For this I have to tunnel ports 443, 902, and 903. Here is what... (1 Reply)
Discussion started by: numetheus
1 Replies

6. Shell Programming and Scripting

kill PID running in background in for loop

Guys, can you help me in killing the process which is running in back ground under for loop I am not able to find the PID using ps -afx|grep <word in command I entered> (1 Reply)
Discussion started by: mohan_xunil
1 Replies

7. Shell Programming and Scripting

A question about the PID of a background function

Dear all, I'm writing a KornShell script that calls inside it a function in background mode #!/bin/ksh function myfunction { . . .} myfunction |& . . . How can I capture the PID of the function myfunction that runs in background? Thanks in advance :) (2 Replies)
Discussion started by: dariyoosh
2 Replies

8. UNIX for Dummies Questions & Answers

Need to get pid of a process and have to store the pid in a variable

Hi, I need to get the pid of a process and have to store the pid in a variable and i want to use this value(pid) of the variable for some process. Please can anyone tell me how to get the pid of a process and store it in a variable. please help me on this. Thanks in advance, Amudha (7 Replies)
Discussion started by: samudha
7 Replies

9. Red Hat

read maps from /proc/pid/

hi, i hav a query abt reading the contents of /proc/pid/maps file.is there any system apis or functions available to get the data from dat file and parse according to my need. i need name of the .so,Create date of the .so file.,Location of .so file etc. please provide a good source. yes i hav... (3 Replies)
Discussion started by: sanjaykhuntia
3 Replies

10. Shell Programming and Scripting

PID of process started in background??

I am having a problem getting the PID of a process I start in the background is a csh. In tcsh and sh it's simple $! give it to you But in csh this just returns Variable syntax From the man page it should work but it doesn't???? Any help. (2 Replies)
Discussion started by: stilllooking
2 Replies
Login or Register to Ask a Question