Projekt

Allgemein

Profil

Fehler #614 » trap_test.sh

Maximilian Seesslen, 25.02.2025 18:19

 
1
#!/bin/bash
2

    
3
ctrlc_count=0
4

    
5

    
6
set -e -u
7

    
8
function no_ctrlc()
9
{
10
    let ctrlc_count++
11
    echo
12
    if [[ $ctrlc_count == 1 ]]; then
13
        echo "Stop that."
14
    elif [[ $ctrlc_count == 2 ]]; then
15
        echo "Once more and I quit."
16
    else
17
        echo "That's it.  I quit."
18
        exit
19
    fi
20
}
21

    
22
function helloWorld()
23
{
24
    echo "Hello Signal"
25
    ls
26
    ls
27
    echo $PWD
28
    # exit 0
29
}
30

    
31
function errorHandler()
32
{
33
    echo "error"
34
    exit 0
35
}
36

    
37

    
38
# trap no_ctrlc SIGINT
39
# trap helloWorld SIGUSR1
40
# trap helloWorld SIGUSR2
41
# trap errorHandler ERR
42

    
43

    
44
# Our USR1 signal handler
45
function usr1_trap(){
46
    printf 'Here is the PID: %d\nExiting right-now!\n' $$
47
    # exit
48
}
49

    
50
# Register USR1 signal handler
51
trap usr1_trap USR1
52

    
53

    
54
echo "PID: $BASHPID, $$"
55
printf 'Run this to stop me:\nkill -USR1 %d\n' $$
56

    
57

    
58
echo Sleeping
59

    
60
while true
61
do
62
    sleep .5
63
done
64

    
65

    
66

    
67
##printf 'Run this to stop me:\nkill -USR1 %d\n' $$
68
##
69
## Wait in background, not consuming CPU
70
#while :; do
71
#   sleep 9223372036854775807 & # int max (2^63 - 1)
72
#   wait $!
73
#done
74

    
75

    
76
echo End
77

    
78

    
79
#---fin--------------------------------------
(2-2/5)