How to fetch PIDs from the fuser command output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to fetch PIDs from the fuser command output
# 1  
Old 03-25-2010
How to fetch PIDs from the fuser command output

All,

I have to have my script wait for another script when it is being executed. I used
Code:
fuser -f scriptname

to get the PID of the script but when passed the PID to wait command, the command does not accept it as a PID.

Below is the example:

my.script1:
Code:
fuser_result=`fuser -f my.script2`
echo " PID using my.file is : $fuser_result"
wait $fuser_result
echo " Script2 Complete. Now continue processing"

Before I ran the script, I made sure my.script2 is open and being executed
my.script1 does not wait for my.script2 at all. The last echo statement runs immediately after the wait statement.

Also the echo PID statement gives me outputs like below:

I run:
Code:
 PID using my.file is : 3436698

II run:
Code:
 PID using my.file is : 3436698 942486

III run:
Code:
 PID using my.file is : 19734589

From this its clear that the wait command did not see the PIDs from fuser command as PIDs. Is there any way to format the same so that I can make my wait command work?

Also as shown in II run sometimes, I get more than 1 PID from fuser. Let me know how to handle those kind of situations too!

Let me know for any questions.

Thanks
Bharath
# 2  
Old 03-25-2010
You can wait your child process.
# 3  
Old 03-25-2010
How do I get the Child Processes from the fuser output. I have to read the output and store the same into a variable which I can wait on. How do I do that?
# 4  
Old 03-25-2010
You can only use the "wait" command for a process which your current shell set off into background.
See "man wait" (even though "wait" is usually a shell builtin).


Code:
#!/bin/ksh
# background script2 and wait
nohup script2 & ; MYPID=$!
wait ${MYPID} ; echo "Finished: ${MYPID}"

# 5  
Old 03-26-2010
Wait is for children (your children)

Hi, bharath.gct,

The actual problem is, as you may have already guessed, not
to discover the pids, whether they are children or not (by the way,
with a single exception, every process is a child to another one.)

The actual problem is that YOU CAN'T wait (thru the "wait" command
in shell, or the wait(2) system call in a binary program) for a process
which was not launched by you (your shell or your program).

Wait can only be used with children processes that you directly
spawned (actually, fork(2)'d) - like when your shell launches a
command to the background, via "&".

So, if the process you want to wait for is not of your own, direct,
"breed", sorry, you'll have to figure out some other way to do it.
"Wait" wasn't designed for that, only for actual children, and not for
siblings, cousins, nephews, grandsons, mothers-in-law (or total
strangers) kind of processes. Smilie

One way could be, once you discover the pid, keep checking, say, every
1 or 5 seconds, ps output until the process disappears... not very efficient,
but effective.

Hope this helps.

Mario.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fetch Command in UNIX

Hi there, What is the equivalent of fetch command in Ubuntu/Cygwin. My purpose is to modify the code in such a way that I can use fetch. #!/bin/sh #============================================================================ # $Id: ios-http-auth.sh,v 1.1 2001/06/29 00:59:44 root Exp root... (1 Reply)
Discussion started by: alvinoo
1 Replies

2. Shell Programming and Scripting

Insert title as output of command to appended file if no output from command

I am using UNIX to create a script on our system. I have setup my commands to append their output to an outage file. However, some of the commands return no output and so I would like something to take their place. What I need The following command is placed at the prompt: TICLI... (4 Replies)
Discussion started by: jbrass
4 Replies

3. UNIX for Advanced & Expert Users

Fuser alternative OR running fuser on a script

Hi, Not sure whether there is a fuser alternative or any better way to check for file in use or not. I am wanting to check whether files are in use or not before removing them. Using fuser, the awk seems to be giving me 'weird' output not to mention that it is giving me 2 lines instead of... (0 Replies)
Discussion started by: newbie_01
0 Replies

4. Solaris

Difference between fuser and lsof command

Hi, Can anyone explain me the difference between fsuer and lsof commands. As per my knowledge both the commands are used to find the processes used by the current file system or user. Apart from that what is the major difference between these commands (3 Replies)
Discussion started by: rogerben
3 Replies

5. Shell Programming and Scripting

fetch last line no form file which is match with specific pattern by grep command

Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ... (13 Replies)
Discussion started by: Himanshu_soni
13 Replies

6. Shell Programming and Scripting

Unable to fetch the output using AWK

Hi all, From the below file I need to fetch the data in the below output format. ToolInstanceID "diw_dm_sales" Identifier "Sales_source_load" Promt Default ParamType ParamLength ParamScale "Database_Name" "ORCL" "0" "0" "0" Identifier "retail_source_load" Promt Default... (4 Replies)
Discussion started by: onesuri
4 Replies

7. Shell Programming and Scripting

awk getline to fetch output

Input File: CPU minf mjf xcal intr ithr csw icsw migr smtx srw syscl usr sys wt idl 0 37 0 594 2205 1174 123 8 6 36 0 1954 6 8 0 86 1 1 0 49 26 0 44 3 2 6 0 624 2 1 0 97 2 0 0 5 5 0 8 1 0 1 ... (3 Replies)
Discussion started by: bhallarandeep
3 Replies

8. UNIX for Dummies Questions & Answers

Command display output on console and simultaneously save the command and its output

Hi folks, Please advise which command/command line shall I run; 1) to display the command and its output on console 2) simultaneous to save the command and its output on a file I tried tee command as follows; $ ps aux | grep mysql | tee /path/to/output.txt It displayed the... (7 Replies)
Discussion started by: satimis
7 Replies

9. Shell Programming and Scripting

help needed in fuser command

Hi all, I want to know if fuser command can be used to check if a file is being written or not??? Thanks In Advance Anju (1 Reply)
Discussion started by: anju
1 Replies

10. UNIX for Advanced & Expert Users

STDOUT redirect to a FILE, when fuser command is used !!

Hi all, I have the following script: ------------------------------------------------- #SCRIPT TO CHECK WHO HAS ACCESSED THE LOG/FILE IN PAST 'N' MINUTES, AND MAIL ACCORDINGLY. MYPATH="/clocal/mqbrkrs/user/mqsiadm/sanjay/" MAIL_RECIPIENTS="vg517@dcx.com" Subject="File accessed in last... (6 Replies)
Discussion started by: varungupta
6 Replies
Login or Register to Ask a Question