This guide provides the main points for setting up a systemd service with a working directory, an ExecStart
command, and automatic restart functionality.
/etc/systemd/system/<service_name>.service
/etc/systemd/system/myapp.service
In the service file, include the following sections:
network.target
for network-dependent services.WorkingDirectory
: The directory from which the service should be executed.ExecStart
: The command to start the service, usually the path to the executable.User
: (Optional) The user under which the service will run.Restart
: Define restart behavior, typically on-failure
.RestartSec
: (Optional) Time to wait before restarting the service after a failure.WantedBy
: Define the target that the service should be attached to, usually multi-user.target
.sudo systemctl daemon-reload
sudo systemctl enable <service_name>.service
sudo systemctl start <service_name>.service
sudo systemctl status <service_name>.service
sudo systemctl restart <service_name>.service
journalctl -u <service_name>.service
Here's an example of a systemd service file for a Node.js application:
[Unit]
Description=Node.js Example App
After=network.target
[Service]
WorkingDirectory=/path/to/nodejs/app
ExecStart=/usr/bin/node /path/to/nodejs/app/app.js
User=nodeuser
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
WorkingDirectory
: This is the directory where your Node.js app resides, e.g., /path/to/nodejs/app
.ExecStart
: This is the command to start your Node.js app, e.g., node app.js
./path/to/nodejs/app
and nodeuser
with the actual path to your application and the username under which it should run.