## 獨立部署
獨立部署即為在后端運行程序,讓程序跑在后臺。
## 部署的目錄
通常我們不需要部署源碼到服務器,只需要部署二進制和配置文件:
~~~
.
├── bin
├── conf
├── runtime
└── .env
~~~
## daemon 參數
- 通過骨架代碼中自帶的 `-d/--daemon` 參數使程序在后臺執行。
~~~
$ mixapp api -d
~~~
~~~
$ mixapp api --daemon
~~~
## nohup
通過 Linux 的 `nohup` 命令使程序在后臺執行。
~~~
$ nohup /data/mix/bin/mixapp api >/dev/null 2>&1 &
~~~
## shell
通常我們會使用一個簡單的 shell 腳本來管理 mix 進程,保存以下腳本為 `mix.sh`:
- `file` mix 入口文件絕對路徑
- `cmd` 使用的 mix 命令名稱
~~~
#!/bin/sh
echo "============`date +%F' '%T`==========="
file=/data/mix/bin/mixapp
cmd=api
getpid()
{
docmd=`ps aux | grep ${file} | grep ${cmd} | grep -v 'grep' | grep -v '\.sh' | awk '{print $2}' | xargs`
echo $docmd
}
start()
{
pidstr=`getpid`
if [ -n "$pidstr" ];then
echo "running with pids $pidstr"
else
$file $cmd -d
sleep 1
pidstr=`getpid`
echo "start with pids $pidstr"
fi
}
stop()
{
pidstr=`getpid`
if [ ! -n "$pidstr" ];then
echo "Not Executed!"
return
fi
echo "kill $pidstr"
kill $pidstr
}
restart()
{
stop
sleep 1
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
esac
~~~
腳本如何使用:
- `sh mix.sh start`
- `sh mix.sh stop`
- `sh mix.sh restart`
> 有些公司喜歡使用 crontab 拉起異常停止的程序,可以定時調用該腳本的 start 方法