LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 11-17-2005, 06:58 AM   #1
Conjurer
Member
 
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56

Rep: Reputation: 15
Check if program is running


Hi!

I'm just starting to look into shell scripts. I would like to know if there is a CLI way of checking whether or not a program is running (something along the lines of 'thisCommand -kaffeine' returning a boolean value)?
 
Old 11-17-2005, 07:17 AM   #2
Y0jiMb0
Member
 
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361

Rep: Reputation: 30
Hi,
I cannot remember a more elegant way (sure some guru around here can provide us with a fascinating solution); but this does the trick:
Code:
some_variable=`ps -C kaffeine|grep -v PID`
then if "some_variable" is empty, kaffeine is not running; if "some_variable" is not empty, kaffeine is running.
It could be that there is an option in "ps" doing exactly what you look for; do "man ps" and have fun reading it.

Regards

edit: or more transparent: substitute "grep -v PID" by "grep kaffeine"

Last edited by Y0jiMb0; 11-17-2005 at 07:19 AM.
 
Old 11-17-2005, 12:24 PM   #3
Conjurer
Member
 
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I'm not particularly knowledgable on the matter - only started looking into it a few days ago - but aren't you supposed to 'enclose' the ps pipe grep command into a variable on its own?

Like
Code:
 a=$(ps -C | grep kaffeine)
- or what? That's the only way I can make it work, anyway. Don't know if I'm missing something.
 
Old 11-17-2005, 12:47 PM   #4
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
both of those examples are called command subtitution. Either one can be used. you can either put the command in backticks
Code:
`enter command here`
or the way you did it
Code:
$(enter command here)
the second method is prefered, it allows for nesting, and possible some other advantages

Last edited by shanenin; 11-17-2005 at 12:48 PM.
 
Old 11-17-2005, 01:07 PM   #5
Conjurer
Member
 
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
OK - so I've got that little part working - and I've discovered the wonderful 'if grep' construct Just one more question:

How would I go about picking up exit values (0 or 1) and variables from other scripts?

Last edited by Conjurer; 11-17-2005 at 01:14 PM.
 
Old 11-17-2005, 01:18 PM   #6
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
the special variable $? gives the return value of the last command executed
Code:
cat filename | grep hhh
if [ "$?" -eq 0 ]; then
    echo "it found the string"
fi

I have not used bash for a while, I think that is the correct syntax
 
Old 11-17-2005, 02:21 PM   #7
Conjurer
Member
 
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
There isn't any way getting the return value of one specific script?
 
Old 11-17-2005, 02:26 PM   #8
shanenin
Member
 
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987

Rep: Reputation: 30
lets say you have a script called script.sh
Code:
script.sh
echo $?
the variable $? will tell you the return value of the script script.sh

Is that what you are asking?
 
Old 11-17-2005, 02:37 PM   #9
Conjurer
Member
 
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56

Original Poster
Rep: Reputation: 15
I suppose I was thinking java style (thisFile.myVar) - but it isn't important, as I can easily accomplish what I want using this more sequential approach.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Check if a program is running naimslim89 Programming 12 05-08-2012 11:24 PM
KDE - please check that the "DCOPserver" program is running Thanotos Linux - Newbie 2 11-27-2006 07:11 PM
please check that the "dcopserver" program is running Thanotos Slackware 7 11-14-2005 09:06 PM
how to check the return value of a program Sammy2ooo Linux - General 1 04-27-2004 03:55 PM
program to check to make sure a process is running? IceNineJon Linux - Software 7 08-06-2003 03:07 PM


All times are GMT -5. The time now is 12:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration