ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 独立部署 独立部署即为在后端运行程序,让程序跑在后台。 ## 部署的目录 通常我们不需要部署源码到服务器,只需要部署二进制和配置文件: ~~~ . ├── 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 方法