Solved

How to know if a program is running

Posted on 2003-10-21
4
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

617 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