I trying to understand the manpage of the dd
program, which mentions:
Sending a USR1 signal to a running 'dd' process makes it print I/O statistics to standard error and then resume copying.
$ dd if=/dev/zero of=/dev/null& pid=$! $ kill -USR1 $pid; sleep 1; kill $pid
What does pid=$!
mean?
Is this an assignment of a variable, which gets the pid of dd
?
And is eventually used in the $pid
variable?
Also why do they use sleep
and kill
?
Is this the way to use -USR1
?
Best Answer
dd if=/dev/zero of=/dev/null&
The trailing
&
means run the prefix command in background. (Disclaimer: This is oversimplified statement)Refer to this:
So
pid=$!
assign the most Recent background PID to variable pid, which isdd
PID.You need
kill $pid
(if not specified parameter, default signal for kill is TERM which is process termination) to terminate thedd
process after you done testing, otherwisedd
process may just stay in background and exhausting your CPU resources. Check your System Monitor of your platform to see.Whereas
Kill -USR1 $pid
print I/O statistics, it doesn't terminate the process.Without sleep 1 second, your
dd
process may get terminated by last command statementkill $pid
** before have the chance to write statistics output to your terminal. The processes is synchronous but trap+write operation (kill -USR1 $pid
) may slower than terminate operation (kill $pid
). Sosleep 1
second to delay the startup ofkill $pid
to ensure statistics output done printing.Just
man dd
:And
man 7 signal
:Combine both statements , you should understand USR1 is User-defined signal which is defined by
dd
to provide a way for user to interrupt it and print I/O statistics on the fly. It's program specific handler, it doesn't means you cankill -USR1 other_program_pid
and expect statistics output.Also you might interest about this: Why does SIGUSR1 cause process to be terminated?.