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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-17-2005, 05:58 AM
|
#1
|
|
Member
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56
Rep:
|
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)?
|
|
|
|
11-17-2005, 06:17 AM
|
#2
|
|
Member
Registered: Jul 2003
Location: Valencia (Spain)
Distribution: slackware 11, FEDORA CORE 4, RHEL3, Gentoo...
Posts: 361
Rep:
|
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 06:19 AM.
|
|
|
|
11-17-2005, 11:24 AM
|
#3
|
|
Member
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56
Original Poster
Rep:
|
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.
|
|
|
|
11-17-2005, 11:47 AM
|
#4
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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 11:48 AM.
|
|
|
|
11-17-2005, 12:07 PM
|
#5
|
|
Member
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56
Original Poster
Rep:
|
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 12:14 PM.
|
|
|
|
11-17-2005, 12:18 PM
|
#6
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
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
|
|
|
|
11-17-2005, 01:21 PM
|
#7
|
|
Member
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56
Original Poster
Rep:
|
There isn't any way getting the return value of one specific script?
|
|
|
|
11-17-2005, 01:26 PM
|
#8
|
|
Member
Registered: Aug 2003
Location: Rochester, MN, U.S.A
Distribution: Gentoo
Posts: 987
Rep:
|
lets say you have a script called script.sh
the variable $? will tell you the return value of the script script.sh
Is that what you are asking?
|
|
|
|
11-17-2005, 01:37 PM
|
#9
|
|
Member
Registered: Oct 2005
Distribution: Ubuntu, ElemntaryOS, Linux Mint, openSUSE, Xubuntu
Posts: 56
Original Poster
Rep:
|
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.
|
|
|
|
All times are GMT -5. The time now is 06:09 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|