3

I've written a shell script that uses the trap feature to output its progress when it receives a SIGUSR1 signal, but I don't see how to have the script be able to output the progress to the STDOUT of the process which sent the signal. So, if the script is running in terminal emulator /dev/pts/10 and I send the process a signal from a terminal emulator on /dev/pts/11, the output is going to /dev/pts/10, but I want to send to /dev/pts/11. I do NOT want to have to resort sending the progress message via wall or hard-coded shell redirection to a specific STDOUT.

1 Answer 1

0

What you want is impossible. A process cannot know for sure which process sent it a signal: if multiple processes send the same process the same signal in a short enough time window, the recipient will only see a single signal.

Even if you knew which process sent the signal, you might not have permission to write to the sender's stdout.

A reasonable way to do what you want is to have the process listen on a Unix socket. When there is a connection on this socket, write the progress information on that socket. Let the other end take care of printing it where it wants.

To query the status, you can use the socket utility found in some distributions, or the more powerful but more complicated socat.

socket -r ./my-program-status              # If the socket is a filesystem domain socket in the current directory
socat UNIX-CLIENT:my-program-status -      # If the socket is a filesystem domain socket in the current directory
socat ABSTRACT-CLIENT:my-program-status -  # If the socket is an abstract domain socket in the current directory

Server-side proof-of-concept:

while date | socket -q -s ./status; do echo .; done

Another approach would be to have your program periodically write its status to a file.

7
  • 3
    See for instance the linux man -s2 sigaction, which includes a description of the siginfo_t datatype structure, which contains field pid_t si_pid; /* Sending process ID */. Also there: "Signals sent with kill(2) and sigqueue(3) fill in si_pid and si_uid". The shell itself internally certainly has access to this, so the most desirable answer would describe how to reveal it to a script. Commented Jun 24, 2020 at 19:15
  • @user1404316 Ah, right, the information is available on modern systems (but unreliable due to possible conflation). You'd have to patch the shell to get it though, even if the unreliability was acceptable. Commented Jun 24, 2020 at 20:33
  • Should I edit your answer accordingly, or do you prefer to do it? Commented Jun 24, 2020 at 20:42
  • @user1404316 I will not recommend patching the shell. If that's your solution, it's your answer, not mine. Commented Jun 24, 2020 at 20:47
  • 1
    You un-did my edit, so let's be clear for the benefit of future readers: The first paragraph of your answer is false, and the second paragraph is irrelevant factoid. Commented Jun 24, 2020 at 21:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.