Solved

How to know if a program is running

Posted on 2003-10-21
4
184 Views
Last Modified: 2010-04-22
Dear all.

I have problem with this simple script. It simply:
if xpenguins is running
    kill it
else
    run it

The script is as follow:
#!/bin/sh
if [ `ps -ax | grep xpenguins` ]; then
    skill xpenguins
else
    xpenguins -n 10 -all
fi

The problem is, ' ps -ax | grep xpenguins' always return true.
Please suggest the better way.

Thanks.

0
Comment
Question by:Kocil
4 Comments
 
LVL 3

Assisted Solution

by:scn
scn earned 50 total points
ID: 9592166
It's because the result of 'ps -ax' also contains the current command 'grep xpenguins'.
Try:
if [ `ps -ax | grep xpenguins | grep -v grep` ]; then
or
if [ -n "`pidof xpenguins`" ]; then
0
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 50 total points
ID: 9592169
if [ `ps -ax | grep xpenguins | grep -v grep` ]; then

0
 
LVL 7

Accepted Solution

by:
glassd earned 100 total points
ID: 9593913
A neater solution (only one call to grep) is:
if [ `ps -ax | grep [x]penguins` ]; then


0
 
LVL 5

Author Comment

by:Kocil
ID: 9596306
Nice answers...,
but I like glassd's most.

Thanks guys.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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