Home Check if timeout command was successful
Reply: 1

Check if timeout command was successful

Hamid Reza
Hamid Reza Published in 2018-02-13 17:07:37Z

I am trying to run a command in a bash file and put the output of command in a variable. But the command must NOT take longer than 2 seconds. I use the command:

timeout -k 2 2 ls /var/log/;

And there is no problem. The command either list the contents of log directory or kills the command in case it took more than two seconds. But when I try to put the output in a variable the commands hangs and doesn't reply or get killed! I use like this:

result=$(timeout -k 2 2 ls /var/log/);

Where is my mistake?

Javier Elices
Javier Elices Reply to 2018-02-13 17:24:21Z

The timeout command will exit with status 124 if it had to kill the process; see here. So you may try something like:

timeout -k 2 2 ls /var/log/ >directory.txt
if [ $? -eq 124 ]
  echo "Timeout exceeded!"
  cat directory.txt
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.5422 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO