A wrapper around a command needs to distinguish normal completion, an application error and an expired deadline. Treating every nonzero status as a timeout hides useful information. This example runs three bounded fixtures and verifies their actual statuses.

It uses GNU timeout around a command argument list. The wrapper captures the result immediately and compares it with the fixture’s expected value. No services, background jobs or scheduled tasks are changed.

Prerequisites

Use Linux with Bash, GNU coreutils timeout and sleep. Check availability with timeout --version. Save the following script as deadline.sh. Run it in a scratch directory; it creates no files.

Complete example

#!/usr/bin/env bash
set -u
run_case() {
  local expected=$1 label=$2 status
  shift 2
  if timeout --kill-after=1s 0.2s "$@"; then
    status=0
  else
    status=$?
  fi
  if [[ $status != "$expected" ]]; then
    printf 'unexpected status %s for %s\n' "$status" "$label" >&2
    return 1
  fi
  printf '%s=%s\n' "$label" "$status"
}
run_case 0 success bash -c 'exit 0' || exit 1
run_case 7 application-error bash -c 'exit 7' || exit 1
run_case 124 deadline bash -c 'exec sleep 5' || exit 1

Run and expected output

bash deadline.sh
success=0
application-error=7
deadline=124

Understand the status handling

The command is placed directly in an if condition. The success branch records zero; the failure branch captures the command’s status before another command can overwrite it. Quoted "$@" forwards the remaining arguments with their original boundaries.

The first child exits normally. The second deliberately returns seven, demonstrating that an application failure survives the wrapper. The third replaces its shell with sleep and outlives the 0.2-second deadline, exercising timeout’s default termination behavior. The one-second kill-after setting provides an escalation interval if initial termination does not stop a child.

Each call has an explicit failure handler. If an observed status differs from the fixture expectation, the script exits unsuccessfully instead of printing a misleading success report. The wrapper’s own return value describes whether the test matched; the printed value describes the command result.

Edge cases and troubleshooting

Under the demonstrated GNU defaults, deadline expiration normally produces 124. GNU documents separate statuses for timeout failures, invocation problems and forced killing. The example does not use preserve-status, which changes how deadline outcomes are reported.

An application can itself return 124. Therefore, a numeric status alone is not universal proof that a deadline expired. For operational tooling, document reserved application statuses or record additional supervision information. Do not promise an unambiguous distinction that the exit-code interface cannot provide.

The short deadline is useful for this small fixture, not a production recommendation. A heavily loaded machine could delay even the quick child processes. If the first fixtures unexpectedly time out, rerun with a larger demonstration deadline and a correspondingly longer sleep, then investigate host load.

This execution exercised normal termination after the deadline. It did not test a child that ignores termination, forced-kill status, detached descendants or interactive terminal behavior. Keep those cases separate when designing a supervisor for a more complicated process tree.

References and related tutorials

The official GNU timeout manual defines deadlines, escalation and exit statuses. Browse Linux tutorials.