0

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

  • Does the filename need to be the command that's executing? Or could you have a process .../php test.php and do you want to match it? – Jeff Schaller Jul 24 at 15:06
  • I just want to know if a specific file is being executed. I was using PHP as an example just for clarification. I might want to check various file types such as test.py, test.js, etc....I am not sure if where its situated at in my directory will make a difference? you tell me? @JeffSchaller – The Chem X Jul 24 at 16:51
  • In php something.py, it'd be precise to say that php is executing the commands in something.py; similarly, if there's a process sed /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. – Jeff Schaller Jul 24 at 16:57
1

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 – The Chem X Jul 24 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 and sed /script/ test.php -- neither of which indicate that test.php is executing. – Jeff Schaller Jul 24 at 16:59
0

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.

0
#!/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 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.