Visit The New, Modern Unix Linux Community


How to get the process id in to variable?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to get the process id in to variable?
# 1  
Data How to get the process id in to variable?

Dear Experts,

Pl help me to fix this

i am trying to unmount the all the file systems in VG, Once unmounted vary off the VG.

I have written the script to do that it works fine when none of user or process id holds it but if in use it says resource is busy and can't unmount.
even i used fuser -kxus fs in the if statement but i am not able to can anybody help to how to assign the process ids holded by filesystem to a variable i am able to fetch the process id using "fuser -c <filesystem> it shows ids like below

Code:
<filesystem>:  1900636c 3207268c

how can i get only process id in to variable(i.e 1900636 3207268)

so i can add line script to kill process ids

Here it is script which i wrote to do the things.


Code:
lsvg -l `lsvg -o | grep -v "rootvg" ` | awk '{ print $7 }' | grep -vE "(^$|LV|N/A)" | sort -r > ${LOGFLDR}/tmp_vg.out
{
        for i in `cat ${LOGFLDR}/tmp_vg.out`
                do
                        umount -f $i
                    if [ $? -ne 0 ]
                       then
                        fuser -kxuc $i
                        umount -f $i
                    fi
                done
        for i in `lsvg -o | grep -v "rootvg"`
                do
                        varyoffvg $i
               done
} 1>${LOGFLDR}/FS-status_daily.log 2>&1
if [ $? -eq 0 ]
then
d=`date +"%d-%m-%Y %H:%M"`
echo "Succefully Completed Execution of flashunmount.bash at $d "
unset d
exit 0
else
echo "Error Occured during the execution of flashunmount.bash. Refer the log file created in "${LOGFLDR}""
fi
exit

Waiting for u u suggestion

Regards,
Madhu

Last edited by pludi; 07-31-2010 at 12:38 PM.. Reason: code tags, please...
# 2  
Things are going to break all over the place here when the amount of data starts getting large. You shouldn't be cramming argument lists into backticks when you can possibly avoid it -- read arguments instead, from files or pipes. This also avoids needing to run cat at all, preventing much wasted CPU and I/O time. See Useless Use of Cat and Useless Use of Backticks.

Code:
# Save this instead of running lsvg over and over
lsvg -o | grep -v "rootvg" > /tmp/$$-lsvg
# "xargs command < filename" roughly equivalent to command `cat filename` but safer 
xargs lsvg -l < /tmp/$$-lsvg | awk '{ print $7 }' | grep -vE "(^$|LV|N/A)" | sort -r > ${LOGFLDR}/tmp_vg.out
{
        while read i
                do
                        umount -f $i
                    if [ $? -ne 0 ]
                       then
                        fuser -kxuc $i
                        umount -f $i
                    fi
                done < ${LOGFLDR}/tmp_vg.out

        while read i
                do
                        varyoffvg $i
               done < /tmp/$$-lsvg

        rm -f /tmp/$$-lsvg
} 1>${LOGFLDR}/FS-status_daily.log 2>&1
if [ $? -eq 0 ]
then
d=`date +"%d-%m-%Y %H:%M"`
echo "Succefully Completed Execution of flashunmount.bash at $d "
unset d
exit 0
else
echo "Error Occured during the execution of flashunmount.bash. Refer the log file created in "${LOGFLDR}""
fi
exit

As for reading the PIDs, from man fuser:
Code:
       fuser  outputs  only  the  PIDs  to  stdout, everything else is sent to
       stderr.

...which makes this a lot easier.

In BASH:

Code:
# /tmp/$$-fuser is just a temporary file.  Use whatever name you please.
fuser -c /filesystem 2> /dev/null > /tmp/$$
while read -d ' ' P
do
        echo "PID is $P"
done < /tmp/$$-fuser
rm -f /tmp/$$-fuser


Last edited by Corona688; 07-31-2010 at 03:42 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Thanks Corona688 it is working..

i was trying to assign the fuser -c <file system > command output to variable like below.

a=`fuser -c <file system> and i was not able to separate the charecter c and the filesystem name. now able to do that. Thanks once again ..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #938
Difficulty: Medium
The ping utility was written by Mike Muuss in December 1983.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses: Hello $name How are you? The perl script: $name = "David"; open(HEADER,"hello.htm"); while(<HEADER>) { $html .= $_; } close(HEADER); print "$html";I making something about template. But it can't process the $name variable. (4 Replies)
Discussion started by: natong
4 Replies

2. Shell Programming and Scripting

Export variable, as a background process

Have a script where I need to wait for a process to run, but from that process I want to capture the row count. Script I have so far (easier than explanation): echo "Start" export NUMROWS=`td "select * from dbc.database" 2> /dev/null | grep "Query completed" | sed -e 's/.*Query... (7 Replies)
Discussion started by: Cranie
7 Replies

3. Shell Programming and Scripting

Get PID of a process into a variable

Hi All, I need to get the PID of a process which i ran in background into a variable echo $! gives me the PID of last background process but how to get this into a variable so that i can use "wait" command later in the script to make sure that this background process was done var = `echo... (5 Replies)
Discussion started by: firestar
5 Replies

4. Shell Programming and Scripting

Capture Process with highest CPU% in Variable

I am trying to capture the process on a red hat linux system with the highest cpu utilization into a variable. I have been playing with ps command but really do not think I am making the correct progress. For instance: top - 09:01:16 up 63 days, 18:29, 2 users, load average: 0.39, 0.49, 0.48... (5 Replies)
Discussion started by: jaysunn
5 Replies

5. Shell Programming and Scripting

Reading a file with while into variable and using in another process

I would appreciate some help, please. I have a file cutshellb.txt, which contains four report names (but it could be any number from 1 to x. The report suffix is not necessarily consecutive: report3785 report3786 report3787 report3788 I have a shell script which attempts to read the... (3 Replies)
Discussion started by: Dicloflex
3 Replies

6. UNIX for Advanced & Expert Users

send a new value to a variable in a running background process

Hi guys, I have a issue with a background process, I need to update the value of a variable in that process which is running at this time and it will be running for at least 2 days. Any idea? I will apreciate your help. regards. Razziel. (2 Replies)
Discussion started by: razziel
2 Replies

7. Shell Programming and Scripting

Finding PID of a process using variable substituition

Hi All, Am copying mulitple files in a directory in names File0,File1,File2 etc. I need to print separately the PID of these copies using File names. for((i=0;i<5;i++)) do mypid=`ps aux | awk '/File$i/ && !/awk/ { print $2 }'` echo PID is $mypid done It printed nothing. Thinking... (6 Replies)
Discussion started by: amio
6 Replies

8. Shell Programming and Scripting

How to export a variable from a child process running in background to the parent

Hi All, I have a script which calls a child script with a parameter to be run in the background . childscript.ksh $a & Can any one suggest me how do i export a variable from the child script to parent script? Note that the child script is in background If the child script is in... (3 Replies)
Discussion started by: aixjadoo
3 Replies

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

10. UNIX for Dummies Questions & Answers

Striping process variable

Hello. I would like to strip out the process name from the ${0##*\} variable. So that if I have a script named "mytest.script" the variable would be populated with just "mytest" I have tried doing a substring but it doesnt seem to work. name=`expr match "${0}" : './*\(/*.\)'` Doing this... (4 Replies)
Discussion started by: new9021
4 Replies

Featured Tech Videos