mw 支持 systemctl
This commit is contained in:
midoks 2022-06-17 15:52:52 +08:00
parent 949a537f46
commit 2abce6e7d6
3 changed files with 55 additions and 13 deletions

View File

@ -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():

View File

@ -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

14
scripts/init.d/mw.service.tpl Executable file
View File

@ -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