Taking action after timeout during command execution

This is the script I am using to run a command in the background and calculate the time it takes for an action to occur after a specified period. There are also pitfalls that will kill a long running command running in the background if I kill the main script earlier than I planned.

#!/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

      

When I kill this script with ctrl + c, I get the following message:

└──> 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

      

Overall it works as expected, I just want to clarify three things:

  • Why am I getting an error regarding PIDs 7572, 7578, 7579? Which process do these PIDs belong to?
  • Is this approach OK or is there a better way to do it?
  • How to move func_trap

    and func_action

    to the end of a script without bash complaining about an unknown command?

PS: The first time I thought about the command timeout

, but it looks like it can only send kill signals after the specified time period and not do any other action.

+3


source to share


1 answer


I suspect this is a bug bash

. While the hook handler is running, it appears to be sleep 1

interrupted from the main loop (the one that keeps printing "Long running command") like a pending job.



Instead, kill $(jobs -p)

you have to do kill $BKGRD_CMD_PID

.

+1


source







All Articles