Finding PID of a process using variable substituition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding PID of a process using variable substituition
# 1  
Old 03-11-2009
Question 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.

Code:
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 "File$i" is not found. Am i right? Is there a way to find the PID of a process using variable substituition? If not, how it cud be achieved?

FYI,I got the PID of FIle0 or FIle1 if i use its name specifically like,

Code:
mypid=`ps aux | awk '/File0/ && !/awk/ { print $2 }'`
echo PID is $mypid

Thanks,
Amio
# 2  
Old 03-11-2009
Quote:
Originally Posted by amio
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.

Code:
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 "File$i" is not found. Am i right? Is there a way to find the PID of a process using variable substituition? If not, how it cud be achieved?

FYI,I got the PID of FIle0 or FIle1 if i use its name specifically like,

Code:
mypid=`ps aux | awk '/File0/ && !/awk/ { print $2 }'`
echo PID is $mypid

Thanks,
Amio
try

ps aux | awk '/File$i/ && !/awk/ { print $2 }' & mypid=$!
echo mypid
# 3  
Old 03-11-2009
To use the Shell variables, use "-v i.e. Variable" option in awk.

Do this:
fname=File$i
mypid=`ps aux | awk -v var="$fname" 'BEGIN /var/ && !/awk/ { print $2 }'`

Note, that your shell variable is fname...which the filename
and the corresponding awk variable is "var"
Hope this helps.
# 4  
Old 03-12-2009
Thanks friends for ur replies.

amio.
# 5  
Old 03-12-2009
Quote:
Originally Posted by angad.makkar
To use the Shell variables, use "-v i.e. Variable" option in awk.

Do this:
fname=File$i
mypid=`ps aux | awk -v var="$fname" 'BEGIN /var/ && !/awk/ { print $2 }'`

Note, that your shell variable is fname...which the filename
and the corresponding awk variable is "var"
Hope this helps.
Have you tried what you've suggested?
Code:
fname=File$i
mypid=`ps aux | awk -v var="$fname" '$0 ~ var && !/awk/ { print $2 }'`

# 6  
Old 03-12-2009
ps aux | awk '/File$i/ && !/awk/ { print $2 }' & mypid=$!
echo mypid

worked for me..
Thanks
# 7  
Old 03-12-2009
mypid=`ps -w aux | awk '/'"$myvar"'/ && !/awk/ { print $2 }'`
echo PID is $mypid

It also gives the PID using variable subtituition..
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with finding the exit status of a 'nohup' command using 'PID'.

Hello All, I need to run a set of scripts, say 50 of them, parallely. I'm running these 50 scripts, from inside a script with the help of 'nohup' command. 1.The fifty scripts are stored in a separate file. 2.In a master script, i'm reading every line of the file through loop and executing... (4 Replies)
Discussion started by: SriRamKrish
4 Replies

2. Shell Programming and Scripting

Error in finding the PID by grep and assigning to a variable

Hello All, I am facing difficulty in getting the PID value & then assigning it to a variable, kindly help me in resolving the issue. Thanks a lot in advance. The custom utility used inside the Test2.sh will process the file in a batch of 10 lines at once and for efficient memory management,... (3 Replies)
Discussion started by: duddukuri
3 Replies

3. Shell Programming and Scripting

Quick question about finding the PID of long-running processes

The end result that I'd like is to terminate any process on my ps -u username list that extends beyond 20 minutes. I know for a fact that this process will be named l.exe, but I don't know the number in between and I won't know the PID. Is there a way to use grep or pidof to do this task every 20... (2 Replies)
Discussion started by: Bolanok
2 Replies

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

5. Solaris

Finding PID of Killing process

Say I have 2 processes(perl scripts on Solaris machine) A and B. the process A kill the process B. While in the process B how do I print the PID of the process that Killed it(process A) before dieing. My process A looks like open(STATS, "ps -ef|"); while ($inputLine = <STATS>) { if... (7 Replies)
Discussion started by: enigma_007
7 Replies

6. Shell Programming and Scripting

Finding the nice(ni) number with PID?

Hi, is there a command that takes the PID of a process and that only diplays it's ni number? I`m pretty sure it would require pipes but I tried a few things that ended up miserably... Since the ps command doesn't show the ni unless I do ps -o ni but then I can't find a way to search the right... (2 Replies)
Discussion started by: Yakuzan
2 Replies

7. Shell Programming and Scripting

Get an PID of particular process

Hi I have written a shell script to find and kill the particular process. Here in shell script i have written the code like cnt = $(ps -ef | grep Shree) echo $cnt I am getting the output root 2326 2317 0 14:39:46 pts/1 0:28 Shree -f fdc.fbconf FDCapp.fbapp Here I want to... (2 Replies)
Discussion started by: Shreedhar Naik
2 Replies

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

9. Solaris

getting pid of process

hi all, Is there a simple script anyone could through out to me, to find the pid of a process given the name. I actually need to bind this pid to a processor set. I would probably put these comamns in a shell script which would have. a) kick start the executable b) get the pid c) bind it to a... (10 Replies)
Discussion started by: Naanu
10 Replies

10. Filesystems, Disks and Memory

finding out the pid for a busy text file

Can some one please tell me how to find out the proccess ID that is holding up a file. I am attempting to remove a file and I am getting a message stating that it is busy. i.e rm filename filename: 777 mode ? (y/n) y rm: filename not removed. Text file busy Thanks in advance. (1 Reply)
Discussion started by: jxh461
1 Replies
Login or Register to Ask a Question