[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Bash: check if process is running

Posted on 2009-12-17
6
Medium Priority
?
806 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:Julian Matz
6 Comments
 
LVL 21

Author Comment

by:Julian Matz
ID: 26074513
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

0
 
LVL 4

Accepted Solution

by:
wareb73 earned 1440 total points
ID: 26074696
That look like the correct logic.  Should work as you want.
0
 
LVL 35

Assisted Solution

by:Duncan Roe
Duncan Roe earned 280 total points
ID: 26075320
There is a pgrep command - saves having the ps pipe
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
LVL 21

Author Comment

by:Julian Matz
ID: 26075420
Could you show me the pgrep alternative?
0
 
LVL 1

Assisted Solution

by:elanops
elanops earned 280 total points
ID: 26081534
The pgrep alternative would be as such:


#!/bin/sh
APP1="firefox"

if pgrep $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

0
 
LVL 21

Author Comment

by:Julian Matz
ID: 26099101
That is a lot shorter! lol

Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This article will show you step-by-step instructions to build your own NTP CentOS server.  The network diagram shows the best practice to setup the NTP server farm for redundancy.  This article also serves as your NTP server documentation.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question