2021 Mar. 11.
2021 Feb. 20.
シェルスクリプトと同じプロセス名で、プロセスIDとグループプロセスIDのいずれもがシェルスクリプトのものと異なるプロセスがあれば多重起動と判断する
## Is another process of same name running? ## If there is a process that Both PID and PGID are different from this script' s, another process of same name as this process is running. ## Abort if another process of same name as this process is running. CmdPath="$0" # get GID of this process ProcList=$(ps ax -o pid,ppid,pgid) PidLines=$(echo "${ProcList}" | grep $$) Gid=$(echo "$PidLines" | awk -v Pid=$$ '{if ($1 == Pid) print $3}') # get PID that might be another process whose name is same as this script OtherPid=$(pgrep -fo "$CmdPath") # If OtherPid and Gid are different from this script's, another process is running. if [ $$ -ne $OtherPid ]; then OtherGid=$(echo "$ProcList" | awk -v Pid=$OtherPid '{if ($1 == Pid) print $3}') if [ $OtherGid -ne $Gid ]; then echo "Another ${CmdPath} is running." 1>&2 echo "Aborted." 1>&2 exit 1 fi fi