Run command after a certain length of time has elapsed?

Just run:

long-command & sleep 300; do-this-after-five-minutes

The do-this-after-five-minutes will get run after five minutes. The long-command will be running in the background.


You could use this script:

#!/bin/bash

TEMPFILE="$(mktemp)"
STARTTIME="$(date +%s)"
(./longprocess; rm -f "$TEMPFILE") &
while [ -f "$TEMPFILE" ]; do
    sleep 1s
    NOW="$(date +%s)"
    if (( (NOW-STARTTIME) % 300 == 0 )); then
        echo "$(( (NOW-STARTTIME)/60 )) minute(s) elapsed"
    fi
done
echo "Done!!!"

It executes your longprocess in a sub-shell and then monitors previously created 'lock' file for existence.


There is a one liner for this:

( ( sleep $TIMEOUT ; echo "5 minutes complete") & $COMMAND )

In your case TIMEOUT=5m and COMMAND is the long command.

Also see my answer to this post Timeout with 'service network restart'