Skip to content

Adding graceful shutdown on SIGTERM #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ You can then see the notification in action by (re-)starting the service: `syste
You can do a lot more via [sd_notify], see its documentation for details.


### Gracefully shutting down on systemd stop

When systemd stops your service ( using `systemctl stop python_demo_service` ) it sends the signal SIGTERM. You can do the same with `kill -SIGTERM <pid>` for testing.

If you do not register a handler for SIGTERM your code will be terminated immediately. If you register a handler you can do some last tasks before exiting (e.g. writing and closing a file and showing an exit message).

```python
import time
import systemd.daemon
import signal
import sys

def graceful_exit(signal_number,stack_frame):
systemd.daemon.notify('STOPPING=1')
print("Python Demo Service received signal {}. Stopping now.".format(signal.Signals(signal_number).name))
time.sleep(5)
print("Python Demo Service has shutdown. Bye bye now!")
sys.exit(0)

# register SIGTERM and SIGINT handlers to enable graceful shutdown of service
signal.signal(signal.SIGINT, graceful_exit)
signal.signal(signal.SIGTERM, graceful_exit)
```

## Creating a System Service

Once you have a working user service you can turn it into a system service. Remember, however, that system services run in the system's central systemd instance and have a greater potential for disturbing your system's stability or security when not implemented correctly. In many cases, this step isn't really necessary and a user service will do just fine.
Expand Down
15 changes: 15 additions & 0 deletions python_demo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,23 @@
if __name__ == '__main__':
import time
import systemd.daemon
import signal
import sys

# graceful shutdown routine - gets called when process gets SIGTERM (systemd stop) or SIGINT (keyboard ctrl+c)
def graceful_exit(signal_number,stack_frame):
systemd.daemon.notify('STOPPING=1')
print("Python Demo Service received signal {}. Stopping now.".format(signal.Signals(signal_number).name))
time.sleep(5)
print("Python Demo Service has shutdown. Bye bye now!")
sys.exit(0)

print('Starting up ...')

# register SIGTERM and SIGINT handlers to enable graceful shutdown of service
signal.signal(signal.SIGINT, graceful_exit)
signal.signal(signal.SIGTERM, graceful_exit)

time.sleep(10)
print('Startup complete')
# Tell systemd that our service is ready
Expand Down