Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:15:00+00:00 2026-05-11T10:15:00+00:00

This answer to Command line command to auto-kill a command after a certain amount

  • 0

This answer to Command line command to auto-kill a command after a certain amount of time

proposes a 1-line method to timeout a long-running command from the bash command line:

( /path/to/slow command with options ) & sleep 5 ; kill $! 

But it’s possible that a given "long-running" command may finish earlier than the timeout.
(Let’s call it a "typically-long-running-but-sometimes-fast" command, or tlrbsf for fun.)

So this nifty 1-liner approach has a couple of problems.
First, the sleep isn’t conditional, so that sets an undesirable lower bound on the time taken for the sequence to finish. Consider 30s or 2m or even 5m for the sleep, when the tlrbsf command finishes in 2 seconds — highly undesirable.
Second, the kill is unconditional, so this sequence will attempt to kill a non-running process and whine about it.

So…

Is there a way to timeout a typically-long-running-but-sometimes-fast ("tlrbsf") command that

  • has a bash implementation (the other question already has Perl and C answers)
  • will terminate at the earlier of the two: tlrbsf program termination, or timeout elapsed
  • will not kill non-existing/non-running processes (or, optionally: will not complain about a bad kill)
  • doesn’t have to be a 1-liner
  • can run under Cygwin or Linux

… and, for bonus points

  • runs the tlrbsf command in the foreground
  • any ‘sleep’ or extra process in the background

such that the stdin/stdout/stderr of the tlrbsf command can be redirected, same as if it had been run directly?

If so, please share your code. If not, please explain why.

I have spent awhile trying to hack the aforementioned example but I’m hitting the limit of my bash skills.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:15:01+00:00Added an answer on May 11, 2026 at 10:15 am

    I think this is precisely what you are asking for:

    http://www.bashcookbook.com/bashinfo/source/bash-4.0/examples/scripts/timeout3

    #!/bin/bash # # The Bash shell script executes a command with a time-out. # Upon time-out expiration SIGTERM (15) is sent to the process. If the signal # is blocked, then the subsequent SIGKILL (9) terminates it. # # Based on the Bash documentation example.  # Hello Chet, # please find attached a 'little easier'  :-)  to comprehend # time-out example.  If you find it suitable, feel free to include # anywhere: the very same logic as in the original examples/scripts, a # little more transparent implementation to my taste. # # Dmitry V Golovashkin <Dmitry.Golovashkin@sas.com>  scriptName='${0##*/}'  declare -i DEFAULT_TIMEOUT=9 declare -i DEFAULT_INTERVAL=1 declare -i DEFAULT_DELAY=1  # Timeout. declare -i timeout=DEFAULT_TIMEOUT # Interval between checks if the process is still alive. declare -i interval=DEFAULT_INTERVAL # Delay between posting the SIGTERM signal and destroying the process by SIGKILL. declare -i delay=DEFAULT_DELAY  function printUsage() {     cat <<EOF  Synopsis     $scriptName [-t timeout] [-i interval] [-d delay] command     Execute a command with a time-out.     Upon time-out expiration SIGTERM (15) is sent to the process. If SIGTERM     signal is blocked, then the subsequent SIGKILL (9) terminates it.      -t timeout         Number of seconds to wait for command completion.         Default value: $DEFAULT_TIMEOUT seconds.      -i interval         Interval between checks if the process is still alive.         Positive integer, default value: $DEFAULT_INTERVAL seconds.      -d delay         Delay between posting the SIGTERM signal and destroying the         process by SIGKILL. Default value: $DEFAULT_DELAY seconds.  As of today, Bash does not support floating point arithmetic (sleep does), therefore all delay/time values must be integers. EOF }  # Options. while getopts ':t:i:d:' option; do     case '$option' in         t) timeout=$OPTARG ;;         i) interval=$OPTARG ;;         d) delay=$OPTARG ;;         *) printUsage; exit 1 ;;     esac done shift $((OPTIND - 1))  # $# should be at least 1 (the command to execute), however it may be strictly # greater than 1 if the command itself has options. if (($# == 0 || interval <= 0)); then     printUsage     exit 1 fi  # kill -0 pid   Exit code indicates if a signal may be sent to $pid process. (     ((t = timeout))      while ((t > 0)); do         sleep $interval         kill -0 $$ || exit 0         ((t -= interval))     done      # Be nice, post SIGTERM first.     # The 'exit 0' below will be executed if any preceeding command fails.     kill -s SIGTERM $$ && kill -0 $$ || exit 0     sleep $delay     kill -s SIGKILL $$ ) 2> /dev/null &  exec '$@' 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 141k
  • Answers 141k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The issue seems to stem from both the NIC cards… May 12, 2026 at 8:02 am
  • Editorial Team
    Editorial Team added an answer ServiceController has a method WaitForStatus where you pass it an… May 12, 2026 at 8:02 am
  • Editorial Team
    Editorial Team added an answer If you need to select the algorithm at runtime, check… May 12, 2026 at 8:02 am

Related Questions

I'm pretty new to programming for Linux environments, so I don't exactly know what
(Bear with me, I promise this gets to shebang and windows.) I have about
Some background information, for a homework assignment I had to write a polish notation
This is similar to this question , but I wanted to flesh it out

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.