0

I have a Python script which prints \a (ASCII Bell character) when some event occurs. The problem that I'm facing currently is that the script closes after I close the terminal which was executing the script. On going through similar questions posted on the site, most answers suggest using the nohup command. The problem with using nohup is that the output is redirected to a nohup.out file. Because of this, the ASCII Bell sound will not ring as the output is now redirected to somewhere else.

I have tried the following:
Using &. This stops the bell as soon as I close the terminal:

python3 script.py &

Using nohup. Redirects the output and can't hear the bell at all:

nohup python3 script.py &

Using disown also didn't work. The script continued till I closed the terminal.

python3 script.py &
disown %1

Is there anyway to overcome this and run the script in background after closing the terminal and still hear the bell sound?

Léa Gris
  • 17,497
  • 4
  • 32
  • 41
mtm
  • 504
  • 1
  • 7
  • 25
  • Can you provide the code so that we can try in our local – abak1802 Dec 29 '21 at 11:04
  • 2
    You cannot background something which needs to continue to print to your terminal. The output is not producing any sound until it is processed by the terminal driver. – tripleee Dec 29 '21 at 11:11
  • @abak1802 I have provided all the commands I have tried out in the question. If you are looking for the python code, you can print '\a' and then do a sleep for 1 second. – mtm Dec 29 '21 at 11:13
  • @tripleee Any other way other than printing bell to make a notification sound while running script in the background? – mtm Dec 29 '21 at 11:15
  • 1
    Depends on your hardware, but if it is capable of producing sound, then absolutely certainly. – tripleee Dec 29 '21 at 11:17
  • On MacOS, try `subprocess.run(['say', 'beep'])` – tripleee Dec 29 '21 at 11:19
  • I will use `spd-say` for the time being and see if I can install `sox` and use `play` as instructed in https://stackoverflow.com/a/16573339/10153731 – mtm Dec 29 '21 at 11:31

0 Answers0