I've searched for ways to automatically restart my application if it crashes, and have only found solutions which need extra software or something like 'forever'.
After a while I found out that you can do this with systemd, the init-process for most linux distrubutions nowadays.
This has 2 major advantages: It uses the internal journal for logging and it can easily be controlled with systemctl.
Creating your own systemd service is quite easy, as it's only a file that you have to create.
First create your service file and add it to /etc/systemd/system. This example uses a NodeJS server, so the file could be named /etc/systemd/system/myapp.service
The Lines are pretty self-explanatory, but don't forget to change it to your own settings!
Contents of the .service file:
[Unit] Description=My NodeJS Server [Service] ExecStart=/usr/bin/node /home/user/myapp/index.js Restart=always RestartSec=5 SyslogIdentifier=myapp User=user Group=user Environment=NODE_ENV=production PORT=3000 [Install] WantedBy=multi-user.target
sudo systemctl start myappThis command simply starts your service. It will automatically restart 5 seconds after crashing thanks to our 6/7th line in the service files.
sudo systemctl enable myappTo check the status use status instead of enable. With stop you can stop the service, and to disable the auto-startup simply use disable instead of enable.