Skip to content

Instantly share code, notes, and snippets.

@ivangonzalezg
Last active December 29, 2023 00:17
Show Gist options
  • Save ivangonzalezg/f4ae48abfd4873d8b980b3cb9a469d4c to your computer and use it in GitHub Desktop.
Save ivangonzalezg/f4ae48abfd4873d8b980b3cb9a469d4c to your computer and use it in GitHub Desktop.
Create a Deamon on Ubuntu
# Set up the service to start on server restart
sudo systemctl enable ZZZZZZ
# Create the executable
#!/bin/bash
# Project folder
cd /home/ubuntu/YYYYYY
# Command to start the project
yarn start
# Create a file in “/etc/systemd/system” and add the extension “.service”
[Unit]
Description=Node API trigger
# Dependent services
After=mongod.service
[Service]
# Username
User=root
# Executable folder
WorkingDirectory=/home/ubuntu
# Executable path
ExecStart=/home/ubuntu/XXXXXX
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
# Pro tip: Create a script to update your project
#!/bin/bash
echo "Stoping ZZZZZZ service"
sudo service API stop
cd /home/ubuntu/YYYYYY
echo "Getting changes"
git pull
echo "Instaling dependencies"
yarn
echo "Starting ZZZZZZ service"
sudo service ZZZZZZ start
echo "ZZZZZZ service updated"
XXXXXX: name of the executable
YYYYYY: folder of the project
ZZZZZZ: name of the service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment