fuser - how to select the given PID? (awk/sed)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting fuser - how to select the given PID? (awk/sed)
# 1  
Old 10-29-2010
fuser - how to select the given PID? (awk/sed)

Hello,

I'm trying to select, or well put the PID that is given by fuser to a var.. It has a wierd format and I somehow can't get it working, any awk/sed experts about?

Code:
PID=`fuser $file | awk/sed....?`
if [ "$PID" != "" ]; then
kill $PID
fi

Greetings and thanks for all your awesome help in advance!
# 2  
Old 10-29-2010
Code:
kill $(fuser $file)

just works, at least on Solaris which created the fuser command. Don't be fooled by fuser output format. It splits its output between stdout and stderr.
# 3  
Old 10-29-2010
Further to jlliagre. There is much variation in "fuser" behaviour.

For a sample file or device which is open for write by more than one process, please post:
Code:
fuser filename 1>/dev/null
fuser filename 2>/dev/null

Concidentally on many O/S /dev/null is open by multiple processes. We could run fuser on this device to get some test output.
Obviously this is not a good device to test a kill script.

Last edited by methyl; 10-29-2010 at 08:24 PM.. Reason: layout
# 4  
Old 11-01-2010
A number of OS also support the -k option to terminate process found. Would that be neater?
Code:
# fuser -k $target_file

It can be dangerous thought to automate killing processes. I have managed to kill process 1 on one occassion. I had trace running to the screen in advance, then after issuing the kill -9 1, the console went very quiet and the phone starting ringing.Smilie



Robin
Blackburn/Liverpool
UK

Last edited by rbatte1; 11-01-2010 at 10:34 AM.. Reason: Example corrected
# 5  
Old 11-01-2010
That would be
Code:
fuser -k -s TERM $file

to avoid SIGKILL.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to get pid from fuser

bash-3.2$ fuser -f /bin/nohup.out /bin/nohup.out: 13136o 13111o The pid is 13136. Can you tell me how can i extract just the pid 13136 from the above output ? bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v I was trying on this lines but i get strange... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. 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

3. Shell Programming and Scripting

Using awk to select one field

Hi, I saw your post.. I have a dought in awk command... how to get the output from a file. i need a first column in etc/passwd file in a single column (in indivijual line)... i couldn't get with this command cat /etc/passwd | awk -F ":" '{printf $1}' Kindly help This thread was created... (3 Replies)
Discussion started by: Dheepak s
3 Replies

4. Shell Programming and Scripting

Sed for select and retrieve data

I would like to recover the data from 3 text tags. These three markers are located between the tags specific location <tag1> and </tag1> knowing that they are in many places. In File.txt: <tag2>txt2</tag2> <tag3>txt3</tag3> <tag4>txt4</tag4> .... <tag1> <tag2>txt2</tag2>... (3 Replies)
Discussion started by: Amad
3 Replies

5. UNIX for Dummies Questions & Answers

Turning to SED to select specific records

Hi All, I am looking for a simple concise solution most likely using sed to process the following 4 rows of data from the same record and only keeps it if the second record satisfy certain critea such as surname matches up to smith or jackson: John (firstname) Smith (surname) ... (21 Replies)
Discussion started by: gjackson123
21 Replies

6. Shell Programming and Scripting

awk PID

Can any one tell me the meaning of the following.. I am still learnig unix Thanks in advance 1) if ($1 == "tommCumulative)") { if (PID == 7) { printf(",CUM_READ = to_number(%d,'XXXXXXXXXX')\n",$2 * SC) } else { printf (",CUM_READ =... (4 Replies)
Discussion started by: raopatwari
4 Replies

7. Shell Programming and Scripting

Select ip and date using sed or gawk

1.56.253.48 - - "GET " 1.6.253.48 - - "GET " 1.65.253.48 - - "GET " 1.65.253.48 - - "GET " 1.63.53.48 - - "GET " 1.65.253.48 - - "GET " 1.16.23.48 - - "GET " 1.64.25.48 - - "GET " need command which give the output 1.6.253.48 - 09/Nov/2009:07:02:24 1.65.253.48 -... (7 Replies)
Discussion started by: sagar_evc
7 Replies

8. Shell Programming and Scripting

Output of ps + awk in a variable to get PID

Hi All, I am getting the PID of a process using its name using the following command in a script mypid=`ps aux | awk '/test5/ && !/awk/ { print $2 }'` echo PID is $mypid The problem is the PID is not getting printed. But when i run the command directly in shell, the PID got printed. ... (2 Replies)
Discussion started by: amio
2 Replies

9. Shell Programming and Scripting

SED scripting select

Say I have a file 'example.txt' with these lines of code in it: hello:anddasd:cheese:gerg whatever:sdadsa:asdfasdfa:wwew hmmmm:something:gfhfhgf:sdasdas Question: 1. How would I write a script which is able to take all the words before the first ':'? 2. How would I write a script which is... (6 Replies)
Discussion started by: i_am_a_robot
6 Replies

10. UNIX for Dummies Questions & Answers

Session PID & socket connection pid

1. If I use an software application(which connects to the database in the server) in my local pc, how many PID should be registered? Would there be PID for the session and another PID for socket connection? 2. I noticed (through netstat) that when I logged in using the my software application,... (1 Reply)
Discussion started by: pcx26
1 Replies
Login or Register to Ask a Question