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

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

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

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.

# 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:

       fuser  outputs  only  the  PIDs  to  stdout, everything else is sent to
       stderr.

...which makes this a lot easier.

In BASH:

# /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
1 Like

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