Check number of running scripts using ps

后端 未结 4 406
慢半拍i
慢半拍i 2021-01-15 06:09

I\'m writing a script (show_volume.sh) which may be called several times in short intervals. I need a way to determine whether there is more than just one running instance o

相关标签:
4条回答
  • 2021-01-15 06:40

    At the moment ps runs a process grep show_volume.sh is also running, so you always count the grep!

    Simple solution, grep s[h]ow_volume.sh. The grep (not shell) will collapse the [h] to h and search for what you want, but your grep won't match itself because it will have [] in the parameter.

    pgrep is also useful for this task and is smart enough to always exclude itself.

    0 讨论(0)
  • 2021-01-15 06:42

    If you're running the script as different users then ps -a will only show instances for the current user and only those with an attached terminal. Use ps -ax or ps -e.

    pgrep -c
    

    will show a count without having to use wc.

    0 讨论(0)
  • 2021-01-15 06:53

    The solution provided by ajreal:

    ps -a | grep show_volume.sh | grep -v grep | wc -l

    should work. If it does not, please provide output of

    ps -a | grep show_volume.sh | grep -v grep

    here

    0 讨论(0)
  • 2021-01-15 06:54

    Try to exclude grep as well, as your grep itself also contains show_volume.sh, an example

    ps -a | grep show_volume.sh | grep -v grep | wc -l
    
    0 讨论(0)
提交回复
热议问题