311

I am trying to check if a process (assume it is called some_process) is running on a server. If it is, then echo 1, otherwise echo 0.

This is the command that I am using but it only works partially (more info below). Note that I need to write the script in one line.

ps aux | grep some_proces[s] > /tmp/test.txt && if [ $? -eq 0 ]; then echo 1; else echo 0; fi

Note: The [s] in some_proces[s] is to prevent grep from returning itself.

If some_process is running, then "1" gets echoed, which is fine. However, if some_process is not running, nothing gets echoed.

2
  • 6
    You can use ps -Ccmd to find processes whose command name is "cmd", which can completely eliminate the grep. ps will set the exit code to some non-zero value if it fails to find a matching process.
    – rici
    Jun 20, 2013 at 0:27
  • Thanks to @rici I solved with easy ps -C php && echo 1 || php d.php this means if a php is running print an echo if not execute that
    – Ax_
    Oct 27, 2022 at 17:38

6 Answers 6

414

There is no need to explicitly check $?. Just do:

ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0 

Note that this relies on echo not failing, which is certainly not guaranteed. A more reliable way to write this is:

if ps aux | grep some_proces[s] > /tmp/test.txt; then echo 1; else echo 0; fi
8
  • 17
    If echo 1 fail, then echo 0 will be executed. In this case, echo 1 will never fail, but Note that A && B || C is not if-then-else. C may run when A is true.(from shellcheck).
    – schemacs
    Feb 26, 2014 at 16:49
  • @schemacs makes a very valid point: editing to provide a much better alternative. Feb 26, 2014 at 17:17
  • 2
    Is it necessary to re-direct the output of the ps command to a file ? Would this not work ? if ps aux | grep some_proces[s]; then echo 1; else echo 0; fi. Locally it seems to work for me. Is it because OP had the redirection in the command he tried?
    – user3104542
    Feb 28, 2014 at 13:27
  • 1
    There is absolutely no need to redirect the output. I merely ape the original position, since I assume the output is being saved for a reason. Often this sort of thing is merely suppressed (with grep -q or redirected to /dev/null). Feb 28, 2014 at 21:24
  • 1
    I would use pgrep.
    – pawciobiel
    Feb 5, 2016 at 17:32
92

&& means "and if successful"; by placing your if statement on the right-hand side of it, you ensure that it will only run if grep returns 0. To fix it, use ; instead:

ps aux | grep some_proces[s] > /tmp/test.txt ; if [ $? -eq 0 ]; then echo 1; else echo 0; fi

(or just use a line-break).

5
  • 7
    +1 This is a much better answer, as it actually answers the question as opposed to merely providing an alternative. Feb 12, 2014 at 11:47
  • This assumes we know $?. I believe that's the most recent return code? But isn't this whole exercise basically creating a return code? How about ps aux | grep some_proces[s] > /tmp/test.txt ; echo $? ?
    – Jeter-work
    Apr 29, 2022 at 13:26
  • @Jeter-work: Doesn't that give the opposite of the desired result? (Though I suppose you could fix that by prefixing the first command with !.)
    – ruakh
    Apr 29, 2022 at 20:05
  • I tested it and the redirect only runs if there's output from the grep statement. pgrep -q some_process > /tmp/test.txt; echo $? will be 0 if the process is running. 1 if not. And you're right, it is actually inverted.
    – Jeter-work
    May 4, 2022 at 17:16
  • @ruakh Yes, success will return 0, which is the recommendation for return code for 'success'.
    – Jeter-work
    May 4, 2022 at 17:28
67

Use grep -vc to ignore grep in the ps output and count the lines simultaneously.

if [[ $(ps aux | grep process | grep -vc grep)  > 0 ]] ; then echo 1; else echo 0 ; fi
0
19

You can make full use of the && and || operators like this:

ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0

For excluding grep itself, you could also do something like:

ps aux | grep some_proces | grep -vw grep > /tmp/test.txt && echo 1 || echo 0
0
10
pgrep -q some_process && echo 1 || echo 0

more oneliners here

1
  • pgrep -q some_process > /tmp/test.txt && echo 1 || echo 0 or pgrep -q some_process > /tmp/test.txt; echo $?; I'm assuming there's a reason to keep the grep output. And suggesting using the return code to skip using the ternary. Less operations.
    – Jeter-work
    May 4, 2022 at 17:13
0

checkdb: image: mysql:8.0.31 environment: MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD} MYSQL_DATABASE: ${MYSQL_DATABASE}

entrypoint: sh -c  'while true; do 
                mysql -h ${DB_MYSQL:-db} --user=${MYSQL_USER:-root} --password=${MYSQL_ROOT_PASSWORD} --execute "use ${MYSQL_DATABASE};";
                if [ $? -eq 0 ]; then exit 0; fi; 
                sleep 5;
              done'
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Mar 5 at 4:37

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.