0

I have a python process that executes this code :

import time, sys
import signal

def signal_handler(signal, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGTERM, signal_handler)

for i in range(0, 1000):
    print(i)
    sys.stdout.flush()
    time.sleep(1)

It's a simple code that prints numbers and when it receives a sigterm signal, it prints and stop.

I package it to an .exe and when I launch it and do ctrl+C it prints and stop, so it works.

The problem occurs when I try to stop the process with Electron. I launch the .exe with this line :

this.pythonProcess = spawn('mydirectory/testsignal.exe', [], { shell: true }); //Also tried with shell false and same results

Now, I want to kill it and that my program prints "You pressed Ctrl+C!" before stopping.

After I launch it, I tried multiple commands to stop it. Here is what I tried :

this.pythonProcess.kill('SIGTERM'); //Do nothing
process.kill(this.pythonProcess.pid, 'SIGTERM'); //Do nothing
treeKill(this.pythonProcess.pid, 'SIGTERM', (err) => {
    if (err) {
      console.error('Error terminating the child process:', err);
    } else {
      console.log('Child process terminated successfully.');
    }
}); //kills it but prints nothing

I also tried with SIGINT but it doesn't works.

When I print all signals received by my python program, on all of the commands I tried above, it receives nothing.

MiThyX
  • 31
  • 6

0 Answers0