To run a Bun application as a daemon using systemd you’ll need to create a service file in
/lib/systemd/system/
.
terminal
Here is a typical service file that runs an application on system start. You can use this as a template for your own service. Replace
YOUR_USER
with the name of the user you want to run the application as. To run as root
, replace YOUR_USER
with root
, though this is generally not recommended for security reasons.
Refer to the systemd documentation for more information on each setting.
my-app.service
If your application starts a webserver, note that non-
root
users are not able to listen on ports 80 or 443 by default. To permanently allow Bun to listen on these ports when executed by a non-root
user, use the following command. This step isn’t necessary when running as root
.
terminal
With the service file configured, you can now enable the service. Once enabled, it will start automatically on reboot. This requires
sudo
permissions.
terminal
To start the service without rebooting, you can manually start it.
terminal
Check the status of your application with
systemctl status
. If you’ve started your app successfully, you should see something like this:
terminal
To update the service, edit the contents of the service file, then reload the daemon.
terminal
For a complete guide on the service unit configuration, you can check this page. Or refer to this cheatsheet of common commands:
terminal