Capturing running process name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing running process name
# 1  
Old 05-17-2010
Capturing running process name

i'm looking to have my script capture it's own process name while running.

i'm going to use this in the output of the script to track which script produced which output file(s).

when i run:

Code:
ps -ef | grep processname

i only get as results a ps -ef listing for the grep inside my script.

test_script:

TEST_PROCESS_OUTPUT.ksh

Code:
 
 echo "script started running"
 
process=$(ps -ef | grep TEST_PROCESS_OUTPUT)
 
echo ""
echo "PROCESS NAME: $process"

RESULTS:

Quote:
script started running

PROCESS NAME: u11a24 1413332 2613402 0 09:54:17 pts/7 0:00 grep TEST_PROCESS_OUTPUT

Where am i going wrong??? maybe just enough coffee this morning yet..Smilie
# 2  
Old 05-17-2010
You got to filter the grep itself out by adding something like
Code:
...| grep -v grep

Then it should show the process in case the process is running at the same time ps is called. It seems in your example that the process you are looking for is currently not running (maybe it is one with a real short life span? check it's logs if it writes any?).
# 3  
Old 05-17-2010
Use Following at the very begining of your SCRIPT :

Code:
#!/usr/bin/bash

And then tyy again...

And let us know if still problem....

Smilie
# 4  
Old 05-17-2010
What's wrong with using $0?

Bear in mind that the output will depend on how the script is invoked:

Code:
$ cat TEST_PROCESS_OUTPUT.ksh 
printf 'script started running\n\nPROCESS NAME: %s\n' "$0"

$ ./TEST_PROCESS_OUTPUT.ksh 
script started running

PROCESS NAME: ./TEST_PROCESS_OUTPUT.ksh
$ ksh TEST_PROCESS_OUTPUT.ksh 
script started running

PROCESS NAME: TEST_PROCESS_OUTPUT.ksh
$ ksh < TEST_PROCESS_OUTPUT.ksh 
script started running

PROCESS NAME: ksh

# 5  
Old 05-17-2010
THANKS TO ALL

Doh! on my part... works great now

i was missing at the top of my script the --> #!/bin/ksh

Code:
#!/bin/ksh
process_name=$(ps -ef | grep TEST_PROCESS_OUT | grep -v grep | awk '{print $9}')
echo ""
echo "PROCESS NAME: $process_name"

RESULT:
Quote:
PROCESS NAME: TEST_PROCESS_OUT.ksh
# 6  
Old 05-17-2010
Quote:
Originally Posted by radoulov
What's wrong with using $0?
Yes, what's wrong ? Smilie
# 7  
Old 05-24-2010
Thanks. i will use "$0"

it's a cleaner solution then the ps + greping + awk... thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capturing the return code from background process

Hi All, I was out not working on unix from quite sometime and came back recently. I would really appreciate a help on one of the issue I am facing.... I am trying to kick off the CodeNameProcess.sh in PARALLEL for all the available codes. The script runs fine in parallel. Let say there are... (1 Reply)
Discussion started by: rkumar28
1 Replies

2. Shell Programming and Scripting

Capturing name of the user who is running a shell script

Hello All, I am writing a shell script which will run on build server where multiple users can login.These users can run this script at the same time. I want to capture the name of the user who ran the script for every instance.How can I do that ? Regards,... (1 Reply)
Discussion started by: anand.shah
1 Replies

3. Shell Programming and Scripting

Capturing the killed process logs

I have two set of questions. 1) To skip killing some process automatically. 2) To kill other process and capture their log. I have set of process, some needs to be killed gracefully and others should be skipped. Listed are the process. adm 1522... (1 Reply)
Discussion started by: murali1687
1 Replies

4. Shell Programming and Scripting

Capturing PIDs of same process at different instances

Hi, I'm gonna launch a process from my 'C' code. I'm gonna launch it a few times. I would like to capture the PID of that process each time I launch. I have to copy the each PIDs into a 'C' variable and I have to kill all of them when I exit from the 'C' code. My requirement is int... (3 Replies)
Discussion started by: suryaemlinux
3 Replies

5. Shell Programming and Scripting

Capturing the CPU% used by a process

Hi, I just wonder I need to write a script where I can check if a particular process is consuming X amount of CPU. I was thinking of using the ps command but doesn't seems to work. Any ideas. Thanks. (2 Replies)
Discussion started by: arizah
2 Replies

6. Shell Programming and Scripting

Capturing the exit status of the script running in background

Hi All, I have a scenario where I am executing some child shell scripts in background (using &)through a master parent script. Is there a way I can capture the exit status of each individual child script after the execution is completed. (2 Replies)
Discussion started by: paragkalra
2 Replies

7. AIX

Capturing Process on AIX boxes - IMP

Guys we all know what command 'COLUMNS=2047 /usr/bin/ps –eo pid,ppid,uid,user,args' does.It prints 5-column output for the running processes on a AIX box. Here is simple thing i need: I need to insert this tabular data in a db2 table. How do i need? I have created table with these five... (0 Replies)
Discussion started by: ak835
0 Replies

8. UNIX for Dummies Questions & Answers

Capturing the Process ID of a process

hi Everybody I am trying to capture process id of a process which has been started through the execution of a shell script CLASSPATH=. CLASSPATH=${CLASSPATH}:sampleswing.jar java -cp ${CLASSPATH} Sample echo $! > pid.sh above is the simple script file called swings.sh When i... (8 Replies)
Discussion started by: appleforme1415
8 Replies

9. Shell Programming and Scripting

capturing process id

I am newbie to unix shells world. I am trying to capture a background process id into a file so that it can be killed later. this process is basically a java program running in background as: java TestApp & this returning process id immediately. So how can i redirect that pid into a file.... (1 Reply)
Discussion started by: bvreddy
1 Replies
Login or Register to Ask a Question