如何在Windows上和Linux上配置自启动服务?

20240130214033

我们以 FastTunnel 这个内网穿透工具为例,其中Server端的程序运行在Linux上,Client端的程序运行在Windows上。关于这个程序的配置请参考官方文档: 快速上手:快速搭建服务。为了避免每次手动启动程序,我们把它们做成开启自启动的服务,这样你就可以无缝使用远程桌面了。

Windows

  1. 下载 nssm http://nssm.cc/download
  2. 下载 FastTunnel 客户端,解压到任意目录,例如:F:\Program Files\FastTunnel.Client.win-x64
  3. 在nssm的目录下打开命令行(必须用管理员权限),并用如下命令安装:
# DisplayName和Description可以随意写。
nssm install FastTunnel "F:\Program Files\FastTunnel.Client.win-x64\FastTunnel.Client.exe"
nssm set FastTunnel DisplayName FastTunnel
nssm set FastTunnel Description FastTunnel for remote desktop
  1. 搜索Services并打开在Services列表里找到刚刚创建的服务。

20240130213429
20240130213513

  1. 右键,打开属性面板,确保Startup为自动。
    20240130213633

  2. 设置失败后自动重启(对于一个稳定的软件,你可以选择无限重启)
    20240130213754

  3. 手动start这个服务。

搞定!

Linux

  1. 将软件解压到 FastTunnel.Server.linux-x64 目录下
  2. 按照如下配置新建一个文件 FastTunnel.service,并复制到 /etc/systemd/system/ 目录下
# copy the file to 
# /etc/systemd/system/FastTunnel.service

[Unit]
Description=FastTunnel service
After=network.target

[Service]
User=[username]
Group=[username]
WorkingDirectory=/home/[username]/FastTunnel.Server.linux-x64
ExecStart=/home/[username]/FastTunnel.Server.linux-x64/FastTunnel.Server

[Install]
WantedBy=multi-user.target
  1. 开启服务
sudo systemctl enable FastTunnel
sudo systemctl start FastTunnel
  1. 重新加载服务
sudo systemctl daemon-reload
  1. 重启服务
sudo systemctl restart FastTunnel
  1. 停止服务
sudo systemctl stop FastTunnel

本文由博客一文多发平台 OpenWrite 发布!

posted @ 2024-01-30 21:50  黑客悟理  阅读(33)  评论(0编辑  收藏  举报