Solved

How to know if a program is running

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

752 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