DEV Community

Ashish R Bhandari
Ashish R Bhandari

Posted on

Run a Program(like App Server) like a SystemD Service

A Very Simple Example of a SystemD COnfigured Service.
Inorder to run a Program like a Service in Background in 2 SImple Steps

Step1: Create a File inside Dir
/lib/systemd/system/
wiith Extension as .service

Step 2: Add the Service Details and Behaviour Etc like creating a Setting File to Tell SystemD what to do

#Section 1
# Provide Details like Description and After is Requried by Your Service i.e if it requires a Interface then below After is Helpfull.
[Unit]
Description=A Simple Poster App ( A Very Cheap Posting System like Twitter) User can Create Post, Delete Post, View Other Posts etc
Documentation=https://app.poster.com
After=network.target

# Section 2 
# ENV Variables can be created here
# Very VVV IMP `WorkingDirectory`
# Most Obvious Command How to Start the Program `ExecStart`
# `Restart` if the Program crashes due to some reason u have a failover.
# User Optional and Depending on Requirement.

[Service]
Environment=NODE_PORT=81
Type=simple
User=root
ExecStart=/usr/bin/node /usr/local/src/apps/app.poster_v2/app.js
WorkingDirectory=/usr/local/src/apps/app.poster_v2
Restart=on-failure

## When and Where this Service should Start
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Example:


#File Content
[Unit]
Description=A Simple Poster App ( A Very Cheap Posting System like Twitter) User can Create Post, Delete Post, View Other Posts etc
Documentation=https://app.poster.com
After=network.target

[Service]
Environment=NODE_PORT=81
Type=simple
User=root
ExecStart=/usr/bin/node /usr/local/src/apps/app.poster_v2/app.js
WorkingDirectory=/usr/local/src/apps/app.poster_v2
Restart=on-failure

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Top comments (0)