在命令运行期间超时后执行操作

时间:2015-05-07 22:16:13

标签: bash timeout

这是我用于在后台运行命令并计算指定时间段过后所需操作时间的脚本。如果我早于计划杀死主脚本,还有一些陷阱会杀死在后台启动的长时间运行命令。

#!/bin/bash
# DESCRIPTION:
# Run long running command in background for number of seconds
# specified in $TIME_TO_RUN. After this period the function
# 'func_action' will be called

func_trap () {
  # FOLLOWING LINE ADDED ACORDING TO Barmar SUGGESTION:
  # http://stackoverflow.com/questions/30112607/taking-action-after-timeout-during-the-command-running
  for pid in $(jobs -p); do ps -p $pid; done

  CHILDREN=$(jobs -p)
  if [ -n "$CHILDREN" ]; then
    echo "Following children will be killed"
    jobs -p
    kill $(jobs -p)
  else
    echo "There are no children running"
  fi
  exit
}

func_action() {
    echo "FUNCTION ACTION CALLED"
}
###################################
trap func_trap INT TERM KILL

LOG_PATH="/tmp/log_file"
TIME_TO_RUN=10
TIME_START=$(date +%s)
TIME_STOP=$(( TIME_START + TIME_TO_RUN ))

# COMMAND RUNNING IN BACKGROUND FOR INFINITE
while true;       do date;    sleep 1; done > "$LOG_PATH" &

# COMMAND RUNNING IN BACKGROUND FOR LONG TIME
# for i in `seq 1 5`; do echo $i; sleep 1; done > "$LOG_PATH" &

BKGRD_CMD_PID=$!

echo "BKGRD_CMD_PID: $BKGRD_CMD_PID"
echo "PID: $BKGRD_CMD_PID IS WRITING TO: $LOG_PATH"
echo "MYPID:         $$"
echo "TIME_START:    $TIME_START"
echo "TIME_STOP:     $TIME_STOP"
echo "jobs -p:       $(jobs -p)"

# TODO: TRY TO USE SLEEP INSTEAD OF LOOP
while true; do
  if [ $(date +%s) -lt $TIME_STOP ]; then
    echo -n "Long running command is still running at time: "
    date +%s
    sleep 1
  else
    kill -s 0 "$BKGRD_CMD_PID" 2>/dev/null && func_action
    kill -s 0 "$BKGRD_CMD_PID" 2>/dev/null && kill "$BKGRD_CMD_PID" && echo "Process with PID: $BKGRD_CMD_PID killed"
    break
  fi
done

当我使用ctrl + c终止此脚本时,我收到以下消息:

└──> time_counter.sh
BKGRD_CMD_PID: 7551
PID: 7551 IS WRITING TO: /tmp/log_file
MYPID:         7549
TIME_START:    1431161303
TIME_STOP:     1431161313
jobs -p:       7551
Long running command is still running at time: 1431161303
Long running command is still running at time: 1431161304
Long running command is still running at time: 1431161305
Long running command is still running at time: 1431161306
^C  PID TTY          TIME CMD
 7551 pts/4    00:00:00 time_counter.sh
  PID TTY          TIME CMD
Following children will be killed
7551
7572
7578
7579
/home/wakatana/bin/time_counter.sh: line 16: kill: (7572) - No such process
/home/wakatana/bin/time_counter.sh: line 16: kill: (7578) - No such process
/home/wakatana/bin/time_counter.sh: line 16: kill: (7579) - No such process

一般来说,它按预期工作我只想澄清三件事:

  1. 为什么我收到有关PID 7572,7578,7579的错误?这些PID属于哪个进程?
  2. 这种做法是否正确或有更好的方法可以做到这一点?
  3. 如果没有bash抱怨未知命令,如何将func_trapfunc_action移到脚本末尾?
  4. PS:我第一次考虑timeout命令,但似乎只能在指定的时间段后发送杀信号,并且不做任何其他操作。

1 个答案:

答案 0 :(得分:1)

我怀疑这是一个bash错误。当陷阱处理程序正在运行时,它似乎认为它从主循环中断的sleep 1(保持打印的那个"长时间运行命令")是待处理作业。

而不是kill $(jobs -p),您应该kill $BKGRD_CMD_PID