0

Currently whenever I press CTRL + Z on a lengthy script I was given, it immediately terminates the script ([1+] stopped(SIGTSTP) ./test.py) which is what I want, but it also leaves the python2 process running (when I type ps to look at processes), which forces me to use killall -9 python2, which I do not want to do every time. Is there a way to immediately terminate a script that doesn't leave the python2 process running in the background?

There is no SIGTSTP currently in the code that I see but I did try using the following code with no luck. It didn't even exit the script when I pressed CTRL + Z.

def handler(signum, frame):
    sys.exit("CTRL+Z pressed. Exiting Test")

signal.signal(signal.SIGTSTP, handler)
Alon Yeager
  • 820
  • 2
  • 8
  • 17
blindside044
  • 446
  • 1
  • 7
  • 20

2 Answers2

1

SIGSTP is a signal to suspend a process, it sounds like you want to terminate a process. You can try sending Ctrl-C or CTRL-D instead, which should send a SIGINT signal.

I believe you could also try CTRL-\ which sends SIGQUIT.

sgillen
  • 596
  • 2
  • 7
  • CTRL + D did nothing during the script. CTRL + C displayed ^C on my screen but the script continued running. CTRL + \ seems to have done the trick though. Thank you. – blindside044 Jul 31 '19 at 23:23
0

Use Ctrl + C. SIGTSTP suspends the process, hence why it keeps it open, but does not terminate it. (Note: On a Linux terminal use Ctrl + \, otherwise use Ctrl + C or Ctrl + D)

Or just use sys.exit()

Ben Rauzi
  • 564
  • 4
  • 13
  • Also check out this post: https://stackoverflow.com/questions/19782075/how-to-stop-terminate-a-python-script-from-running – Ben Rauzi Jul 31 '19 at 23:25