5

Thank you guys for seeing my post.

First, the following is my code:

import os

print("You can create your own message for alarm.")
user_message = input(">> ")

print("\n<< Sample alarm sound >>")

for time in range(0, 3):
    os.system('say ' + user_message) # this code makes sound.

print("\nOkay, The alarm has been set.")

"""
##### My problem is here #####
##### THIS IS NOT STOPPED #####

while True:
    try:
        os.system('say ' + user_message)
    except KeyboardInterrupt:
        print("Alarm stopped")
        exit(0)
"""

My problem is that Ctrl + C does not work!

I tried changing position of try block, and making signal(SIGINT) catching function.

But those also does not work.

I have seen https://stackoverflow.com/a/8335212/5247212, https://stackoverflow.com/a/32923070/5247212, and other several answers about this problem.

I am using MAC OS(10.12.3) and python 3.5.2.

2
  • Have you seen this question? Mar 9, 2017 at 11:06
  • 1
    Seemed to work fine for me on windows 7 and Ubuntu 14.04 (though obviously had to replace the MACOS specific "say" command with a print. The point is that ctrl-C was correctly trapped, it printed alarm stopped and exited. Mar 9, 2017 at 11:11

4 Answers 4

5

This is expected behaviour, as os.system() is a thin wrapper around the C function system(). As noted in the man page, the parent process ignores SIGINT during the execution of the command. In order to exit the loop, you have to manually check the exit code of the child process (this is also mentioned in the man page):

import os
import signal

while True:
    code = os.system('sleep 1000')
    if code == signal.SIGINT:
        print('Awakened')
        break

However, the preferred (and more pythonic) way to achieve the same result is to use the subprocess module:

import subprocess

while True:
    try:
        subprocess.run(('sleep', '1000'))
    except KeyboardInterrupt:
        print('Awakened')
        break

Your code would then look like something like this:

import subprocess

print("You can create your own message for alarm.")
user_message = input(">> ")

print("\n<< Sample alarm sound >>")

for time in range(0, 3):
    subprocess.run(['say', user_message]) # this code makes sound.

print("\nOkay, The alarm has been set.")

while True:
    try:
        subprocess.run(['say', user_message])
    except KeyBoardInterrupt:
        print("Alarm terminated")
        exit(0)

As an added note, subprocess.run() is only available in Python 3.5+. You can use subprocess.call() to achieve the same effect in older versions of Python.

0

Also catch "SystemExit"

except (KeyboardInterrupt, SystemExit):
    print("Alarm stopped")
0
0

The problem seems to be that Ctrl+C is captured by the subprocess you call via os.system. This subprocess reacts correspondingly, probably by terminating whatever it is doing. If so, the return value of os.system() will be not zero. You can use that to break the while loop.

Here's an example that works with me (substituting say by sleep):

import os
import sys

while True:
    try:
        if os.system('sleep 1 '):
            raise KeyboardInterrupt
    except KeyboardInterrupt:
        print("Alarm stopped")
        sys.exit(0)
0

If Ctrl-C is captured by the subprocess, which is the case here, the simplest solution is to check the return value of os.system(). For example in my case it returns value of 2 if Ctrl-C stops it, which is a SIGINT code.

import os

while True:
    r = os.system(my_job)
    if r == 2:
        print('Stopped')
        break
    elif r != 0:
        print('Some other error', r)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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