It sounds like timeout
is what you need then, assuming it's ok for the script to call on external tools (as I mentioned, timeout is part of the gnu coreutils, so all linux systems should have it).
If timeout has to terminate the process it's watching, it exits with a status of 124. If not, it exits with the status of the command.
So simply test the output of timeout, and use it to exit the script with the value you want.
timeout 4s program
case $? in
124) exit 255 ;;
*) exit $? ;;
timeout also includes options to send stronger kill signals if the standard termination call fails.