Skip to content

Instantly share code, notes, and snippets.

@taufiqpsumarna
Last active August 29, 2022 09:23
Show Gist options
  • Save taufiqpsumarna/092b3f93bb724434bc1582b5aee7950c to your computer and use it in GitHub Desktop.
Save taufiqpsumarna/092b3f93bb724434bc1582b5aee7950c to your computer and use it in GitHub Desktop.
Simple Bashscript For Checking and Starting PM2 Process Automatically

Created By Taufiq - 2022

This simple script will check your PM2 process, if there existing PM2 it will stop the process and delete the process then start new PM2 process

install npm PM2 as user or globally

npm install pm2
npm install -g pm2
#!/bin/bash
# Font & Color variables
red='\033[0;31m'
green='\033[0;32m'
clear='\033[0m'
Bold='\e[1m'
#PM2 process variables
PROCESS="server"
PROCESS_PATHS="path/to/service --name $PROCESS "
echo -e "Checking $PROCESS service status..."
if (pm2 list | grep $PROCESS);then
echo -e "PM2 $PROCESS : ${red}${Bold}Found${clear}, Terminate Client !" &&
pm2 stop $PROCESS && pm2 delete $PROCESS &&
echo -e "${green}${Bold}Start new $PROCESS Service...${Clear}" &&
pm2 serve $PROCESS_PATHS &&
echo -e "PM2 $PROCESS : ${green}${Bold}Online${clear}, Happy Ending !"
else
pm2 serve $PROCESS_PATHS &&
echo -e "PM2 $PROCESS : ${green}${Bold}Online${clear}, Happy Ending !"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment