Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i find the process name by the shell script that invokes it?

Tags:

bash

shell

unix

ksh

Is there a way, I can find the process name of bash script by the shell script that was used to invoke it? or can I set process name of bash script to something such as

-myprocess

(I have looked into argv[0], but I am not clear about it)

so when I use

ps -ef | grep -c '[M]yprocess'

I get only all the instances of myprocess?

like image 1000
Vijay Shekhawat Avatar asked Nov 19 '25 08:11

Vijay Shekhawat


1 Answers

To obtain the command name of the current script that is running, use:

ps -q $$ -o comm=

To get process information on all running scripts that have the same name as the current script, use:

ps -C "$(ps -q $$ -o comm=)"

To find just the process IDs of all scripts currently being run that have the same name as the current script, use:

pgrep "$(ps -q $$ -o comm=)"

How it works

$$ is the process ID of the script that is being run.

The option -q $$ tells ps to report on only process ID $$.

The option -o comm= tells ps to omit headers and to skip the usual output and instead print just the command name.

like image 58
John1024 Avatar answered Nov 20 '25 21:11

John1024



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!