Solved

How to know if a program is running

Posted on 2003-10-21
4
177 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
Comment Utility
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
Comment Utility
if [ `ps -ax | grep xpenguins | grep -v grep` ]; then

0
 
LVL 7

Accepted Solution

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


0
 
LVL 5

Author Comment

by:Kocil
Comment Utility
Nice answers...,
but I like glassd's most.

Thanks guys.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Mounting Linux Partition.... 30 300
Optimize bash script 20 449
Percona cluster member randomly crash 15 335
pipe to sed or perl, please help 5 120
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now