Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this instead:</p> <pre><code>ps aux | grep -q "[p]rocessName" if [ $? -eq 0 ]; then echo "running" else echo "not running" fi </code></pre> <p>The brackets around the first letter of processName means do don't need the "grep -v grep", while the -q means we don't need the pipe to /dev/null</p> <p>$? gives you the return code of the previous command executed. Hence, testing if it were 0 would indicate if "grep" found what it was looking for.</p> <p><strong>Update</strong></p> <p>If your process name is really short (say "cup"), you might get a false positive as it may match other processes too (say "cupsd"). You can overcome this by having grep match whole words - add the "-w" flag.</p> <p>Not that this technique is not perfect. You may end up matching entries in the username/date fields. If that happens, look up "man ps" and be more selective of what you print out before doing a grep. Alternatively, prefilter the output with awk to extract only the column showing process/cmd name . E.g:</p> <pre><code>ps aux | awk '{print $11}' | grep -q -w "[p]rocessName" if [ $? -eq 0 ]; then echo "running" else echo "not running" fi </code></pre> <p><strong>Update 2</strong></p> <p>you can also use pgrep as suggested in answer below. </p> <p>For really short process names, you might want to specify word boundaries (\b) before and after your process name to prevent overmatching (as described above)</p> <pre><code>pgrep "\bprocname\b" &gt; /dev/null if [ $? -eq 0 ]; then echo "running" else echo "not running" fi </code></pre> <p><strong>Update 3</strong></p> <p>From the updated question, I see that you're running it from an init script. There's always a danger of pgrep matching the script itself. Try:</p> <pre><code>pgrep Processname | grep -q -v $$ if [ $? -eq 0 ]; then echo "running" else echo "not running" fi </code></pre> <p>That excludes the PID of the script from pgrep matches.</p> <p><strong>Update 4</strong></p> <p>(final update? fingers crossed)</p> <p>If the init script is run via the "service" command, then we need to filter out the parent PID as well. How about:</p> <pre><code>pgrep Processname | grep -v $$ | grep -q -v $PPID if [ $? -eq 0 ]; then echo "running" else echo "not running" fi </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload