I am trying to see if there is a way I can suspend and then resume a process in a python script. I get the process pid using os.getpid() and then i suspend the process using suspend(). Is there a way to resume the process without having to manually type "fg" in a shell?

Here is my code:

#!/usr/bin/env python
import time
import psutil
import os

sm_pid = os.getpid()
p = psutil.Process(sm_pid)

print "Going to suspend"


print "process resumed"
  • 1
    What are you trying to accomplish with a suspension that you can't accomplish with a simple sleep? – BowlOfRed Nov 27 '18 at 0:58
  • The sleep is just a placeholder here. I'm trying to "pause" a process and then resume it after receiving a user input through a GUI – hama_ROW Nov 28 '18 at 16:42
  • Suspend probably isn't the right way (because the process can't wake itself up). Normally GUIs have a main loop that will accept input signals and execute callbacks, pausing when none are available (often via select()). I would suspect the specifics of the GUI might matter here. – BowlOfRed Nov 28 '18 at 16:51
  • My GUI is executing callbacks from a main loop but it is running on a separate thread. I'm trying to pause the process on the main thread from the GUI thread and then resume it by sending a command again from the GUI thread. – hama_ROW Nov 28 '18 at 17:16
  • Are these threads separate processes as well? Suspension is per-process, not per-thread. You can't suspend only part of a process. It's possible your question would be better handled by python folks in stackoverflow rather than as a generic unix question. – BowlOfRed Nov 28 '18 at 17:46

You could start an asynchronous process that sleeps for five seconds (or whatever) and then sends a SIGCONT signal to the main process.

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.