From 0417d15e307ffeac4dc26f5b0a7527c2121b2ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A9=BA=E8=A8=80?= <43477078+soragotosann@users.noreply.github.com> Date: Tue, 24 Nov 2020 22:27:34 +0800 Subject: [PATCH] update systemd install script (#216) * add systemd install script * rename systemd script * fix * fix * fix: update readme, python script * Update systemd.sh update * Update systemd.sh Co-authored-by: New Future --- README.md | 14 ++++++++++++-- run.py | 3 ++- systemd.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100755 systemd.sh diff --git a/README.md b/README.md index 46052ea..3fdfa45 100644 --- a/README.md +++ b/README.md @@ -187,8 +187,18 @@ python run.py -c /path/to/config.json #### linux -运行 `sudo ./task.sh` - +- 使用init.d和crontab: +`sudo ./task.sh` +- 使用systemd: + ```bash + 安装: + sudo ./systemd.sh install + 卸载: + sudo ./systemd.sh uninstall + ``` + 该脚本安装的文件符合 [Filesystem Hierarchy Standard (FHS)](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard): + 可执行文件所在目录为 `/usr/share/DDNS` + 配置文件所在目录为 `/etc/DDNS` ## FAQ diff --git a/run.py b/run.py index 91a2a71..d1faf3c 100755 --- a/run.py +++ b/run.py @@ -70,7 +70,8 @@ def get_config(key=None, default=None, path="config.json"): "debug": False, } dumpjson(configure, configfile, indent=2, sort_keys=True) - sys.exit("New template configure file `%s` is generated." % path) + sys.stdout.write("New template configure file `%s` is generated.\n" % path) + sys.exit(1) except: sys.exit('fail to load config from file: %s' % path) if key: diff --git a/systemd.sh b/systemd.sh new file mode 100755 index 0000000..3688d90 --- /dev/null +++ b/systemd.sh @@ -0,0 +1,54 @@ +# /bin/bash +service='[Unit] +Description=NewFuture ddns +After=network.target + +[Service] +Type=simple +WorkingDirectory=/usr/share/DDNS +ExecStart=/usr/bin/env python /usr/share/DDNS/run.py -c /etc/DDNS/config.json + +[Install] +WantedBy=multi-user.target' + +timer='[Unit] +Description=NewFuture ddns timer + +[Timer] +OnUnitActiveSec=5m +Unit=ddns.service + +[Install] +WantedBy=multi-user.target' + +if [[ "install" == $1 ]]; then + echo "$service" > /usr/lib/systemd/system/ddns.service + echo "$timer" > /usr/lib/systemd/system/ddns.timer + cp -r `pwd` /usr/share/ + mkdir -p /etc/DDNS + if [ ! -f "/etc/DDNS/config.json" ];then + if [ -f "config.json" ];then + cp config.json /etc/DDNS/config.json + fi + fi + systemctl enable ddns.timer + systemctl start ddns.timer + echo "installed" + echo "useful commands:" + echo " systemctl status ddns view service status." + echo " journalctl -u ddns.timer view the logs." + echo "config file: /etc/DDNS/config.json" + +elif [[ "uninstall" == $1 ]]; then + systemctl disable ddns.timer + rm /usr/lib/systemd/system/ddns.service + rm /usr/lib/systemd/system/ddns.timer + rm -rf /etc/DDNS + rm -rf /usr/share/DDNS + systemctl daemon-reload + echo "uninstalled" +else + echo "Tips:" + echo " $0 install install the ddns systemd service." + echo " $0 uninstall uninstall the ddns service." +fi