Link to home
Start Free TrialLog in
Avatar of Julian Matz
Julian MatzFlag for Ireland

asked on

Bash: check if process is running

Hi!

I'm trying to make a bash script to check if Firefox is running. If not, then I would like to start it.

So far, I have the following:

APP1="firefox"
APPCHK=$(ps aux | grep -c $APP1)

But I'm not sure about the return codes. I'd be grateful if someone could help.

The command /usb/bin/firefox will start Fx the way I want, but I think I also need to export the DISPLAY variable before hand. How do I do that in bash? This is how I'd do it from the command line:

$ export DISPALY=:0

Also, do I need to exit the bash script once Firefox is running, because normally, if I start Fx from command line I need do use something like following so that I can close my terminal session without shutting down Fx again:

$ ssh user@localhost -f -X firefox
Avatar of Julian Matz
Julian Matz
Flag of Ireland image

ASKER

I think I've figured it out. My code is below. Would still appreciate comments. Does it look ok?

The ampersand in /usr/bin/firefox & is to return control to the terminal if the bash script is run from the command line.
#!/bin/sh
APP1="firefox"

if ps ax | grep -v grep | grep $APP1 > /dev/null
then
        echo "$APP1 is running, everything is fine."
else
        echo "$APP1 is not running. Attempting to start..."
        DISPLAY=":0" ; export DISPLAY
        /usr/bin/firefox &
fi

exit

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of wareb73
wareb73
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could you show me the pgrep alternative?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That is a lot shorter! lol

Thanks!