Process ID of Command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process ID of Command
# 1  
Old 03-13-2007
Process ID of Command

I need a way to get the Process ID of the last command I executed in a script. Not the last background process but the last command.

For example, suppose I am executing a binary inside a script like so.

binary.program argument1 argument2

If this binary program runs fast, is there a command that will tell me the last PID of that command. In other words, can I get the PID of a command I execute within a shellscript? Is my only alternative to get information like this by running the process in the background and using "$!"?

Any thoughts are appreciated. Thanks in advance.
# 2  
Old 03-13-2007
You could do that with system call tracer,

in Linux available as strace

on Solaris as truss commands

take this sample,

Code:
>cat script.zsh
#! /bin/zsh
  ./while
exit 0

while binary is nothing but, endless loop
Code:
#include <stdio.h>
int main()
{
  while(1) {
  }
  return 0;
}

Run it as,

strace zsh script.zsh > out.log 2>&1

And now in the out.log strace would have traced system call of the script which spawned the binary within it, it would be available as the return value of clone system call.

In the out.log it should be something like,
Code:
clone(parameters) = return value;

Return value above is the pid of the binary ( child ) spawned by the script ( parent )

Try it out! Smilie
# 3  
Old 03-13-2007
Interesting

I like your alternative, however I unfortunately need to make this work on HP-UX out of the box. So I do not have trace or tusc available on this server so I cannot use this method. Thanks for the reply, I will keep digging and see if I can find another solution. Thanks again. Smilie
# 4  
Old 03-13-2007
Am surprised, Smilie

why is that there isnt tusc (system call tracer for HP-UX) available ? Smilie
# 5  
Old 03-13-2007
I know

Well, tusc is not default, it is something that has to be installed extra. I am not a sysadmin on this box so I cannot make the call of adding it. Since what I was trying to do would go elsewhere, I have to also assume the HP-UX box sitting elsewhere would also not have tusc installed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if process is running if not then use command

Hello, Could someone do the following bash ubuntu script for me? I have 5 screen processes of bot: SCREEN -dmS Xbot_instance_1 php core.php -i 1 SCREEN -dmS Xbot_instance_2 php core.php -i 2 SCREEN -dmS Xbot_instance_3 php core.php -i 3 SCREEN -dmS Xbot_instance_4 php core.php -i 4 ... (2 Replies)
Discussion started by: kotch
2 Replies

2. Shell Programming and Scripting

Command to get exact tomcat process I am running ignoring other java process

Team, I have multiple batchjobs running in VM, if I do ps -ef |grep java or tomcat I am getting multiple process list. How do I get my exact tomcat process running and that is unique? via shell script? (4 Replies)
Discussion started by: Ghanshyam Ratho
4 Replies

3. Shell Programming and Scripting

Storing process Id of a nohup command

Hi, I am running below code: for i in `ls` do nohup sqlldr userid=apps/apps data=data01.dat log=my1.log control=my.ctl bad=my1.bad direct=yes silent=all parallel=true & done This will run the sqlldr command in parallel as a background process. I want to store the process Id each... (7 Replies)
Discussion started by: Pratiksha Mehra
7 Replies

4. Shell Programming and Scripting

Getting Process ID of a ftp command

Dear all Here is my command in my ksh script: ftp ${ftpParameter} ${serverName} 2>&1 <${ftpScriptFile} |tee -a $LOG_FILE & ftpPid=$! wait Due to server problem,the server accepts the connection and then do nothing,it makes the above script hang, is it possible to set time out for ftp... (1 Reply)
Discussion started by: cstsang
1 Replies

5. Shell Programming and Scripting

Tracking process via ps command

Hello folks, I am tracking a process httpd only. But when i am grepping it, it is returning me multiple process of httpd, second it is showing another process of monitorix-httpd. Below commands i have tried. Current output # ps ax |grep http 929 ? Ss 0:00 monitorix-httpd... (5 Replies)
Discussion started by: learnbash
5 Replies

6. Shell Programming and Scripting

Running a command in a new process?

Hello I'm using GNU screen for an application that I'm making. I will try to explain: This application opens 2 screen session, A and B. Screen session A has a script running in teh first window. I want to be able to switch from screen session A to screen session B, from the script running in... (1 Reply)
Discussion started by: jondecker76
1 Replies

7. Shell Programming and Scripting

what is the command to how much percentage has taken by a process

Hello Friends, please help me to know about tha above question. Thanks & Regards Siva Ranganath 7760774961 (1 Reply)
Discussion started by: sivaranganath
1 Replies

8. UNIX for Dummies Questions & Answers

process command

I'm looking for a specific command to extract the libraries linked to a specific process. Would someone get an idea ?? (2 Replies)
Discussion started by: tonoche
2 Replies

9. Shell Programming and Scripting

need script to process ls-l command

ne1 know how i can do this???? a script that process the output of UNIX command ls -l cheers (12 Replies)
Discussion started by: sinner
12 Replies

10. Shell Programming and Scripting

trying to read batch process but need some command help

I am trying to access batch process that take place each nite. I am using Solaris 5.8 (and i am used to redhat). however I am trying to access say a certain directory. The home/oradev , is the directory...in there i am trying to access say a batch file within this, how can see if they are in... (1 Reply)
Discussion started by: etravels
1 Replies
Login or Register to Ask a Question