Alarm clock Set Timeout For A Shell Command

last updated in Categories , , , , , , , , , , ,

How can I run a command called foo, and have it timeout/abort after 10 seconds under GNU/Linux running bash shell or script? How do I run the command under an alarm clock?

You should able to use python / ruby / php or perl to set alarm clock timer to timeout for a shell command. Shell do not have in built facility for timeout a command.

Advertisements

Alarm clock Set Timeout For A Shell Command

Let us see various examples

Perl program

Here is a sample perl code:

perl -e 'alarm shift @ARGV; exec @ARGV' 5 foo arg1 arg2

You may define it as shell function:

alarm() { perl -e 'alarm shift; exec @ARGV' "$@"; }

And call it as follows (wait 20 seconds before alrming foo command:)

alarm 20 foo arg1

Sample shell script to set timeout for a command

You can use following shell script to set timeout for a command:

##########################################################################
# Shellscript:	timeout - set timeout for a command 
#               Alarm clock Set Timeout
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	29.07.1999
# Category   :	File Utilities
# Requires   :	
# SCCS-Id.   :	@(#) timeout	1.3 03/03/18
##########################################################################
# Description
#    o	Runs a command, and terminates it (by sending a signal) after
#	a specified time period
#    o	This command first starts itself as a "watchdog" process in the
#	background, and then runs the specified command.
#	If the command did not terminate after the specified
#	number of seconds, the "watchdog" process will terminate
#	the command by sending a signal.
#
# Notes
#    o	Uses the internal command line argument "-p" to specify the
#	PID of the process to terminate after the timeout to the
#	"watchdog" process.
#    o	The "watchdog" process is invoked by the name "$0", so
#	"$0" must be a valid path to the script.
#    o	If this script runs in the environment of the login shell
#	(i.e. it was invoked using ". timeout command...") it will
#	terminate the login session.
##########################################################################
 
PN=`basename "$0"`			# Program name
VER='1.3'
 
TIMEOUT=5				# Default [seconds]
 
Usage () {
    echo >&2 "$PN - set timeout for a command, $VER
usage: $PN [-t timeout] command [argument ...]
     -t: timeout (in seconds, default is $TIMEOUT)"
    exit 1
}
 
Msg () {
    for MsgLine
    do echo "$PN: $MsgLine" >&2
    done
}
 
Fatal () { Msg "$@"; exit 1; }
 
while [ $# -gt 0 ]
do
    case "$1" in
	-p)	ParentPID=$2; shift;;	# Used internally!
	-t)	Timeout="$2"; shift;;
	--)	shift; break;;
	-h)	Usage;;
	-*)	Usage;;
	*)	break;;			# First file name
    esac
    shift
done
 
: ${Timeout:=$TIMEOUT}			# Set default [seconds]
 
if [ -z "$ParentPID" ]
then
    # This is the first invokation of this script.
    # Start "watchdog" process, and then run the command.
    [ $# -lt 1 ] && Fatal "please specify a command to execute"
    "$0" -p $$ -t $Timeout &		# Start watchdog
    #echo >&2 "DEBUG: process id is $$"
    exec "$@"				# Run command
    exit 2				# NOT REACHED
else
    # We run in "watchdog" mode, $ParentPID contains the PID
    # of the process we should terminate after $Timeout seconds.
    [ $# -ne 0 ] && Fatal "please do not use -p option interactively"
 
    #echo >&2 "DEBUG: $$: parent PID to terminate is $ParentPID"
 
    exec >/dev/null 0<&1 2>&1	# Suppress error messages
    sleep $Timeout
    kill $ParentPID &&			# Give process time to terminate
    	(sleep 2; kill -1 $ParentPID) &&
	(sleep 2; kill -9 $ParentPID)
    exit 0
fi

(Credit: Heiner Steven)

doalarm c program

Download doalarm program using the wget command or curl command
wget https://www.cyberciti.biz/files/doalarm-0.1.7.tgz
OR
$ wget http://pilcrow.madison.wi.us/sw/doalarm-0.1.7.tgz
Untar program using the tar command
$ tar -zxvf doalarm-0.1.7.tg
Compile doalarm:
$ cd doalarm-0.1.7
$ make
$ ./doalarm

Sample output:

Error: missing required parameter
Usage: doalarm [-hr] [-t type] sec command [arg...]
Run command under an alarm clock.

Options:
 -t          Type of timer: 'real' (SIGALRM), 'virtual' (SIGVTALRM),
    --timer= 'profile' (SIGPROF), 'cpu' (SIGXCPU).  Default 'real'.
 -r --recur        Recurring alarm, every sec seconds.
 -h --help         Show help text (this message).
    --version      Display version.

doalarm 0.1.7 (14 Dec 2001)

Run foo as follows:
$ doalarm 20 foo
The doalarm executes command fooo under an impending alarm. The default timer expires with a (normally fatal) SIGALRM after secs real seconds. Available process timers come in two types: interval and limit. See OPTIONS below for timers and respective signals. Interval timers may alarm just once, or may recurrently alarm (-r), signalling every secs seconds. For interval timers, a secs value of zero clears an inherited alarm, if any. For the limit timer, zero raises command’s cpu limit to the hard maximum (typically unlimited).

How to timeout a command in Linux or Unix shell

Set sleep time with the command, && condition sa follows:
command1 arg1 & sleep 10 ; kill $!
/path/to/foo bar & sleep 5 ; kill $!

say hello to timeout command

GNU/Linux comes timeout command. It runs a command with a time limit. This page explains how to start a command, and kill it if the specified timeout expires:
timeout DURATION COMMAND
timeout DURATION COMMAND arg1 arg2
timeout 1m ping nixcraft.com

Conclusion

Alarm clock Set Timeout with doalarm and perl code for linux and unix
You learned how to run Linux or Unix commands under an alarm clock to set Timeout, either setitimer(2) or RLIMIT_CPU. Apart from C code one can use Perl or shell too.

ADVERTISEMENTS

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Start the discussion at www.nixcraft.com

Historical Comment Archive

16 comment

  1. You can also do it with bash using sleep and job control:
    $ foo & sleep 10 && kill %1 && fg

  2. Good one, I have to download and use it. Thanks.

    The tip from Professor Fapsanders is also useful, we can make it more significant to kill the last background process by this:

    $ sh foo & sleep 5 && kill $! && fg

    // Jadu, unstableme.blogspot.com

  3. Warning: the tip from Professor Fapsanders only works if “foo” does not require any input (background processes cannot do that).

    For the general case, you need to background the alarm process instead, as the sample “timeout” script does.

    1. $ sh foo & sleep 5 && kill $! & fg ### (note the last “&” is not “&&”, so you foreground the job immediately)

  4. How can i suppress the stupid ‘alarm clock’ output when the timeout expires?
    I’m running “perl -e ‘alarm shift @ARGV; exec @ARGV’ 5 cat” it terminates the cat command after 4 seconds but then i get stupid ‘Alarm clock’ output on the screen. I have to use this command in a shell script and i don’t want this output.

    I have tried redirecting standard error and std out but if i specify >/dev/null at the end of cat command, it redirects ‘cat”s output not from perl -e….. Any ideas? Thanks a lot

    1. even m facing the error, in the code below

      #include
      #include
      #include
      using namespace std;
      void onAlarm(int a)
      {
      cout<<"Alarm Buzzz\n";
      sleep(2);
      exit(0);
      }
      int main()
      {
      signal(SIGALRM, onAlarm);
      cout<<"enter\n";
      alarm(3);
      execl("./sant/a.out","./a.out",(char*)0);
      }

      if the file a.out has a sleep of say 10 seconds
      then it wont execute the onAlarm() function, instead it would just show Alarm clock
      can u help?

  5. Thanks Peter.

    But the problem is appending >/dev/null 2>&1 to the command redirects stdout and stderr for the “command” not for the perl. So even by appending this to the command i still get “Alarm Clock” after command terminates.

    For example perl -e ‘alarm shift @ARGV; exec @ARGV’ 5 foo arg1 arg2 > /dev/null 2>&1 ‘ will redirect output and stderr from ‘foo’ command not from the alarm command.
    Thanks

  6. hey, you’re right… that’s funny.
    I tried to dig some deeper using strace and funnily it does not show up then:

    strace perl -e “alarm shift @ARGV; exec @ARGV” 5 cat >/dev/null 2>&1

    interesting issue, I do lack enough time to investigate
    regards

  7. Thanks. Using the perl snippet is neat. Pretty much all UNIX systems come with bash and perl. So, this is cool.

    Still, have a question? Get help on our forum!