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