DarkKnight
DarkKnight

Reputation: 657

Handling of error : program run from shell doesnt return

I have a shell script which updates different firmwares with different executables. I need to know if one of the executable has hung and not returning back to shell. Can I introduce any kind of timeout ?

Sample shell script below. How to handle if updatefw command hangs and does not return.

#!/bin/sh

updatefw -c config.cfg
    if [ $? != 0 ]; then
        echo "exec1 failed"
        exit 1   
    fi  
exit 0

Upvotes: 0

Views: 32

Answers (1)

Cyrus
Cyrus

Reputation: 88583

I suggest with timeout from GNU core utilities:

#!/bin/bash

timeout 30 updatefw -c config.cfg
if [[ $? == 124 ]]; then
  echo "update failed"
  exit 1
fi

When timeout quits updatefw, the return code is 124.

I assume here that the update will never take longer than 30 seconds.

Upvotes: 3

Related Questions