-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.sh
executable file
·68 lines (48 loc) · 1.26 KB
/
demo.sh
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env bash
checkpoint_requested=false
function my_signal_handler()
{
case ${sig} in
SIGINT)
printf "...caught %s at %s\n" ${sig} "$(date)"
checkpoint_requested=true
;;
SIGTERM)
printf "...caught %s at %s\n" ${sig} "$(date)"
checkpoint_requested=true
;;
SIGUSR1)
printf "...caught %s1, ignoring...\n" ${sig}
;;
*)
printf "...caught unknown signal: %s\n" ${sig}
exit 1
;;
esac
printf " --> Restoring default handler for signal %s\n" ${sig}
trap - ${sig}
}
function register_signal_handler ()
{
printf "Registering user-specified signal handlers for PID %d\n" $$
trap "sig=SIGINT; my_signal_handler" SIGINT
trap "sig=SIGTERM; my_signal_handler" SIGTERM
trap "sig=USR1; my_signal_handler" USR1
}
function do_checkpoint ()
{
for i in $(seq 1 10); do
printf "\t%2d : Inside checkpoint function\n" $i
sleep 5s
done
checkpoint_requested=false;
}
# main function follows
register_signal_handler
for i in $(seq 1 50); do
printf "%2d : Inside main function\n" $i
sleep 5s
if $checkpoint_requested; then
do_checkpoint
fi
done