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

  • SEARCH
  • Home
  • 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 7407077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:39:16+00:00 2026-05-29T05:39:16+00:00

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific

  • 0

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld, httpd…
What is the simplest way/command to do this?

  • 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-29T05:39:17+00:00Added an answer on May 29, 2026 at 5:39 am

    While pidof and pgrep are great tools for determining what’s running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following: ps cax | grep command

    The output on Gentoo Linux:

    14484 ?        S      0:00 apache2
    14667 ?        S      0:00 apache2
    19620 ?        Sl     0:00 apache2
    21132 ?        Ss     0:04 apache2
    

    The output on OS X:

    42582   ??  Z      0:00.00 (smbclient)
    46529   ??  Z      0:00.00 (smbclient)
    46539   ??  Z      0:00.00 (smbclient)
    46547   ??  Z      0:00.00 (smbclient)
    46586   ??  Z      0:00.00 (smbclient)
    46594   ??  Z      0:00.00 (smbclient)
    

    On both Linux and OS X, grep returns an exit code so it’s easy to check if the process was found or not:

    #!/bin/bash
    ps cax | grep httpd > /dev/null
    if [ $? -eq 0 ]; then
      echo "Process is running."
    else
      echo "Process is not running."
    fi
    

    Furthermore, if you would like the list of PIDs, you could easily grep for those as well:

    ps cax | grep httpd | grep -o '^[ ]*[0-9]*'

    Whose output is the same on Linux and OS X:

    3519 3521 3523 3524

    The output of the following is an empty string, making this approach safe for processes that are not running:

    echo ps cax | grep aasdfasdf | grep -o '^[ ]*[0-9]*'

    This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.

    #!/bin/bash
    PROCESS=$1
    PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*[0-9]*'`
    if [ -z "$PIDS" ]; then
      echo "Process not running." 1>&2
      exit 1
    else
      for PID in $PIDS; do
        echo $PID
      done
    fi
    

    You can test it by saving it to a file (named “running”) with execute permissions (chmod +x running) and executing it with a parameter: ./running "httpd"

    #!/bin/bash
    ps cax | grep httpd
    if [ $? -eq 0 ]; then
      echo "Process is running."
    else
      echo "Process is not running."
    fi
    

    WARNING!!!

    Please keep in mind that you’re simply parsing the output of ps ax which means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g. ./running "mysql" will also match ‘mysqld’ processes). I highly recommend using which to check against a full path where possible.


    References:

    http://linux.about.com/od/commands/l/blcmdl1_ps.htm

    http://linux.about.com/od/commands/l/blcmdl1_grep.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a cross-platform library/algorithm that will convert between 32-bit and 16-bit floating point
I'm using Mac OSX but I need a platform independent method to print a
I need to access the process' environment block in a platform-independent manner. The python
I just need to know if WCF is platform independent like Webservices? Can the
I need to make one function in a module platform-independent by offering several implementations,
I need a cross platform solution for clearing the console in both Linux and
I need to run Linux-Apache-PHP-MySQL application (Moodle e-learning platform) for a large number of
I need a simple application, preferably a cross-platform one, that enables sending of files
In Java I have code that works well on OSX but not in linux.
I need to get the following code working platform independent: timeval tv; tv.tv_sec =

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.