I am wondering is it possible to check if a specific file with a specific name is running? I see a lot of various cases that show PID's rather than a filename but would rather take a filename route. For instance lets say a file called test.php is being executed via PHP command (in this case lets just pretend this file is just an infinite loop just so the file remains running indefinitely). Is there any command that I can write out that will let me know that this particular file is currently being executed? If this is not doable can I see all PHP files that are currently being ran? Currently using Ubunutu 18.04 LTS
3 Answers
Try pgrep
:
pgrep -f test.php
This will output the PID of your command.
Alternative:
ps aux | grep '[t]est\.php'
This will output the corresponding line of ps aux
.
-
This actually turned out to be my solution. I was overthinking my problem thank you for the help Jul 24, 2019 at 16:56
-
Note that this will return the processes that have
test.php
anywhere in their process name; some false positives include/some/other/test.php/process-name arg1 arg2
andsed /script/ test.php
-- neither of which indicate thattest.php
is executing.– Jeff Schaller ♦Jul 24, 2019 at 16:59
Another option is using killall
killall -0 -q test.php && echo test.php is running
Despite the name of the command, the process is not killed.
#!/bin/sh
processname='login'
if [ "$(pgrep $processname)x" != "x" ]
then
echo $processname is running
else
echo $processname is NOT running
fi
An alternate version that uses the exit status rather than the return value, and also uses a more compact syntax
#!/bin/sh
processname='login'
pgrep $processname > /dev/null
test $? -eq 0 && echo "$processname is running" || echo "$processname is NOT running"
-
pgrep
exits with a zero exit status if finding the process, otherwise with a non-zero exit status, so no need to use[ ... ]
at all.– Kusalananda ♦Jul 24, 2019 at 16:22
.../php test.php
and do you want to match it?php something.py
, it'd be precise to say thatphp
is executing the commands insomething.py
; similarly, if there's a processsed /something/ test.php
,sed
is executing -- test.php is not being executed. Scripts of various kinds can also be executed directly (/path/to/test.php
), so I was curious what situation you were looking for.