How do I make my custom app automatically restart on crash?

Quick Linux Tip #38:

Try: sudo systemctl edit --force --full myapp.service

Info: Restart=on-failure restarts the app after a non-zero exit or crash, while RestartSec adds a delay before restarting. StartLimitBurst and StartLimitIntervalSec help prevent a service from repeatedly restarting in a crash loop.

Examples:

  • $ sudo systemctl status myapp  # View service status
  • $ sudo journalctl -u myapp -f  # Follow application logs in real-time
  • $ sudo systemctl reset-failed myapp  # Reset crash rate limits

Note: Restart modes include no, always, on-failure, and on-abnormal. always also restarts a service after a clean exit (0), while on-failure is generally suitable for services that should restart after crashes or unsuccessful exits.




LinuxTeck.com
linuxteck@ubuntu:~$ sudo systemctl edit --force --full myapp.service

linuxteck@ubuntu:~$ cat /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
StartLimitBurst=3
StartLimitIntervalSec=60

[Service]
Type=simple
User=linuxteck
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/run.sh
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

linuxteck@ubuntu:~$ sudo systemctl daemon-reload
linuxteck@ubuntu:~$ sudo systemctl enable --now myapp
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #37: My backup is killing disk performance. How do I slow it down? NEXT ARTICLE Quick Linux Tip #39: How do I access a private database from my laptop through a jump server?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.