How to set a delay between service exiting and service restarting?

We have a python application that exits with a runtime error, then it is immediately restarted automatically. Is there a way to put a delay between the application exit and when the supervisor restarts the service?
See logs for timestamps:

[Logs]    [2023-08-30T05:59:21.447Z] Service exited 'main sha256:d32dcf1d7d5b77dfe39c2d67298689918443b7c077dcc43508e3892199e0d0c7'
[Logs]    [2023-08-30T05:59:22.170Z] Restarting service 'main sha256:d32dcf1d7d5b77dfe39c2d67298689918443b7c077dcc43508e3892199e0d0c7'

Hi there,

Unfortunately I don’t believe that there is a way to do this right now. Could you describe the issue that automatically attempting to restart the container causes for you, maybe there’s other workarounds that might help you


I’ve tried something similar a while back.
My solution was to simply write a wrapper script/entrypoint.
This wrapper

  • Does preparations needed for my application, like fixing certificates;
  • Calls the actual application;
  • Does cleanup after my application exits

I use an environment variable to tell the wrapper what to do when my application returns.
This variable translates to:

  • do nothing (sleep forever)
  • wait X seconds before exiting the container (causing supervisor to restart it)
  • request a reboot from supervisor
1 Like

If you are using Systemd as your supervisor on a Linux system, you can modify the service unit configuration to introduce a delay before restarting the service. You can do this by editing the service unit file (typically found in /etc/systemd/system/) and adding a delay using the RestartSec option. For example:

RestartSec=10 # Add a 10-second delay before restarting