Skip to content

Instantly share code, notes, and snippets.

@pagxir
Last active May 3, 2017 16:22
Show Gist options
  • Save pagxir/5f9ab9179a1a5c0fd8b00badd5cff0bf to your computer and use it in GitHub Desktop.
Save pagxir/5f9ab9179a1a5c0fd8b00badd5cff0bf to your computer and use it in GitHub Desktop.
execute a shell command with timeout
timeout() {
exec 4<&1;
(sh -c 'echo $PPID'; exec $@ 3<&1 1<&4 4>&-) | (cat; echo END;) | (read PID; read -t 3 || kill $PID)
exec 4>&-;
}
timeout wget http://www.baidu.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment