Solved

How to know if a program is running

Posted on 2003-10-21
4
188 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Equivalent solution for gettimezoneinformation 12 131
qos on linux 3 105
linux $PWD 2 656
Linux : how to point the OS to use the new DNS server 11 104
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…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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 …

809 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