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

后台运行的正确姿势 #71

Open
blurgyy opened this issue May 4, 2020 · 1 comment
Open

后台运行的正确姿势 #71

blurgyy opened this issue May 4, 2020 · 1 comment

Comments

@blurgyy
Copy link

blurgyy commented May 4, 2020

关于 #45 提到的 root 权限, 分享一个使用普通用户的 systemd file 和相关配置

  • 新建用户 tg-bot 并禁止登录
useradd tg-bot -s /usr/sbin/nologin
  • 为用户 tg-bot 建立用于放置 rssbot.json 的文件夹,
sudo mkdir /var/lib/tg-bots-data
sudo chown tg-bot:tg-bot /var/lib/tg-bots-data
  • 编辑 /etc/systemd/system/rssbot.service, 写入
[Unit]
Description = RSS Bot for Telegram
Documentation = https://github.com/iovxw/rssbot
After = network.target
Wants = network-online.target

[Service]
# 对于 systemd 版本 240 及以上, 取消注释 `Type=exec`, 注释掉 `Type=simple` 
# Type=exec
Type = simple
User = tg-bot
Group = tg-bot
NoNewPrivileges = yes

ExecStart = /path/to/rssbot -d /var/lib/tg-bots-data/rssbot.json <token>

[Install]
WantedBy = multi-user.target

/path/to/rssbot 改成自己的可执行文件路径, <token> 改成自己的 token.

  • 启动
sudo systemctl daemon-reload
sudo systemctl start rssbot.service
sudo systemctl enable rssbot.service
systemctl status rssbot.service
  • 这样就可以以最小权限运行 rssbot, 使用 ps -ef | grep rssbot 查看:
tg-bot   28290     1  0 06:47 ?        00:00:57 /path/to/rssbot -d /var/lib/tg-bots-data/rssbot.json <token>

当然直接用 root 也可以...不过跑自己服务器上的东西还是想尽量简单嘛

@lxl66566
Copy link

也可以考虑 systemd-run,一行解决:systemd-run rssbot <token>
本质上是创建临时 service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants