- 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啓動訪問網絡失敗,實際網絡是通的,原因應該是網絡初始化沒有完成?
解決的辦法:
- 在網絡初始化之後(沒有找到怎麼具體設置)
- 自動重啓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