function finish { # re-start service sudo /etc/init.d/something start } trap finish EXIT sudo /etc/init.d/something stop # Do the work... # Allow the script to end and the trapped finish function to start the # daemon back up.