You need a wrapper.
The most obvious one is to rename the original script and in its place to create a wrapper script that would call the original with a timeout. A variation on this is to create a link with a different name to the script itself, and the way the script is called would decide what happens in it. It should be clear in a moment.
If your script is called without parameters, you can add this inner wrapper to your script. It will invoke itself again with
timeout and with a switch in the
if [ "$1" != FALSE ]; then
timeout "$TIMEOUT" "$0" "$WRAPPER"
#the proper script
echo Here\'s the original script starting.
echo End of the original script.
So going back to the previous solution, you might use
$0 to see how the script was called. Was it the file or the link to it?
If you don't want or can't create a new executable/link, and the original call uses parameters, other switches are available. For example, a temp file might be used. This will involve some corner cases that demand a cleanup of that file in case something goes wrong. You might also use PPID as a switch. If the parent's command is the same as child's, then it executes the script proper, otherwise it executes itself with a timeout.