Check if a process is running using Bash

后端 未结 7 927
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 18:53

I am trying to check if a process is running with the code below:

SERVICE=\"./yowsup/yowsup-cli\"
RESULT=`ps aux | grep $SERVICE`

if [ \"${RESULT:-null}\" =         


        
相关标签:
7条回答
  • 2020-12-19 19:18

    Use pgrep:

    if pgrep "$SERVICE" >/dev/null 2>&1 ; then
        echo "$SERVICE is running"
    fi
    

    or, more reliable:

    if pgrep -f "/path/to/$SERVICE" >/dev/null 2>&1 ; then
        echo "$SERVICE is running"
    fi
    
    0 讨论(0)
  • 2020-12-19 19:22

    For systems where pgrep isn't available you can use:

    service="[.]/yowsup/yowsup-cli"
    
    if ps aux | grep -q "$service"; then
        echo "not running"
    else
        echo "running"
    fi
    
    • [.] in will force grep to not list itself as it won't match [.] regex.
    • grep -q can be utilized to avoid command substitution step.
    • Prefer using lowercase variables in shell.
    0 讨论(0)
  • 2020-12-19 19:25

    I thought pidof was made for this.

    function isrunning()
    {
        pidof -s "$1" > /dev/null 2>&1
        status=$?
        if [[ "$status" -eq 0 ]]; then
            echo 1
        else
            echo 0
        fi
    )
    if [[ $(isrunning bash) -eq 1 ]]; then echo "bash is running"; fi
    if [[ $(isrunning foo) -eq 1 ]]; then echo "foo is running"; fi
    
    0 讨论(0)
  • 2020-12-19 19:33
    current_pid="$$" # get current pid
    # Looking for current pid. Don't save lines either grep or current_pid
    isRunning=$(ps -fea | grep -i $current_pid | grep -v -e grep -e $current_pid)
    
    # Check if this script is running
    if [[ -n "$isRunning" ]]; then
        echo "This script is already running."
    fi
    
    0 讨论(0)
  • 2020-12-19 19:34

    The problem is that grep you call sometimes finds himself in a ps list, so it is good only when you check it interactively:

    $ ps -ef | grep bash
    ...
    myaut    19193  2332  0 17:28 pts/11   00:00:00 /bin/bash
    myaut    19853 15963  0 19:10 pts/6    00:00:00 grep --color=auto bash
    

    Easiest way to get it is to use pidof. It accepts both full path and executable name:

    service="./yowsup/yowsup-cli" # or service="yowsup-cli"
    if pidof "$service" >/dev/null; then
        echo "not running"
    else
        echo "running"
    fi
    

    There is more powerful version of pidof -- pgrep.


    However, if you start your program from a script, you may save it's PID to a file:

    service="./yowsup/yowsup-cli"
    pidfile="./yowsup/yowsup-cli.pid"
    service &
    pid=$!
    echo $pid > $pidfile
    

    And then check it with pgrep:

    if pgrep -F "$pidfile" >/dev/null; then
        echo "not running"
    else
        echo "running"
    fi
    

    This is common technique in /etc/init.d start scripts.

    0 讨论(0)
  • 2020-12-19 19:34
    ## bash
    
    ## function to check if a process is alive and running:
    
    _isRunning() {
        ps -o comm= -C "$1" 2>/dev/null | grep -x "$1" >/dev/null 2>&1
    }
    
    ## example 1: checking if "gedit" is running
    
    if _isRunning gedit; then
        echo "gedit is running"
    else
        echo "gedit is not running"
    fi
    
    ## example 2: start lxpanel if it is not there
    
    if ! _isRunning lxpanel; then
        lxpanel &
    fi
    
    ## or
    
    _isRunning lxpanel || (lxpanel &)
    

    Note: pgrep -x lxpanel or pidof lxpanel still reports that lxpanel is running even when it is defunct (zombie); so to get alive-and-running process, we need to use ps and grep

    0 讨论(0)
提交回复
热议问题