Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-06-2007, 04:44 AM   #1
LQ Newbie
Registered: Jan 2006
Posts: 12

Rep: Reputation: 0
How to print my process id using shell script

i want to print the process id of the shell script which i executed.
in shell script , the symbol '$$' gives the process id of the bash running but not the shell script

if i have shell script named

and when i execute the command. &

then i would get an output :
[1] 10234

which is the process id of the background.
Now i want to capture this process id so that i can kill this process later .
Old 06-06-2007, 05:09 AM   #2
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,707

Rep: Reputation: 416Reputation: 416Reputation: 416Reputation: 416Reputation: 416
That would be "$!"
Old 06-06-2007, 09:45 AM   #3
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
bash is a POSIX shell so this is what POSIX thinks about $!:

$! Expands to the decimal process ID of the most recent background command
executed (as in a list) from the current shell.
(For example, background commands executed from subshells
do not affect the value of "$!" in the current shell environment.)
For a pipeline, the process ID is that of the last command in the pipeline.
Old 06-07-2007, 12:16 AM   #4
LQ Newbie
Registered: Jan 2006
Posts: 12

Original Poster
Rep: Reputation: 0
we cannot use $!

Originally Posted by jim mcnamara
bash is a POSIX shell so this is what POSIX thinks about $!:
jim mcnamara is right. we cannot use $! as this the process ofthe bash running and not the
shell script which was running . if you try to print that value using the shell script and
try to execute that shell script in hte background you will find two different process id.

so i need different method for this .
Old 06-07-2007, 12:23 AM   #5
LQ Newbie
Registered: Jan 2006
Posts: 12

Original Poster
Rep: Reputation: 0
thanks ill use $!

ill go for that . what i'll do is ill write another script to exececute my script and capture
its process id from this script.

thanks all


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script - process directory keith2045 Programming 7 05-31-2007 07:26 PM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 08:02 AM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 09:09 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 02:09 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:47 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration