0

Is there a way I can monitor the execution time of a script and generate notifications if the script is still running at milestone intervals?

AFAICS, wait in bash always blocks.

I intend using this in conjunction with STDOUT, STDERR capture to quieten down cron emails but I want notifications out if the script overruns its expected window (WITHOUT terminating it).

Google just keeps pointing me towards time and timeout which (in the absence of additional logic) are not what I'm looking for.

2 Answers 2

1

How about running your process in background and just monitoring it using something like the following loop? You can also set up a counter in this case, for example to terminate the process if it takes 3 times longer than it should

# Interval for checking status
readonly interval=10

# Your task here, maybe with some output redirections
sleep 100 &

# Infinite loop
while true
do
    # Chill some time
    sleep "$interval"

    # Check if the background process still exists
    if ps -p "$!" &> /dev/null
    then
        # Do something here, again maybe with >> logfile
        echo "Process is still running!"
    fi
done
2
  • That would work very well - and I'm now banging my head off the desk for not thinking of it myself :)
    – symcbean
    Commented May 23, 2023 at 13:55
  • Sometimes this happens to everyone. You're welcome! Moreover, now I can leave comments, so thank you lmao
    – xezo360hye
    Commented May 24, 2023 at 15:20
0

You may put this lines ahead of your script, and tune it. If execution of your task has different status, like in_queue and started, you write a corresponding signal to &$fdtick file descriptor, the timer will repeat that signal at intervals, also printing a newline every minute. Your loop may also include other watchdog functions of course.

NOTE: >( ... ) is called process substitution and is a bash syntax that create and give a name to a pipe between a running process and a new coprocess. <( ... ) does the same when the creator holds the read-end of the pipe.

############################################################
#                        VISUAL TIMER                      #
# >$fdtick : tick input file descriptor                    #
# >stderr : visual output                                  #
############################################################
exec {fdtick}> >(
  ((tick = 2, begintime = SECONDS))
  while read -t $tick dot || { (( $? > 128)) && dot=${odot:-#}; }; do
    odot=$dot
    (( lastmin = durationmin, durationtick = (duration = SECONDS - begintime) / tick, durationmin = duration / 60 ))
    while (( durationtick > lasttick )) ; do echo -n "$dot " ; ((lasttick++)) ; done
    (( durationmin == lastmin )) || echo $durationmin
  done >&2
) &&
trap "$(trap -p EXIT)"$'\n'"exec {fdtick}>&-" EXIT


sleep 50 ## long starting step
echo '.'>&$fdtick ## say the script is going on
sleep 50 ## long working step
## terminated, cleanup by trap EXIT

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .