Get PID of a process into a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get PID of a process into a variable
# 1  
Old 08-11-2010
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

Code:
var = `echo $!`
echo $var

this is not working Smilie

could some body please help
thanks in advance
firestar

Last edited by Scott; 08-11-2010 at 06:08 PM.. Reason: Please use code tags
# 2  
Old 08-11-2010
Hi.

It would work if you removed the spaces around the =

Code:
var=`echo $!`
echo $var

But you could just say:
Code:
var=$!

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-11-2010
Quote:
Originally Posted by scottn
Hi.

It would work if you removed the spaces around the =

Code:
var=`echo $!`
echo $var

I think it won't work until you put it like that:
Code:
var=`echo ${!}`
echo $var

Couse without "{}" it tries to use ! as bash command. Anyway your second solution is much better Smilie
# 4  
Old 08-11-2010
Quote:
Originally Posted by bartus11
I think it won't work until you put it like that:
Code:
var=`echo ${!}`
echo $var

Couse without "{}" it tries to use ! as bash command. Anyway your second solution is much better Smilie
Hmm. Interesting, but it seems to work fine:

Code:
$ bash
$ cat Slp
sleep 3 &
var=`echo $!`
echo var is $var 
ps -ef | grep [s]leep


$ ./Slp
var is 77811
  503 77811 77810   0   0:00.00 ttys001    0:00.00 sleep 3

(but, yes, the command sub isn't neccessary anyway)
# 5  
Old 08-11-2010
Hmmm, maybe it's because I tried that on the command line Smilie so I got something like "bash: !`: event not found". But it seems that it's working as a part of a script.
# 6  
Old 08-11-2010
Quote:
Originally Posted by bartus11
Hmmm, maybe it's because I tried that on the command line Smilie so I got something like "bash: !`: event not found". But it seems that it's working as a part of a script.
Smilie you're right.

That's the kind of thing I'd expect from the C-Shell, not bash Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How to get process name from PID?

HI, i used ps -ef | grep 3539052 | grep -v grep and i got a output like ths root 3539052 3407918 0 May 07 - 709:31 /usr/sbin/syslogd but what i need is instead of full path /usr/sbin/syslogd i want only the process name that is 'syslogd' here. (3 Replies)
Discussion started by: sumanthupar
3 Replies

2. Shell Programming and Scripting

Getting child process id for a given PID

HI Am trying to get child process id for a PID using ksh.. ps -ef | grep xntpd root 3342472 2228308 0 12:17:40 - 0:00 /usr/sbin/xntpd root 4522024 6488316 0 12:18:56 pts/0 0:00 grep xntpd root 6291614 3342472 0 12:17:40 - 0:00 /usr/sbin/xntpd Here now i... (1 Reply)
Discussion started by: Priya Amaresh
1 Replies

3. Programming

[C] Process pid by name

Hi I use linux OS. I've already written a function that allow me to get the process name by pid. (searching in /proc). Now I'd like to perform the inverse task.I mean get the process pid by its name. I could write a function that search in every folder in /proc for the process name, but i... (2 Replies)
Discussion started by: Dedalus
2 Replies

4. Shell Programming and Scripting

getting pid from a process

Hi all, i was able to redirect pid of process to a file in the following way ps aux|awk '$11 == "/Applications/ProjectX/DServer" >> /Applications/ProjectX/DServer.pid it works fine but if one folder name caontains space its not working like below ps aux|awk '$11 == "/Applications/Project\... (1 Reply)
Discussion started by: kirankumars
1 Replies

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

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

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

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

9. UNIX for Advanced & Expert Users

killing a process pid

What option is used with kill to cause the server to reread its config file. (16 Replies)
Discussion started by: jo calamine
16 Replies

10. Shell Programming and Scripting

Process PID

Hi Friends :p I have a little problem please help me out. I have a Unix based OS Sun Server having oracle 8i as database on it. The server has one client with windows OS. The client uses developer 2000 (GUI) to run query and run processes. I want to know how can I know the PID of a process run... (3 Replies)
Discussion started by: vanand420
3 Replies
Login or Register to Ask a Question