Killing previous instances at first time,if running

前端 未结 3 1077
没有蜡笔的小新
没有蜡笔的小新 2021-01-27 23:10

Following is my script \'Infinite.sh\' code, I am running this script in background,Before running this script, i want to kill all previous instances. But this is killing my cur

3条回答
  •  情话喂你
    2021-01-27 23:41

    Simply check if the pid is the same as the one of the running script.

    Something like this could work:

    #!/bin/bash
    
    pids=($(pgrep "$(basename "$0")"))
    
    for i in "${pids[@]}"
    do
        (( $$ != i)) && kill "$i"
    done
    
    while :; do sleep 1;done
    ~                        
    

提交回复
热议问题