Using the freestyle projects to execute bash shell scripts work fine. But cancelling a jenkins job seems to use SIGKILL. In this way the script cannot perform cleanup operations and free resources.
SIGKILL cannot be handled by shell
SIGINT/SIGTERM are not used by jenkins
Preferred: SIGINT -> wait 5 seconds -> SIGKILL