Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 447345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:33:40+00:00 2026-05-12T21:33:40+00:00

Is there any reason why this script always returns running, regardless of whether my

  • 0

Is there any reason why this script always returns “running”, regardless of whether my process is started or stopped?

if ps ax | grep -v grep | grep "processName" > /dev/null
then
   echo $"running"
else
   echo $"not running"
fi

Thank you very much

UPDATE :
I add a full example of my script, maybe there is something wrong elsewhere.

case "$1" in
  start)
    # Start daemons.

    echo -n $"Starting daemon: "
    ;;

  stop)
    # Stop daemons.
    echo -n $"Shutting down: "
    echo
    ;;
  status)
    pgrep -f "ProcessName" > /dev/null
    if [ $? -eq 0 ]; then
        echo $"ProcessName is running"
    else
        echo $"ProcessName is not running"
    fi
    ;;
  restart)
    $0 stop
    $0 start
    ;;

  *)
    echo $"Usage: $0 {start|stop|status|restart}"
    exit 1
esac

UPDATE 2 :

[user@dev init.d]# pgrep -f "MyProcess" > /dev/null
[user@dev init.d]# echo $?
0
[user@dev init.d]# service MyProcess stop
Shutting down MyProcess: Terminated
[user@dev init.d]# pgrep -f "MyProcess" > /dev/null
[user@dev init.d]# echo $?
1

But if [ $? -eq 0 ]; then seems to be TRUE all the time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:33:40+00:00Added an answer on May 12, 2026 at 9:33 pm

    Try this instead:

    ps aux | grep -q "[p]rocessName"
    if [ $? -eq 0 ]; then
        echo "running"
    else
        echo "not running"
    fi
    

    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

    $? 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.

    Update

    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.

    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:

    ps aux | awk '{print $11}' | grep -q -w "[p]rocessName"
    if [ $? -eq 0 ]; then
        echo "running"
    else
        echo "not running"
    fi
    

    Update 2

    you can also use pgrep as suggested in answer below.

    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)

    pgrep "\bprocname\b" > /dev/null
    if [ $? -eq 0 ]; then
        echo "running"
    else
        echo "not running"
    fi
    

    Update 3

    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:

    pgrep Processname | grep -q -v $$
    if [ $? -eq 0 ]; then
        echo "running"
    else
        echo "not running"
    fi
    

    That excludes the PID of the script from pgrep matches.

    Update 4

    (final update? fingers crossed)

    If the init script is run via the “service” command, then we need to filter out the parent PID as well. How about:

    pgrep Processname | grep -v $$ | grep -q -v $PPID
    if [ $? -eq 0 ]; then
        echo "running"
    else
        echo "not running"
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This script worked fine for a few weeks then stopped working for no reason.
This is my first time using JQuery in any of my projects. I have
I have a script that runs fine in the browser, however fails when run
Heh, I'm using jQuery AJAX Call to pull data from a self hosted webservice
I am trying to ultimately extract some files from a tar archive within PHP,
Im trying to load an xml file using the jquery ajax function,it works fine
I have an odd issue that I can not for the life of me
When developing on BlackBerry or iOS, you can deploy your application just by dropping
I'm using jquery and php for some async job. I'm using $.ajax with beforesend
I'm working on an ASP.NET Web Project with some AJAX magic. As my GridView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.