I have a script running on fedora 22 where I am restarting service network as part of a troubleshoot calling
service network restart. I want to check if that command is taking too long to execute. If it takes too long I want to output a message saying “Error restarting network service”. Else I want to continue with the script.
You can use
timeout command to run your command or script in a given timeout.
Something similar to this:
timeout 10m command
Which waits for the command to finish withing 10 minutes otherwise kills it and exits with status 124. Then you can check exit status of
timeout and print the appropriate message based on it.
See here for more: timeout manpage.
If you don’t want to kill the long command do something like this:
(( sleep $TIMEOUT; echo "command took so long!" ) & exec $COMMAND )
On TIMEOUT this will print the message but command continues to execute.