I started learning bash 3-4 days ago and I have a task to do that I'm having hard time completing. I need to make a script, running a loop which after being signaled SIGUSR1, it should print the process id and exit. I'd be very thankful if i receive some help.
You'd do it this way:
#!/usr/bin/env bash
# Our USR1 signal handler
usr1_trap(){
printf 'Here is the PID: %d\nExiting right-now!\n' $$
exit
}
# Register USR1 signal handler
trap usr1_trap USR1
printf 'Run this to stop me:\nkill -USR1 %d\n' $$
# Wait in background, not consuming CPU
while :; do
sleep 9223372036854775807 & # int max (2^63 - 1)
wait $!
done