Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add systemd #212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,67 @@ hosts 文件在每个系统的位置不一,详情如下:

### 2.3 一行命令 (适用于类 Unix 系统)

#### 使用 Systemd 管理的 Linux

<details>
<summary><b>Linux 发行版中 systemd 软件包状态</b></summary>

[![Packaging status](https://repology.org/badge/vertical-allrepos/systemd.svg)](https://repology.org/project/systemd/versions)

</details>

```bash
# 方式 1 : 克隆 GitHub520 仓库
git clone https://github.com/521xueweihan/GitHub520.git

# 进入 GitHub520 仓库
cd GitHub520

# 方式 2:还可以通过下载 raw 文件方式下载相应的文件
wget -O remove-github520-hosts.service https://raw.githubusercontent.com/521xueweihan/GitHub520/main/remove-github520-hosts.service && \
wget -O update-github520-hosts.service https://raw.githubusercontent.com/521xueweihan/GitHub520/main/update-github520-hosts.service && \
wget -O update-github520-hosts.timer https://raw.githubusercontent.com/521xueweihan/GitHub520/main/update-github520-hosts.timer

# 安装 github520 host 的 systemd 服务到系统的 systemd 目录
sudo install -Dm0644 *-github520-hosts.* -t /usr/lib/systemd/system/

# 手动刷新 systemd 服务列表
sudo systemctl daemon-reload

# 使用 systemd 的 systemctl 设置 github520 定时服务自启并运行
sudo systemctl enable --now update-github520-hosts.timer

# 使用 systemd 的 systemctl 设置 github520 服务运行,如果不手动需要等定时服务自动运行
sudo systemctl start update-github520-hosts.service

# 使用 systemd 的 systemctl 查看 github520 服务运行状态
sudo systemctl status update-github520-hosts.service

# 核查 /etc/hosts 修改
cat /etc/hosts

# 使用 systemd 的 systemctl 移除 github520 对 /etc/hosts 的修改
sudo systemctl start remove-github520-hosts.service

# 核查 /etc/hosts 修改
cat /etc/hosts

# 使用 systemd 的 systemctl 移除 github520 定时服务自启
sudo systemctl disable update-github520-hosts.timer

# 使用 systemd 的 systemctl 停止 github520 定时服务
sudo systemctl stop update-github520-hosts.timer

# 系统的 systemd 目录中删除 github520 host 的 systemd 服务
sudo rm -rf *-github520-hosts.* -t /usr/lib/systemd/system/
```

- Arch Linux: [AUR github520-git](https://aur.archlinux.org/packages/github520-git)

```bash
yay -Syu github520
```

#### GNU(Ubuntu/CentOS/Fedora)

`sudo sh -c 'sed -i "/# GitHub520 Host Start/Q" /etc/hosts && curl https://raw.hellogithub.com/hosts >> /etc/hosts'`
Expand Down
11 changes: 11 additions & 0 deletions remove-github520-hosts.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Remove GitHub520 Hosts File
After=network.target

[Service]
User=root
PermissionsStartOnly=true
ExecStart=/bin/bash -c 'sed -i "/# GitHub520 Host Start/,/# GitHub520 Host End/d" /etc/hosts'

[Install]
WantedBy=default.target
14 changes: 14 additions & 0 deletions update-github520-hosts.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Update GitHub520 Hosts File
After=network.target

[Service]
User=root
PermissionsStartOnly=true
ExecStart=/bin/bash -c 'sed -i "/# GitHub520 Host Start/Q" /etc/hosts && { wget -O - https://raw.hellogithub.com/hosts || wget -O - https://raw.githubusercontent.com/521xueweihan/GitHub520/main/hosts; } >> /etc/hosts'
Restart=on-failure
RestartSec=30
StartLimitInterval=3min

[Install]
WantedBy=default.target
9 changes: 9 additions & 0 deletions update-github520-hosts.timer
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Update GitHub520 Hosts File Timer

[Timer]
OnCalendar=*-*-* *:0/1:00
Persistent=true

[Install]
WantedBy=timers.target