How can I find the name of the process of an app running in Arch Linux

Viewed 424

TL;DR I need to find the name of the process being run by any app I have open. I need to input this into ps h -C to, at any time, see if the app is running.

I have a bash script written that opens an app, but only if it's not already running. It is extremely simple, so here is the whole thing.

~/.config/runifnotrunning.sh

if (( $(ps h -C $2 | wc -l) == 0 )); then
    exec $1
fi

The code below is the bit of code that I am struggling with.

$(ps h -C $2 | wc -l) 

It returns 0 when the process isn't running and something else when the process is running.

For an app like Brave, I can run

./runifnotrunning.sh brave brave

because the name of the process and the command to run brave are the same. The issue I'm having is when I don't know the name of the process. IntelliJ IDEA (installed with JetBrains Toolbox), for example, is run with

exec "~/.local/share/JetBrains/Toolbox/apps/IDEA-U/ch-0/201.7846.76/bin/idea.sh" %f

But what do I put inside $(ps h -C $2 | wc -l)?

I would like to know how I can find the name of the process to put in that code for any of my apps.

1 Answers

Hello

Keep it simple and platform independent. A very oldschool way for checking if a programm name is already loaded and running comes with pidof. Example...

if (pidof ${1})
then
 (echo "Result: ${?} - ${1} running";echo "Fail Code";return 1)
else
 (echo "Result: ${?} - ${1} not running";echo "Start Code";return 0)
fi

...and pidof is seen often on embedded linux devices.

Related