Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Execute a command with a timeout

Terminal - Execute a command with a timeout
timeout 10 sleep 11
2009-09-23 14:59:40
User: syssyphus
Functions: sleep
21
Execute a command with a timeout

Alternatives

There is 1 alternative - vote for the best!

Terminal - Alternatives
timeout 5s COMMAND
very_long_command& sleep 10; kill $!
2010-04-29 20:43:13
User: dooblem
Functions: kill sleep
5

or "Execute a command with a timeout"

Run a command in background, sleep 10 seconds, kill it.

! is the process id of the most recently executed background command.

You can test it with:

find /& sleep10; kill $!

perl -e "alarm 10; exec @ARGV" "somecommand"
2009-09-23 12:03:55
User: jgc
Functions: perl
4

In this example the command "somecommand" will be executed and sent a SIGALARM signal if it runs for more than 10 seconds. It uses the perl alarm function. It's not 100% accurate on timing, but close enough. I found this really useful when executing scripts and commands that I knew might hang E.g. ones that connect to services that might not be running. Importantly this can be used within a sequential script. The command will not release control until either the command completes or the timeout is hit.

$COMMAND 2>&1 >/dev/null & WPID=$!; sleep $TIMEOUT && kill $! & KPID=$!; wait $WPID
2010-05-26 11:12:26
User: keymon
Functions: kill sleep wait
3

I like much more the perl solution, but without using perl. It launches a backgroup process that will kill the command if it lasts too much.

A bigger function:

check_with_timeout() {

[ "$DEBUG" ] && set -x

COMMAND=$1

TIMEOUT=$2

RET=0

# Launch command in backgroup

[ ! "$DEBUG" ] && exec 6>&2 # Link file descriptor #6 with stderr.

[ ! "$DEBUG" ] && exec 2> /dev/null # Send stderr to null (avoid the Terminated messages)

$COMMAND 2>&1 >/dev/null &

COMMAND_PID=$!

[ "$DEBUG" ] && echo "Background command pid $COMMAND_PID, parent pid $$"

# Timer that will kill the command if timesout

sleep $TIMEOUT && ps -p $COMMAND_PID -o pid,ppid |grep $$ | awk '{print $1}' | xargs kill &

KILLER_PID=$!

[ "$DEBUG" ] && echo "Killer command pid $KILLER_PID, parent pid $$"

wait $COMMAND_PID

RET=$?

# Kill the killer timer

[ "$DEBUG" ] && ps -e -o pid,ppid |grep $KILLER_PID | awk '{print $1}' | xargs echo "Killing processes: "

ps -e -o pid,ppid |grep -v PID | grep $KILLER_PID | awk '{print $1}' | xargs kill

wait

sleep 1

[ ! "$DEBUG" ] && exec 2>&6 6>&- # Restore stderr and close file descriptor #6.

return $RET

}

timeout -k 1m 30s some_command
2012-03-27 18:06:18
User: tlemerond
Tags: fg bg kill timeout
1

A timeout is great, but what if the command is taking longer than expected because it's hung up or ran into some other problem? That's where the -k option comes in. Run "some_command" and timeout after 30s. If the command is still running after 1 minute, it will receive a kill signal.

Know a better way?

If you can do better, submit your command here.

What others think

I can not find the timeout command on my centos5. Does it depend on some special Distribution?

Comment by hywl51 259 weeks ago

it is in the ubuntu repo,

apt-get install timeout

according to the man page it is from here:

http://www.porcupine.org/forensics/tct.html

but looked around a bit and it didn't jump out at me.

Comment by syssyphus 259 weeks ago

this bug report indicates that the timeout command is no longer part of tct, so.... you may have to dig up an old version of tct in order to install it.

https://bugs.launchpad.net/ubuntu/+source/tct/+bug/78742

Comment by syssyphus 259 weeks ago

perhaps this would be a better alternative, I have not tested it however.

http://www.cpan.org/authors/id/D/DE/DEXTER/timeout-0.11.pl

Comment by syssyphus 259 weeks ago

There is now a timeout command as part of GNU coreutils

One could also try this script http://www.pixelbeat.org/scripts/timeout

Comment by pixelbeat 230 weeks and 3 days ago

I think sleep is a better command for timeout option.

sleep 2s && echo "hi"

for 2 sec , for hours replace s by h and for min replace s by m :)

Comment by pahnin 212 weeks and 5 days ago

@syssyphus sleep must be pre-installed :) check it

Comment by pahnin 212 weeks and 5 days ago

Your point of view

You must be signed in to comment.

Related sites and podcasts