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:

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

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

RESULTS:

Where am i going wrong??? maybe just enough coffee this morning yet..:confused:

You got to filter the grep itself out by adding something like

...| 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?).

Use Following at the very begining of your SCRIPT :

#!/usr/bin/bash

And then tyy again...

And let us know if still problem....

:b:

What's wrong with using $0?

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

$ 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

Doh! on my part... works great now

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

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

RESULT:

Yes, what's wrong ? :confused:

it's a cleaner solution then the ps + greping + awk... thanks again for your help.