Linux/Unix command to determine if process is running?

后端 未结 14 918
忘掉有多难
忘掉有多难 2020-11-28 01:54

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

相关标签:
14条回答
  • 2020-11-28 02:18

    The simpliest way is to use ps and grep:

    command="httpd"
    running=`ps ax | grep -v grep | grep $command | wc -l`
    if [ running -gt 0 ]; then
        echo "Command is running"
    else
        echo "Command is not running"
    fi
    

    If your command has some command arguments, then you can also put more 'grep cmd_arg1' after 'grep $command' to filter out other possible processes that you are not interested in.

    Example: show me if any java process with supplied argument:

    -Djava.util.logging.config.file=logging.properties

    is running

    ps ax | grep -v grep | grep java | grep java.util.logging.config.file=logging.properties | wc -l
    
    0 讨论(0)
  • 2020-11-28 02:22

    Just a minor addition: if you add the -c flag to ps, you don't need to remove the line containing the grep process with grep -v afterwards. I.e.

    ps acux | grep cron
    

    is all the typing you'll need on a bsd-ish system (this includes MacOSX) You can leave the -u away if you need less information.

    On a system where the genetics of the native ps command point back to SysV, you'd use

    ps -e |grep cron
    

    or

    ps -el |grep cron 
    

    for a listing containing more than just pid and process name. Of course you could select the specific fields to print out using the -o <field,field,...> option.

    0 讨论(0)
  • 2020-11-28 02:22

    The following shell function, being only based on POSIX standard commands and options should work on most (if not any) Unix and linux system. :

    isPidRunning() {
      cmd=`
        PATH=\`getconf PATH\` export PATH
        ps -e -o pid= -o comm= |
          awk '$2 ~ "^.*/'"$1"'$" || $2 ~ "^'"$1"'$" {print $1,$2}'
      `
      [ -n "$cmd" ] &&
        printf "%s is running\n%s\n\n" "$1" "$cmd" ||
        printf "%s is not running\n\n" $1
      [ -n "$cmd" ]
    }
    

    $ isPidRunning httpd
    httpd is running
    586 /usr/apache/bin/httpd
    588 /usr/apache/bin/httpd
    
    $ isPidRunning ksh
    ksh is running
    5230 ksh
    
    $ isPidRunning bash
    bash is not running
    

    Note that it will choke when passed the dubious "0]" command name and will also fail to identify processes having an embedded space in their names.

    Note too that the most upvoted and accepted solution demands non portable ps options and gratuitously uses a shell that is, despite its popularity, not guaranteed to be present on every Unix/Linux machine (bash)

    0 讨论(0)
  • 2020-11-28 02:24

    I use pgrep -l httpd but not sure it is present on any platform...
    Who can confirm on OSX?

    0 讨论(0)
  • 2020-11-28 02:24

    You should know the PID of your process.

    When you launch it, its PID will be recorded in the $! variable. Save this PID into a file.

    Then you will need to check if this PID corresponds to a running process. Here's a complete skeleton script:

    FILE="/tmp/myapp.pid"
    
    if [ -f $FILE ];
    then
       PID=$(cat $FILE)
    else
       PID=1
    fi
    
    ps -o pid= -p $PID
    if [ $? -eq 0 ]; then
      echo "Process already running."  
    else
      echo "Starting process."
      run_my_app &
      echo $! > $FILE
    fi
    

    Based on the answer of peterh. The trick for knowing if a given PID is running is in the ps -o pid= -p $PID instruction.

    0 讨论(0)
  • 2020-11-28 02:28

    Here is my version. Features:

    • checks for exact program name (first argument of the function). search for "mysql" will not match running "mysqld"
    • searches program arguments (second argument of the function)

    script:

    #!/bin/bash
    
    # $1 - cmd
    # $2 - args
    # return: 0 - no error, running; 1 - error, not running
    function isRunning() {
        for i in $(pidof $1); do
            cat /proc/$i/cmdline | tr '\000' ' ' | grep -F -e "$2" 1>&2> /dev/null
            if [ $? -eq 0 ]; then
                return 0
            fi
        done
        return 1
    }
    
    isRunning java "-Djava.util.logging.config.file=logging.properties"
    if [ $? -ne 0 ]; then
        echo "not running, starting..."
    fi
    
    0 讨论(0)
提交回复
热议问题