3

My script cannot be terminated with Ctrl+C key. It is an error.

Please explain how to modify my script to make it terminable by Ctrl+C.

#!/bin/bash

while true; do
  paplay /usr/share/sounds/phone.wav
done
4

If you catch SIGINT in the parent shell and exit the shell, it brings down paplay too:

trap exit SIGINT;
while true; do paplay  /usr/share/sounds/phone.wav ; done

It looks like paplay blocks the INT signal and that affects that prevents the parent shell from running its default handler.

Installing a handler in the parent shell in which you reset the handler and reraise the signal probably solves it in a better way:

trap 'trap - SIGINT; kill -SIGINT $$' SIGINT; 
while :; do paplay  /usr/share/sounds/linuxmint-login.wav ; done 

(With the exit approach the launcher process thinks its child exited voluntarily whereas with the second approach it looks like the script was killed with SIGINT which it was).

0

The question is actually about paplay, which may be blocking signals, or (as hinted in the Arch Wiki), may be simply too busy.

You could make the script easier to stop by putting a sleep 1 in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.