Quantcast
Viewing all articles
Browse latest Browse all 2

Answer by muru for See if a script is running when using #!/usr/bin/env

On Linux, you could use pgrep to get PIDs of likely suspects, and inspect the first argument of those PIDs (available in /proc/$PID/cmdline). /proc/$PID/cmdline has all the arguments (including argument 0) of the process, separated ASCII NUL (\0).

Something like:

pgrep bash | xargs -I {} sed -nz '2{p; q}' /proc/{}/cmdline | grep -Fqzx "$0"

This assumes your sed and grep support null-separated lines. The sed prints the second line of the respective cmdline files (argument 1), which would be the script name. grep then looks for exact matches of your script name. You could do the entire match in sed, but I don't feel like juggling quotes.

This will fail if you call your script by different paths:

/home/user/myscriptcd /home; usr/myscriptmyscript

However, it should be safe against whitespace in script names.


Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>