docker 定时重启脚本_使用 Go 添加启动脚本
生活随笔
收集整理的這篇文章主要介紹了
docker 定时重启脚本_使用 Go 添加启动脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 簡介
- 實踐
- 困惑
- 總結
- 當前部分的代碼
簡介
雖然 Makefile 能很好的整合各種命令, 是一個非常方便的工具. 但啟動腳本也是必不可少的, Makefile 更多用于開發階段, 比如編譯, 單元測試等流程.
啟動腳本的作用是控制程序的狀態, 管理程序的啟動, 停止, 查詢運行狀態等.
實踐
直接上腳本了:
#!/bin/bashSERVER="web" BASE_DIR=$PWD INTERVAL=2# 命令行參數,需要手動指定, 這是在 docker 容器中運行的參數 ARGS="-c $BASE_DIR/conf/config_docker.yaml"function start() {if [ "`pgrep $SERVER -u $UID`" != "" ];thenecho "$SERVER already running"exit 1finohup $BASE_DIR/$SERVER $ARGS >/dev/null 2>&1 &echo "sleeping..." && sleep $INTERVAL# check statusif [ "`pgrep $SERVER -u $UID`" == "" ];thenecho "$SERVER start failed"exit 1elseecho "start success"fi }function status() {if [ "`pgrep $SERVER -u $UID`" != "" ];thenecho $SERVER is runningelseecho $SERVER is not runningfi }function stop() {if [ "`pgrep $SERVER -u $UID`" != "" ];thenkill `pgrep $SERVER -u $UID`fiecho "sleeping..." && sleep $INTERVALif [ "`pgrep $SERVER -u $UID`" != "" ];thenecho "$SERVER stop failed"exit 1elseecho "stop success"fi }function version() {$BASE_DIR/$SERVER $ARGS version }case "$1" in'start')start;;'stop')stop;;'status')status;;'restart')stop && start;;'version')version;;*)echo "usage: $0 {start|stop|restart|status|version}"exit 1;; esac用法如下:
- ./admin.sh start 啟動
- ./admin.sh stop 停止
- ./admin.sh restart 重啟
- ./admin.sh status 查看狀態
- ./admin.sh version 查看版本
困惑
在運行啟動腳本的過程中遇到了一個問題, 就是使用腳本 stop 進程的時候, 進程會變成僵尸進程(Zombies), 而不是正常停止.
但如果不使用 nohup, 直接在前臺運行, 然后在另一個終端中關閉, 是會關閉的.
這個問題困擾了我很久, 直到看到 stackoverflow 上的 類似問題.
這是在評論中發現的, 有時候豁然開朗就在一瞬間,
If you're running the process (even if you've called wait finally) inside the docker container with pid:1, it will also lead to a zombie. http://github.com/krallin/tiniwill be helpful in this case. – McKelvin Mar 8 '17 at 11:34只要在 docker-compose 中設置 init 為 true 就行了, 類似這樣:
version: "3.7" services:web:image: alpine:latestinit: true這會在 docker 容器內運行一個 init 來轉發信號, 默認的 init 程序就是上面提到的 Tini.
這是在使用 容器開發 時遇到的問題.
總結
啟動腳本是一個非常方便的工具, 用于管理進程的啟動和停止.
當前部分的代碼
作為版本 v0.13.0
總結
以上是生活随笔為你收集整理的docker 定时重启脚本_使用 Go 添加启动脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cs代码写在html页面哪里,当用于在d
- 下一篇: hana SQL函数