Check which processes are running from a given location in bash
I have the following directories structure:
./folder1
-- file1.py
-- file2.py
./folder2
-- file3.py
-- file4.py
etc...
useless_file_a.py
useless_file_b.py
I am trying to write a bash
script to know which of file[i].py
processes are running. By running, I mean to see the process displayed as a result of the ps -elf
command.
My idea is to:
-
Loop through those
folder[i]
and list the.py
files available. For this step I can use:find . -type f -name "*.py" -mindepth 2
-
Check running processes that matches the names found in 1.
I know about the
ps -elf
command and I would like to link it to step 1.
I am struggling to come up with something for this last step.
The whole point of this script is to alert me if one of those .py
scripts are not running. As there can be many scripts and many folders, I want to automatize this process.
NB: The useless_file_*.py
are in the pwd
and are not relevant for me.
Solution 1:
It's not possible, generally, to find out "if a script is running". This information is not stored anywhere.
You can check if the string that is the filename is in the command line arguments of processes:
for f in */*.py; do
pgrep -f "$(basename "$f")"
done