Skip to content

Instantly share code, notes, and snippets.

@serslon
Last active January 14, 2019 14:07
Show Gist options
  • Select an option

  • Save serslon/01f247eb268c80a944f4fb0bc9613723 to your computer and use it in GitHub Desktop.

Select an option

Save serslon/01f247eb268c80a944f4fb0bc9613723 to your computer and use it in GitHub Desktop.
systemd example configuration for run nodejs application like service
[Unit]
Description=MVP Admin application
Requires=After=nginx.service # Requires the nginx and mongo service to run first
Requires=After=mongod.service
[Service]
# Start application if need collection application log to one file
ExecStart=/bin/sh -c '/usr/bin/nodejs <application_path>/server/index.js 2>&1 > <path_to_logs>/<application>.log'
# If applicatuion have other system collection logs, can usage short run string
# ExecStart=/usr/bin/nodejs <application_path>/server/index.js
Restart=always
RestartSec=10 # Restart service after 10 seconds if node service crashes
StandardOutput=syslog # Output to syslog
StandardError=syslog # Output to syslog
SyslogIdentifier=<application>
User=<user>
Group=<user>
Environment= NODE_ENV=production # Also can add other environment like DEBUG=* DEBUG_FD=1
WorkingDirectory=<application_path>
[Install]
WantedBy=multi-user.target
1. INSTALL as service *mvp-admin*: Run command `systemctl enable /srv/mvp-admin/server/systemd/<application>.service`
2. Enable control service from user:
- Create file in `/etc/sudoers.d` with name user. Command: `touch /etc/sudoers.d/<user>`
- Add to file some command:
- %<user> ALL= NOPASSWD: /bin/systemctl stop <application>
- %<user> ALL= NOPASSWD: /bin/systemctl start <application>
- %<user> ALL= NOPASSWD: /bin/systemctl restart <application>
3. After any change in files <application>.service need restart systemd next command `systemctl daemon-reload`
Now you can constrol service from user:
systemctl start <application> # for start application
systemctl restart <application> # for restart application
systemctl stop <application> # for stop application
@jeremy21212121
Copy link
Copy Markdown

Typo in Note.txt: "systemctrl" should be "systemctl" on lines 8,11,12,13

I make this typo so much myself that I symlinked systemctrl-->systemctl 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment