-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrap_example4.sh
More file actions
29 lines (25 loc) · 808 Bytes
/
trap_example4.sh
File metadata and controls
29 lines (25 loc) · 808 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# Fred Denis -- August 21st 2020 -- fred.denis3@gmail.com
# Trap command example
#
on_exit() {
printf "\033[1;36m%s\033[m\n" "Delete tempfile"
rm -f "${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Check if the tempfile still exists"
ls -ltr "${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Sending an email to let people know the script is finished"
}
on_term() {
printf "\033[1;31m%s\033[m\n" "I have been killed !"
exit 123
}
trap on_term TERM
trap on_exit EXIT
TEMPFILE=$(mktemp)
printf "\033[1;36m%s\033[m\n" "Tempfile is ${TEMPFILE}"
printf "\033[1;36m%s\033[m\n" "Sending an email to let people know the script is starting"
printf "\033[1;36m%s\033[m\n" "I am PID $$"
printf "\033[1;36m%s\033[m\n" "A first sleep"
sleep 20
printf "\033[1;36m%s\033[m\n" "A second sleep"
sleep 10