I came across this command using SIGUSR1 to get current progress of dd:
dd if=/dev/urandom of=file.img bs=4KB& pid=$! while [[ -d /proc/$pid ]]; do kill -USR1 $pid && sleep 1 && clear; done
Before this, I didnt know anything useful from sending SIGUSR1 and SIGUSR2 to a process. In dd, SIGUSR1 is for showing current progress, this is useful for long process. These two signals arent given any guidelines of how to use them, its completely up to the developers; unlike BSD-like system, they have a specific signal named SIGINFO for informational message as the signal name already suggests, and you can simply press Ctrl+T to send the signal.
I wanted to see what other programs utilize these two signals, so I ran man -K SIGUSR to see what manpages mention about them. Some also use SIGUSR1 for progress reporting, feh uses two signals to move forward or backward one image, or some use them for configuration reloading. For easy tasks, these two signals provide the simplest one-way channel to receive the commands from users or controlling programs.
You can implement it in Bash just as simply as shown in the following code:
#!/bin/bash trap sigusr1 SIGUSR1 sigusr1() { echo $i >&2 } i=0 while let ++i; do sleep 1; done
To handle a signal, simply trap it with a function handler or any command, e.g. exit. Sending message to standard error may be a good practice lest standard output is piped into another program. The informational message can break the piping programs processing.
To monitor regularly, you can write a loop like the first code in the beginning of this post, or you can use watch.
0 comments:
Post a Comment