3

I am on Ubuntu 16.04.5 LTS (AWS)

I am creating a python process via this command: nohup python -u main.py > nohup.out 2>&1 &

I would like to send a ctrl-c/SIGINT to the process, so I send kill -2 <pid>.

When I start the process from my terminal, this works fine, the program receives the keyboard interrupt and closes gracefully.

When I start the process via a .sh script or via another bash process (e.g. bash -c 'nohup python -u main.py > nohup.out 2>&1 &'). (I believe both methods start the process in the same way), the process does not receive the SIGINT when I send it.

SIGTERM (default kill) works normally and closes the process, but does not let the process close gracefully, but I need.

What's happening?

Eric
  • 131

1 Answers1

3

No, they're not started in the same way. Since bash is running scripts with the job control turned off, the background mode of the commands started with & from a script is just "faked" by letting them ignore SIGINT and SIGQUIT and redirecting their input from /dev/null.

See my answer here (with references to the standard describing this).

In your case this also means the main.py isn't setting any handler for SIGINT in order to do a "graceful" exit, but it's just forcefully killed by a Control-C or kill -2 when run from an interactive shell. If it did set a handler, that handler would've also overridden the SIG_IGN disposition of SIGINT set by the parent shell when run from a script.