Visit The New, Modern Unix Linux Community

Top Forums UNIX for Advanced & Expert Users How to get the process id in to variable? Post 302441525 by Corona688 on Saturday 31st of July 2010 02:32:04 PM
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:
 
Test Your Knowledge in Computers #480
Difficulty: Easy
Memory addresses are represented using hexadecimal, which is a base-8 counting system.
True or False?

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

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

Featured Tech Videos

All times are GMT -4. The time now is 01:25 AM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy