LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 06-06-2007, 05:44 AM   #1
ik_nitk
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 myShell.sh

and when i execute the command.

myShell.sh &

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, 06:09 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

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

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

Quote:
$! 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, 01:16 AM   #4
ik_nitk
LQ Newbie
 
Registered: Jan 2006
Posts: 12

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

Quote:
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, 01:23 AM   #5
ik_nitk
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
 
  


Reply


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 08:26 PM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 12:29 PM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 09:02 AM
Shell Script to kill a process automaticaly brazilian_user Programming 11 06-12-2006 10:09 PM
Kill a process launched by a shell script gsbarry Programming 7 01-06-2004 03:09 PM


All times are GMT -5. The time now is 03:07 PM.

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