rokkonet

PC・Androidソフトウェア・アプリの開発・使い方に関するメモ

引数に指定したコマンドが稼働中か調べるシェルスクリプト

2020 Apr. 21.
2020 Apr. 19.


isAnyRunning.sh

#!/bin/bash
# 2020 Apr. 20.
# 2020 Apr. 19.
# Ryuichi Hashimoto.

# check commands that you gave on arguement are running.

# usage: ${CMD} COMMAND1 [COMMAND2 COMMAND3...]
# check COMMANDS that you gave on arguements are running.
# partial match of command-name is possible.
# return value:
#   0: any inquireing COMMAND is running
#   1: error
#   2: inquireing COMMANDS are not running


CMD=`basename $0`

function usage() {
cat << EOP
usage: ${CMD} COMMAND1 [COMMAND2 COMMAND3...]
check COMMANDS that you gave on arguement are running.
partial match of command-name is possible.
return value:
  0: any inquireing COMMAND is running
  1: error
  2: inquireing COMMANDS are not running
EOP
}


############
### main ###
############
if [ $# -lt 1 ]
then
  usage 1>&2
  exit 1
fi

## get command-line-arguements
ArguesPipe=''
Argues=($@)
for EachArgue in ${Argues[@]}
do
   ArguesPipe=${ArguesPipe}${EachArgue}\|
done
ArguesPipe=${ArguesPipe%\|}

## get parent process which started this script to avoid to search parent process in ps-command-result 
# 6th col of "ps ax" is parent command
# after 7th cols are arguements of parent command
HasParent="no"
NumColPs=`ps ax | grep $PPID | grep -v 'grep' | awk '$1 == '"${PPID}"' {print NF}'`
if [ ${NumColPs} -gt 5 ]
then
  ## case of some parent command started this script
  # get parant command & arguements to store to array
  #   String in ParentStr of result of awk is separated by LF not space.
  HasParent="yes"
  ParentStr=`ps ax | grep $PPID | grep -v 'grep' | awk '{for (i = 6; i <= NF; i++ ) print $i }'`
  # replace LF to space in ParentStr
  ParentStr=`echo $ParentStr | sed 's/\n/ /g'`
  ParentStrArray=($ParentStr)
fi

if [ ${HasParent} = "yes" ]
then
  ps ax -f | grep -v grep | grep -v ${CMD} | grep -E ${ArguesPipe} | grep -v ${ParentStrArray[0]} > /dev/null 2>&1
else
  ps ax -f | grep -v grep | grep -v ${CMD} | grep -E ${ArguesPipe} > /dev/null 2>&1
fi
ResultCode=$?
if [ ${ResultCode} -ne 0 ];then
   # not matched to any command
   \exit 2
fi
# matched to any command
\exit 0



上記のisAnyRunning.shを複数回連続起動するスクリプト
isAnyRunningLoop.sh

#!/bin/bash
# 2020 Apr. 19.
# Ryuichi Hashimoto.

# run isAnyRunning.sh 5 times every 1 second.
# check COMMANDS that you gave on arguements are running.
# partial match of command-name is possible.
# return value:
#   0: any inquireing COMMAND is running
#   1: error
#   2: inquireing COMMANDS are not running
#
# return 0 if script return 0 at least 1 time in loop.


CmdLoop=`basename $0`
export CmdLoop

# loop-count
NumLoop=5

# interval seconds
SleepSec=1

# set default-retrun-code.
Result=0


function usage() {
cat << EOP
usage: ${CmdLoop} COMMAND1 [COMMAND2 COMMAND3...]
run isAnyRunning.sh 5 times every 1 second.
check COMMANDS that you gave on arguements are running.
partial match of command-name is possible.
return value:
  0: any inquireing COMMAND is running
  1: error
  2: inquireing COMMANDS are not running

return 0 if script return 0 at least 1 time in loop.
EOP
}


############
### main ###
############
if [ $# -lt 1 ]
then
  usage 1>&2
  exit 1
fi

# get pipe-connected-arguements-string
ArguesPipe=''
Argues=($@)
for EachArgue in ${Argues[@]}
do
   ArguesPipe=${ArguesPipe}${EachArgue}\|
done
ArguesPipe=${ArguesPipe%\|}

IsRunning="isAnyRunning.sh ${ArguesPipe}"

while [ $NumLoop -gt 0 ]; do
  ${IsRunning}
  ResultCode=$?
  if [ 0 -eq $ResultCode ]; then
    # when some commands are running
    exit 0
  fi
  sleep 1
  NumLoop=$(($NumLoop - 1))
done
exit $ResultCode