3

I have the command:

ps ax | grep my_application

Which outputs a large string including the port of the proccesses involved in my_application.

If my_application is not running it outputs:

3873 pts/0    S+     0:00 grep my_application

I need a condition to test the output of ps ax | grep my_application and do exit 2in case my_application is still running.

Any ideas?

3
  • 2
    You can use pgrep if you have it. Commented May 5, 2014 at 16:49
  • if the output of ps ax | grep my_application command produces two or more lines as output then it means that the process is currently running or otherwise it's not. Commented May 5, 2014 at 17:13
  • possible duplicate of More elegant "ps aux | grep -v grep" Commented May 6, 2014 at 9:05

3 Answers 3

6

You can add brackets to exclude the grep process:

ps ax | grep -q '[m]y_application' && exit 2

If my_application is running, ps ax will print my_application along with the grep command and pattern. Grep understands [m] as a character class, but it will not match the litteral string '[m]' printed by ps ax, so the grep process is excluded.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you explain what bracket does?
@Shahbaz, I've added a note regarding the brackets.
@tripleee, maybe I should rephrase the comment: To improve the quality of your answer, it's best if you explain how the solution works, rather than just putting the solution. While links to the manual are also appreciated, an excerpt where the relevant part is highlighted helps the OP a lot.
@Shahbaz This question and this answer are both extremely frequently repeated. I was unable to quickly find a precise duplicate on Stack Overflow but brief googling should get you everything you need.
@tripleee, brief googling would answer 99% percent of the questions on SO. This topic actually comes up a lot (a recent discussion) and the answer has always been to answer these trivial questions, rather than close it, refer them to google, etc. If you wish to argue about this, please refer to the posts in meta (I'm just following the policy of SO).
|
1

The simplest solution is to use pgrep, if it is available on your system.

Otherwise, you can customize the way ps reports processes. You don't have to use the default format, which includes (some) command-line arguments.

For example:

ps ax -ocomm

will only output the executable name. If you want pids as well,

ps ax -opid,comm

For convenient grepping, you might want to remove the headers

ps ax -opid=,comm=

Any of those should work just fine as input to grep application_name, although you still need to watch out for application_name being a substring of another application name.

man ps should give you the list of possible output fields. There are lots of them.

Comments

0

You can exclude stuff from the results returned by grep by using something like this:

ps ax | grep my_application | grep -v grep

This excludes the process that is returning when your application is not running that shows the grep running. When this is run and your application is not running, it will return nothing. Check for the empty string, and exit that way.

2 Comments

Even better, just use [m]y_application as the pattern, which does not match itself in the output of ps.
Just be sure to protect the string from the shell with quotes in case there happens to be a file named my_application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.