From 2abce6e7d6319fd2eedc0422b9c6e607fb7f1255 Mon Sep 17 00:00:00 2001 From: midoks Date: Fri, 17 Jun 2022 15:52:52 +0800 Subject: [PATCH] #39 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit mw 支持 systemctl --- class/core/common.py | 40 +++++++++++++++++++++++++------------- scripts/init.d/mw-task.service.tpl | 14 +++++++++++++ scripts/init.d/mw.service.tpl | 14 +++++++++++++ 3 files changed, 55 insertions(+), 13 deletions(-) create mode 100755 scripts/init.d/mw-task.service.tpl create mode 100755 scripts/init.d/mw.service.tpl diff --git a/class/core/common.py b/class/core/common.py index f98aacfd8..6c81acb55 100755 --- a/class/core/common.py +++ b/class/core/common.py @@ -50,30 +50,44 @@ def initDB(): print(str(ex)) -def initInitD(): - script = mw.getRunDir() + '/scripts/init.d/mw.tpl' - script_bin = mw.getRunDir() + '/scripts/init.d/mw' - # if os.path.exists(script_bin): - # return - - content = mw.readFile(script) +def doContentReplace(src, dst): + content = mw.readFile(src) content = content.replace("{$SERVER_PATH}", mw.getRunDir()) + mw.writeFile(dst, content) - mw.writeFile(script_bin, content) - mw.execShell('chmod +x ' + script_bin) +def initInitD(): mw.setHostAddr(mw.getLocalIp()) - if not mw.isAppleSystem(): + # systemctl + if os.path.exists('/lib/systemd/system'): + systemd_mw = '/lib/systemd/system/mw-task.service' + systemd_mw_task = '/lib/systemd/system/mw-task.service' + + systemd_mw_tpl = mw.getRunDir() + '/scripts/init.d/mw.service.tpl' + systemd_mw_task_tpl = mw.getRunDir() + '/scripts/init.d/mw-task.service.tpl' + + doContentReplace(systemd_mw_tpl, systemd_mw) + doContentReplace(systemd_mw_task_tpl, systemd_mw_task) + + mw.execShell('systemctl enable mw') + mw.execShell('systemctl enable mw-task') + mw.execShell('systemctl daemon-reload') + + script = mw.getRunDir() + '/scripts/init.d/mw.tpl' + script_bin = mw.getRunDir() + '/scripts/init.d/mw' + doContentReplace(script, script_bin) + mw.execShell('chmod +x ' + script_bin) + + # initd + if os.path.exists("/etc/init.d"): initd_bin = '/etc/init.d/mw' if not os.path.exists(initd_bin): import shutil shutil.copyfile(script_bin, initd_bin) mw.execShell('chmod +x ' + initd_bin) # 加入自启动 - mw.execShell('chkconfig --add mw') - mw.execShell('systemctl enable mw') - mw.execShell('systemctl daemon-reload') + mw.execShell('which chkconfig && chkconfig --add mw') def initUserInfo(): diff --git a/scripts/init.d/mw-task.service.tpl b/scripts/init.d/mw-task.service.tpl new file mode 100755 index 000000000..76da148a5 --- /dev/null +++ b/scripts/init.d/mw-task.service.tpl @@ -0,0 +1,14 @@ +[Unit] +Description=mw-task server daemon +After=network.target + +[Service] +Type=forking +ExecStart=cd {$SERVER_PATH} && python3 task.py +ExecStop=kill -HUP $MAINID +ExecReload=kill -HUP $MAINID +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/scripts/init.d/mw.service.tpl b/scripts/init.d/mw.service.tpl new file mode 100755 index 000000000..e19baf38e --- /dev/null +++ b/scripts/init.d/mw.service.tpl @@ -0,0 +1,14 @@ +[Unit] +Description=mw server daemon +After=network.target + +[Service] +Type=forking +ExecStart=cd {$SERVER_PATH} && gunicorn -c setting.py app:app +ExecStop=kill -HUP $MAINID +ExecReload=kill -HUP $MAINID +KillMode=process +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file