Systemd
From Nest Wiki
More actions
The (generally) best way to run your apps 24/7 in the background on nest is through the use of systemd. It runs as PID 1 and is the first thing that starts in any nest container. For more information check out https://wiki.archlinux.org/title/Systemd (one of the best systemd docs in my opinion).
Tutorial
We are going to run your project as a systemd service, which are usually located in /etc/systemd/system/<name>.service. To create/edit this, file you can either use nano /etc/systemd/system/<name>.service or systemctl edit --full --force <name>.service. For this guide we will use the latter. If you use nano, you will also need to run systemctl daemon-reload after.
- Find out how you are running your project exactly. If you are running with node, run
which nodeand note down the full path you get (for example/usr/local/bin/node). For anything else it's the same, just runwhich python/bun/etc. - Create a new systemctl service with
systemctl edit --full --force <name>.service, make sure name is unique and represents your project (for example: "cool-slack-bot" is good, python is bad). Example:systemctl edit --full --force cool-slack-bot.service - Paste this file, and edit it to match your project:
[Unit]
Description=Cool Slack Bot
Wants=network-online.target
After=network-online.target
[Service]
# If you are not using node, replace /usr/local/bin/node
# Also make sure to replace <project>, and index.js if necessary
ExecStart=/usr/local/bin/node /root/<project>/index.js
# Make sure to replace <project>
WorkingDirectory=/root/<project>
Restart=always
RestartSec=5
# Uncomment this if you have a .env file, change the path if needed:
# EnvironmentFile=/root/<project>/.env
[Install]
WantedBy=multi-user.target
- Enable and start your project using
systemctl enable <name>.service --now, then check if it's running withsystemctl status <name>.service. - If you need to view the latest logs for the service, use
journalctl -eu <name>.