3

I'm working on a project to produce a shell in Python, and one important feature is the ability to pause and background a running subprocess. However the only methods I've found of pausing the subprocess appear to kill it instantly, so I can't resume it later. Our group has tried excepting KeyboardInterrupt:

try:
    process = subprocess.Popen(processName)
    process.communicate()
except KeyboardInterrupt:
    print "control character pressed"

and also using signals:

def signal_handler(signal,frame):
    print 'control character pressed'

signal.signal(signal.SIGINT, signal_handler)
process.communicate()

Another issue is that both of these only work when Ctrl-C is pressed, nothing else has any effect (I imagine this is why the subprocesses are being killed).

1
  • what do you mean by "pause". Is it "suspend" as with SIGSTOP/SIGCONT signals (see Jobs and sessions)? Commented Nov 11, 2013 at 20:16

1 Answer 1

1

The reason you have the process dying is because you are allowing the Ctrl+C to reach the subprocess. If you were to use the parameter preexec_fn = os.setpgrp, as part of the Popen call, then the the child is set to be in a different process group from the parent.

Ctrl+C sends a SIGINT to the complete process group, but since the child is in a different process group, it doesn't receive the SIGINT and thus doesn't die.

After that, the send_signal() function can be used to send a SIGSTOP to the child process whenever you want to pause it, and a SIGCONT to resume it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.