做node层后台有一阵时间,记录一下node作为一个后台服务的部署经历的一个演进路程
常规写一个入口文件(大体是注册一些事件,然后启动一个类似ngix服务的东西)然后命令行敲node 这个入口文件名,服务也算是启动起来了~
但是呢,这种方法有个问题,就是命令行窗口不能关闭,关闭了,服务就挂了,开发什么的无所谓,测试生产的服务器是不可能做到这一点的,所以想了个法子,让服务后台运行,这就又进一步了,写了个脚本文件server.sh,大体是这样。
APP_HOME=$(echo `pwd` | sed 's/\/bin//')
SERVER_PATH=$APP_HOME/server
LOG_PATH=$APP_HOME/log
DIR_ARR=(${APP_HOME })
APP_NAME=${DIR_ARR[${#DIR_ARR[@]}-1]}
if [ $1 = "start" -o $1 = "restart" ] && [ $2 = "testing" -o $2 = "production" ];
then
echo "Will" $1 "server with environment > " $2
else
echo -e "\n \033[47;31m Usage: server.sh {start|restart} {testing|production} \033[0m \n"
echo "This shell shall only be executed for these situation:"
echo "1. First time to launch a server of project dev and built with vue-koa-boilerplate"
echo "2. Force restart the server. Will delete the original application in PM2"
exit -1
fi
cd $SERVER_PATH
case "$1" in
start)
echo "Luanching application server with PM2 for the first time..."
sleep 1
pm2 start pm2.json --env $2
echo "Commands of PM2 are available now."
;;
stop)
echo "This shell is not built for st