Capturing a PID.

Tom Bradford plug-discuss@lists.PLUG.phoenix.az.us
Fri, 25 May 2001 11:02:11 -0700


This is a multi-part message in MIME format.
--------------EC99529D03E7A357B2C9EB98
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Don Harrop wrote:
> Does anyone have ideas on how to capture the pid of a program into a file as it starts up?  When you execute your program with the "&" (background process) the shell spits out the process id but I can't seem to redirect that out put to a file.  I don't want to use "pidof" after the fact either in case there is another process running with the same name.

Try this:

<your command> &
echo $! > program.pid

$! is the most recent program executed.
$$ is the current shell script

I've attached a copy of dbXML's startup script and its SYSV wrapper for
a better example.

--Tom

-- 
Tom Bradford --- The dbXML Project --- http://www.dbxml.org/
We store your XML data a hell of a lot better than /dev/null
--------------EC99529D03E7A357B2C9EB98
Content-Type: application/octet-stream;
 name="dbxml.server"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="dbxml.server"

IyEvYmluL3NoCgojIFRoaXMgaXMgYSBxdWljayBhbmQgdWdseSBTWVNWIFNlcnZpY2UgU2Ny
aXB0IGZvciBkYlhNTAojIFlvdSdyZSBnb2luZyB0byBoYXZlIHRvIG1vZGlmeSB0aGUgZm9s
bG93aW5nIHZhcmlhYmxlcwojIGJhc2VkIG9uIHlvdXIgaW5zdGFsbGF0aW9uIGxvY2F0aW9u
cy4KCiMgJElkOiBkYnhtbC5zZXJ2ZXIsdiAxLjUgMjAwMS8wNS8yMSAxOTo0Njo0OSB0b20g
RXhwICQKCkJBU0VESVI9L2hvbWUvdG9tL3dvcmsvZGJYTUwKQklORElSPSRCQVNFRElSL2Jp
bgpKQVZBX0hPTUU9L3Vzci9sb2NhbC9qYXZhClBBVEg9JFBBVEg6JEpBVkFfSE9NRS9iaW46
JEJJTkRJUjovdXNyL2JpbjovdXNyL3NiaW4KUElERklMRT0kQkFTRURJUi9jb25maWcvZGJ4
bWwucGlkCgpleHBvcnQgUEFUSCBKQVZBX0hPTUUKCmNkICRCQVNFRElSCgpNT0RFPSQxCmNh
c2UgIiRNT0RFIiBpbgoKICAgJ3N0YXJ0JykKICAgICAgaWYgdGVzdCAteCAkQkFTRURJUi9z
dGFydAogICAgICB0aGVuCiAgICAgICAgIGV4ZWMgLi9zdGFydCAxPiAvZGV2L251bGwgMj4g
L2Rldi9udWxsICYKICAgICAgZWxzZQogICAgICAgICBlY2hvICJDYW4ndCBleGVjdXRlICRC
QVNFRElSL3N0YXJ0IgogICAgICBmaQogICAgICA7OwoKICAgJ3N0b3AnKQogICAgICBpZiB0
ZXN0IC1mICIkUElERklMRSIKICAgICAgdGhlbgogICAgICAgICBEQlhNTF9QSUQ9YGNhdCAk
UElERklMRWAKICAgICAgICAgZWNobyAiS2lsbGluZyBkYlhNTCB3aXRoIHBpZCAkREJYTUxf
UElEIgogICAgICAgICBraWxsIC0xNSAkREJYTUxfUElECiAgICAgIGVsc2UKICAgICAgICAg
ZWNobyAiTm8gZGJYTUwgcGlkIGZpbGUgZm91bmQuICBMb29rZWQgZm9yICRQSURGSUxFLiIK
ICAgICAgIGZpCiAgICAgICA7OwogICAncmVzdGFydCcpCiAgICAgIGlmIHRlc3QgLWYgIiRQ
SURGSUxFIgogICAgICB0aGVuCiAgICAgICAgIERCWE1MX1BJRD1gY2F0ICRQSURGSUxFYAog
ICAgICAgICBlY2hvICJSZXN0YXJ0aW5nIGRiWE1MIHdpdGggcGlkICREQlhNTF9QSUQiCiAg
ICAgICAgIGtpbGwgLTEgJERCWE1MX1BJRAogICAgICBlbHNlCiAgICAgICAgIGVjaG8gIk5v
IGRiWE1MIHBpZCBmaWxlIGZvdW5kLiAgTG9va2VkIGZvciAkUElERklMRS4iCiAgICAgIGZp
CiAgICAgIDs7CiAgICopCiAgICAgICMgdXNhZ2UKICAgICAgZWNobyAidXNhZ2U6ICQwIHN0
YXJ0fHN0b3B8cmVzdGFydCIKICAgICAgZXhpdCAxCiAgICAgIDs7Cgplc2FjCg==
--------------EC99529D03E7A357B2C9EB98
Content-Type: application/octet-stream;
 name="start"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="start"

IyEvYmluL2Jhc2gKaWYgdGVzdCAteiAkREJYTUxfSE9NRTsgdGhlbgogICBEQlhNTF9IT01F
PWBkaXJuYW1lICQwYAogICBleHBvcnQgREJYTUxfSE9NRQpmaQoKZWNobyAiZGJYTUwgTG9h
ZGVyIgplY2hvICI9PT09PT09PT09PT0iCmVjaG8KZWNobyAiRW52aXJvbm1lbnQiCmVjaG8g
Ii0tLS0tLS0tLS0tIgplY2hvICJEQlhNTF9IT01FID0gIiAkREJYTUxfSE9NRQoKQ0xBU1NQ
QVRIPSREQlhNTF9IT01FL2NsYXNzZ2VuOiRDTEFTU1BBVEgKCkpBUlM9YGxzIC0xICREQlhN
TF9IT01FL2phdmEvbGliLyouamFyYApmb3IgamFyIGluICRKQVJTCmRvCiAgIENMQVNTUEFU
SD0kamFyOiRDTEFTU1BBVEggOwpkb25lCgplY2hvICJDTEFTU1BBVEggPSAiICRDTEFTU1BB
VEggCmVjaG8KZWNobyAiU3RhcnRpbmcgZGJYTUwiCmVjaG8gIi0tLS0tLS0tLS0tLS0tIiAK
CmNkICREQlhNTF9IT01FCgplY2hvICQkID4gJERCWE1MX0hPTUUvY29uZmlnL2RieG1sLnBp
ZAoKUkVTVEFSVD0xCgpzZXJ2ZXJfc2h1dGRvd24oKSB7CiAgIFNIVVRET1dOX0NNRD0iIgog
ICBJTlNUQU5DRVM9ImBjYXQgJERCWE1MX0hPTUUvY29uZmlnL2luc3RhbmNlcy5jZmdgIgog
ICBmb3IgaW5zdGFuY2UgaW4gJElOU1RBTkNFUwogICBkbwogICAgICBkYnhtbGFkbWluIHNo
dXRkb3duIC1jIC8kaW5zdGFuY2UgMT4gL2Rldi9udWxsIDI+IC9kZXYvbnVsbAogICBkb25l
IAp9CgpzZXJ2ZXJfcmVzdGFydCgpIHsKICAgUkVTVEFSVD0xCiAgIHNlcnZlcl9zaHV0ZG93
biAgIAp9Cgp0cmFwICJlY2hvOyBlY2hvICdB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