ProgrammingThis 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.
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.
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)?
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"
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.