LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 10-04-2005, 09:05 AM   #1
Timok
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Rep: Reputation: 0
Question Process ID


Hello,

I would like to do something like that in a shell script

MyProcessA &
MyProcessB &
MyProcessC &
echo "My processes started"
wait MyProcessA
wait MyProcessB
wait MyProcessC
echo "My processes finished"

The wait command can receive a PID and wait for its completion. But I do not know how to get the PID of MyProcessA when I start it. I do not want to use the ps command because MyProcessA may be finished then. All I want is the PID that you have when you type "MyProcessA &". Ex:

/users/timok/tests/shell>mkdir toto &
[1] 1058

So I would like to get the value '1058' in a variable.

Thanks for any help,
Timok
 
Old 10-04-2005, 10:11 AM   #2
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
$! will give the pid of the most recently executed background process in your script. So just get the pid of it after you execute it and put it in a variable.
 
Old 10-04-2005, 10:15 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Use $!

Code:
#!/bin/bash

ls -l >/dev/null &
echo "\$! : $!"

ls -l >/dev/null &
echo "\$! : $!"

ls -l >/dev/null &
echo "\$! : $!"
Hope this helps.
 
Old 10-04-2005, 11:54 AM   #4
Timok
LQ Newbie
 
Registered: Oct 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks

Thank you both for this quick reply. It works just like I wanted.

Timok
 
  


Reply



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
how a father process know which child process send the signal SIGCHLD icoming Programming 10 07-20-2010 07:26 AM
Linux boot up process Vs. Windows boot up process darkskull Linux - Software 7 12-30-2006 04:21 PM
how can i get the process id on exec a process antony_csf Linux - Software 1 06-17-2004 03:06 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
problem with binding process and than execute another process chapa Programming 0 08-27-2003 03:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration