1

aim working on some shell script , i would like to check the following command

df -h

if timeout more than 60 second then echo "error " else pass.

so if this command will not responding for 1 minute then I will receive and error message , otherwise it will pass , any creative idea will be high apricated .

1
  • There is a timeout command that might help. Commented Jan 25, 2021 at 12:54

1 Answer 1

3
timeout 60 df -h > /dev/null && echo "PASS" || echo "PROBLEM"

Using timeout, if the command df -h doesn't execute within 60 seconds or errors, it will return a none 0 return code. If a none 0 return code is returned echo "PROBLEM" using the || condition.

Sign up to request clarification or add additional context in comments.

5 Comments

But can i just write an message instead of the whole command : like if df -h work then "pass" otherwise "problem", is that possible ?
I have amended the answer
it still show me all the df -h and at end pass :(
You can add a redirect to /dev/null. I've amended the answer
a && b || c is an antipattern when used in place of if a; then b; else c; fi. Behavior is not identical. See BashPitfalls #22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.