回显未运行的进程列表

回显未运行的进程列表

我开发了一个脚本,该脚本引用一个文件来启动该服务器上所需的进程。脚本在 180 秒后超时。在脚本的末尾,我想显示在 180 秒的时间范围内尚未启动的进程。我该如何实现这一目标?

参考文件ref.txt

#TEST:process1
#TEST:process2
TEST:Process3
TEST:process4
TEST:process5

脚本中用于启动所需进程的命令行是:

grep "TEST:" /products/test/ref.txt | while read LINE (Selects process3,4,5 to start)
do
PROC_NAME=$(echo ${LINE} | awk -F ':' '{print $2}')
echo "Starting the required process"
echo "<command line to start the processes>"
done 

#Giving script 180 seconds to start the processes

(( START_TIME = 0 ))
until [ ${START_TIME} -gt 180 ]
do 
greps for the processes running
echo " N number of processes are running"
<I want to display the processes which have not started here>
done

脚本在 180 秒后超时。

我需要添加一个逻辑来显示 180 秒后尚未启动的进程,我该怎么做?

答案1

bash假设有一个类似or 的shell ksh

max_time=3

SECONDS=0

IFS=$'\n:'

grep -v '^#' ref.txt |
while read tag cmd; do
    if (( SECONDS < max_time )); then
        echo "starting '$cmd' (tag is '$tag')"
        sleep 2 # for simulation purposes
    else
        echo "did not have time to start '$cmd' (tag is '$tag')"
    fi
done

这将给出以下输出:

$ ksh script.sh
starting 'Process3' (tag is 'TEST')
starting 'process4' (tag is 'TEST')
did not have time to start 'process5' (tag is 'TEST')

该脚本从文件中挑选出所有ref.txt未注释掉的行,并将它们拆分:以将初始标记与命令分开。

如果脚本尚未超时,它将启动当前的读取进程(通过sleep此处的调用进行模拟)。

如果它使用了太多时间,它不会启动更多进程,但会报告尚未启动的进程。

超时不会异步发生,即如果时间超过几秒,脚本不会中断进程的启动max_time

SECONDS变量包含自 shell 启动以来的秒数,或者在本例中,自上次将其设置为零以来的秒数。

答案2

显示 180 秒后尚未启动的进程的逻辑。假设 shell 是 bourne shell。我测试bashzsh

# ...

# generate a timestamp in seconds since UNIX epoch
START_TIME=$(date '+%s')
until [[ $(date +%s) -gt $((START_TIME + 180)) ]]
do 
    # greps for the processes running
    echo " N number of processes are running"
    #<I want to display the processes which have not started here>
done

# ...

相关内容