封装frp为service实现开机自启动

  • frp简介

frp(https://github.com/fatedier/frp)是go语言开发的一款优秀的内网穿透工具,可以方便进行网络转发的功能,但是在客户端需要运行frpc来进行相关转发的实现。为了避免每次开机都需要手动运行相关的脚本,决定封装一个service交给系统自动管理。

  • service 封装
[Unit]
Description = service for frp client
[Service]
Type = simple
ExecStart = /home/andy/applications/frp/frpc -c /home/andy/applications/frpc.ini

将封装的service保存为frpc.service,软连接到目录下 /etc/systemd/system下,此时已经可以使用

service frpc status|start|stop 
  • 开机自启动

对封装的frpc.service进行基本的管理,但是在添加到开机自启动的时候会出现一下错误

The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
Alias= settings in the [Install] section, and DefaultInstance= for template
units). This means they are not meant to be enabled using systemctl.

Possible reasons for having this kind of units are:
• A unit may be statically enabled by being symlinked from another unit's
  .wants/ or .requires/ directory.
• A unit's purpose may be to act as a helper for some other unit which has
  a requirement dependency on it.
• A unit may be started when needed via activation (socket, path, timer,
  D-Bus, udev, scripted systemctl call, ...).
• In case of template units, the unit is meant to be enabled with some
  instance name specified.

故而添加[Install]相关的配置,新的service如下:

[Unit]
Description = service for frp client
[Service]
Type = simple
ExecStart = /home/andy/applications/frp/frpc -c /home/andy/applications/frp/frpc.ini
[Install]
WantedBy=multi-user.target

重新加载service并添加到开机自启动service可以实现需要的功能。

systemctl daemon-reload # reload the defined service
systemctl enable frpc.service # 实现开机service自启动
  • 实操存在的问题

但是在实际的开机过程中发现,service启动访问网络失败,实际网络是通的,原因应该是网络初始化没有完成?

解决的办法:

  1. 在网络初始化之后(没有找到怎么具体设置)
  2. 自动重启service(可以配置service自动重启)

所以最终修改的service为:

[Unit]
Description = service for frp client
[Service]
Type = simple
ExecStart = /home/andy/applications/frp/frpc -c /home/andy/applications/frp/frpc.ini
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target

最终的目的是学习service的一般的封装方法及相关的操作。 PS:

发现一篇写得很详细的关于systemd的博客:Linux 守护进程之systemd

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章