I'm trying to catch the SIGUSR1
signal in a bash script that is sleeping via the sleep
command:
#!/bin/bash trap 'echo "Caught SIGUSR1"' SIGUSR1 echo "Sleeping. Pid=$$" while : do sleep 10 echo "Sleep over" done
The signal trap works, but the message being echoed is not displayed until the sleep 10
has finished.
It appears the bash signal handling waits until the current command finished before processing the signal.
Is there a way to have it interrupt the running sleep
command as soon as it gets the signal, the same way a C program would interrupt the libc sleep()
function?
How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.
/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.
In other words, the sleep command pauses the execution of the next command for a given number of seconds. The sleep command is useful when used within a bash shell script, for example, when retrying a failed operation or inside a loop.
#!/bin/bash trap 'echo "Caught SIGUSR1"' SIGUSR1 echo "Sleeping. Pid=$$" while : do sleep 10 & wait $! echo "Sleep over" done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With