Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

75%
+4 −0
Q&A How can I use SIGUSR1 or SIGUSR2 without risk of terminating the process?

SIGUSR1 and SIGUSR2 are user-defined signals. Imagine there is a tool designed to do something useful upon receiving one or the other. The problem is the default action for these signals is Term (...

1 answer  ·  posted 12mo ago by Kamil Maciorowski‭  ·  last activity 12mo ago by Kamil Maciorowski‭

#1: Initial revision by user avatar Kamil Maciorowski‭ · 2023-06-30T06:42:52Z (12 months ago)
How can I use SIGUSR1 or SIGUSR2 without risk of terminating the process?
SIGUSR1 and SIGUSR2 are user-defined signals. Imagine there is a tool designed to do something useful upon receiving one or the other.

The problem is the default action for these signals is `Term` (see `man 7 signal`), so it's safe to send SIGUSR1 or SIGUSR2 to the tool only after it started handling (or ignoring, or blocking) the signal. Send it too soon and the process will be terminated.

Personally I'm surprised by this default behavior. When I want to use SIGUSR1 or SIGUSR2, it's because there is (or I hope there is) a handler that does something useful. In no case I want the signal to terminate the process. If I wanted to terminate the process, I would simply generate SIGTERM.

Our example tool can be GNU `dd`. If it manages to set things up then it will react to SIGUSR1 by printing I/O statistics to its stderr. But try this:

    dd if=/dev/zero of=/dev/null & kill -s USR1 "$!"; sleep 1; kill -s USR1 "$!"; sleep 1; kill "$!"

Most likely you will see `dd` terminated by SIGUSR1 generated by the first `kill`. The second and the third `kill` will tell you there is `no such process`.

If you `sleep 1` before the first `kill` then you most likely give `dd` enough time to set things up and each `kill` will work as intended:

    dd if=/dev/zero of=/dev/null & sleep 1; kill -s USR1 "$!"; sleep 1; kill -s USR1 "$!"; sleep 1; kill "$!"

But this is not a firm method, as there is no guarantee `sleep 1` is enough. In theory no interval is long enough.

How can I use SIGUSR1 or SIGUSR2 without any risk of terminating the process?