Linux command to check if a shell script is running or not

前端 未结 9 2120
栀梦
栀梦 2020-12-04 14:55

What is the linux command to find if a process say aa.sh is running or not. ps command does not seem to work and it does not show the shell script names.

Please advi

相关标签:
9条回答
  • 2020-12-04 15:03

    Adding to the answers above -

    To use in a script, use the following :-

    result=`ps aux | grep -i "myscript.sh" | grep -v "grep" | wc -l`
    if [ $result -ge 1 ]
       then
            echo "script is running"
       else
            echo "script is not running"
    fi
    
    0 讨论(0)
  • 2020-12-04 15:05

    I was quite inspired by the last answer by mklement0 - I have few scripts/small programs I run at every reboot via /etc/crontab. I built on his answer and built a login script, which shows if my programs are still running. I execute this scripts.sh via .profile -file on every login, to get instant notification on each login.

    cat scripts.sh 
    #!/bin/bash
    
    getscript() {
      pgrep -lf ".[ /]$1( |\$)"
    }
    
    script1=keepalive.sh
    script2=logger_v3.py
    
    # test if script 1 is running
    if getscript "$script1" >/dev/null; then
      echo "$script1" is RUNNING
      else
        echo "$script1" is NOT running
    fi
    
    # test if script 2 is running:
    if getscript "$script2" >/dev/null; then
      echo "$script2" is RUNNING
      else
        echo "$script2" is NOT running
    fi
    
    0 讨论(0)
  • 2020-12-04 15:06

    The simplest and efficient solution is :

    pgrep -fl aa.sh
    
    0 讨论(0)
  • 2020-12-04 15:07

    here a quick script to test if a shell script is running

    #!/bin/sh
    
    scripToTest="your_script_here.sh"
    scriptExist=$(pgrep -f "$scripToTest")
    [ -z "$scriptExist" ] && echo "$scripToTest : not running" || echo "$scripToTest : runnning"
    
    0 讨论(0)
  • 2020-12-04 15:18

    Check this

    ps -ef | grep shellscripname.sh
    

    You can also find your running process in

    ps -ef
    
    0 讨论(0)
  • 2020-12-04 15:18

    The solutions above are great for interactive use, where you can eyeball the result and weed out false positives that way.

    False positives can occur if the executable itself happens to match, or any arguments that are not script names match - the likelihood is greater with scripts that have no filename extensions.

    Here's a more robust solution for scripting, using a shell function:

    getscript() {
      pgrep -lf ".[ /]$1( |\$)"
    }
    

    Example use:

    # List instance(s) of script "aa.sh" that are running.
    getscript "aa.sh"  # -> (e.g.): 96112 bash /Users/jdoe/aa.sh
    
    # Use in a test:
    if getscript "aa.sh" >/dev/null; then
      echo RUNNING
    fi
    
    • Matching is case-sensitive (on macOS, you could add -i to the pgrep call to make it case-insensitive; on Linux, that is not an option.)
    • The getscript function also works with full or partial paths that include the filename component; partial paths must not start with / and each component specified must be complete. The "fuller" the path specified, the lower the risk of false positives. Caveat: path matching will only work if the script was invoked with a path - this is generally true for scripts in the $PATH that are invoked directly.
    • Even this function cannot rule out all false positives, as paths can have embedded spaces, yet neither ps nor pgrep reflect the original quoting applied to the command line. All the function guarantees is that any match is not the first token (which is the interpreter), and that it occurs as a separate word, optionally preceded by a path.
    • Another approach to minimizing the risk of false positives could be to match the executable name (i.e., interpreter, such as bash) as well - assuming it is known; e.g.
    # List instance(s) of a running *bash* script.
    getbashscript() {
      pgrep -lf "(^|/)bash( | .*/)$1( |\$)"
    }
    

    If you're willing to make further assumptions - such as script-interpreter paths never containing embedded spaces - the regexes could be made more restrictive and thus further reduce the risk of false positives.

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