最近在研究反向代理,用的nps,过些天研究明白了再单独唠唠,但是现在遇到问题了。 代理软件分客户端和服务器端,服务器端因为是Linux,各种设置很容易配置,但是我的客户端是Windows系统,遇到最头疼的一件事就是没有办法自动启动,后台运行,试了开机运行批处理,但是会有一个命令提示符框,还不能关掉,后来想注册成服务,找了很久,终于找到了一个工具:`winsw`。 它可以将Windows上的任何一个程序注册为服务,如果不需要,也可以方便的卸载服务。 ### 下载 Github:[https://github.com/kohsuke/winsw/releases](https://github.com/kohsuke/winsw/releases) 作者给出了两个版本,`dotNet2.0`和`dotNet4.0`,Win10当然选最新的`dotNet4.0`啦,如果你的系统是Win7之前的,那最好选择`dotNet2.0`版本。 下载完成后将文件名重命名为`winsw.exe`,方便命令调用。 ### 编写配置文件 参考官方给的`sample-*.xml`文档,我的配置是这样的。 ```xml npc npc nps反向代理客户端 npc.exe -server=1.1.1.1:8284 -vkey=123 Normal 15 sec npc.exe stop Automatic ``` ### 注册服务 配置完成后命名为`winsw.xml`,将`winsw.exe`,`winsw.xml`一同移动到`npc.exe`所在文件夹内,以管理员身份打开命令提示符,运行: ```shell # 安装 > winsw.exe install 2019-09-18 00:25:08,429 INFO - Installing the service with id 'npc' # 开启 > winsw.exe start 2019-09-18 00:25:24,883 INFO - Starting the service with id 'npc' # 关闭 > winsw.exe stop # 卸载 > winsw.exe uninstall ``` ###### 补充Linux下自启动,防止下次忘记了 编写脚本,我命名为`npsAutoStart.sh`,放在`/root`目录,其实就一行,但是需要加点东西,不然无法继续后续操作 ```shell #!/bin/sh # chkconfig: - 85 15 # description: Automatically start nps at boot. nps start ``` 赋予脚本可执行权限 ```shell chmod +x /root/npsAutoStart.sh ``` 有两种方法添加自启动脚本 **方法1:** 打开/etc/rc.d/rc/local文件,在末尾增加如下内容 ```shell /root/npsAutoStart.sh ``` > 在Centos7中,`/etc/rc.d/rc.local`的权限被降低了,所以需要执行如下命令赋予其可执行权限 > `chmod +x /etc/rc.d/rc.local` **方法二:** 将脚本移动到`/etc/rc.d/init.d`目录下 赋予脚本可执行权限 ```shell chmod +x /etc/rc.d/init.d/npsAutoStart.sh ``` 添加脚本到开机自动启动项目中 ```shell cd /etc/rc.d/init.d chkconfig --add npsAutoStart.sh chkconfig npsAutoStart.sh on ``` 大功告成 Loading... 最近在研究反向代理,用的nps,过些天研究明白了再单独唠唠,但是现在遇到问题了。 代理软件分客户端和服务器端,服务器端因为是Linux,各种设置很容易配置,但是我的客户端是Windows系统,遇到最头疼的一件事就是没有办法自动启动,后台运行,试了开机运行批处理,但是会有一个命令提示符框,还不能关掉,后来想注册成服务,找了很久,终于找到了一个工具:`winsw`。 它可以将Windows上的任何一个程序注册为服务,如果不需要,也可以方便的卸载服务。 ### 下载 Github:[https://github.com/kohsuke/winsw/releases](https://github.com/kohsuke/winsw/releases) 作者给出了两个版本,`dotNet2.0`和`dotNet4.0`,Win10当然选最新的`dotNet4.0`啦,如果你的系统是Win7之前的,那最好选择`dotNet2.0`版本。 下载完成后将文件名重命名为`winsw.exe`,方便命令调用。 ### 编写配置文件 参考官方给的`sample-*.xml`文档,我的配置是这样的。 ```xml <service> <id>npc</id> <name>npc</name> <description>nps反向代理客户端</description> <executable>npc.exe</executable> <startarguments>-server=1.1.1.1:8284 -vkey=123</startarguments> <priority>Normal</priority> <stoptimeout>15 sec</stoptimeout> <stopexecutable>npc.exe</stopexecutable> <stoparguments>stop</stoparguments> <startmode>Automatic</startmode> <onfailure action="restart" delay="120 sec" /> <log mode="reset"></log> </service> ``` ### 注册服务 配置完成后命名为`winsw.xml`,将`winsw.exe`,`winsw.xml`一同移动到`npc.exe`所在文件夹内,以管理员身份打开命令提示符,运行: ```shell # 安装 > winsw.exe install 2019-09-18 00:25:08,429 INFO - Installing the service with id 'npc' # 开启 > winsw.exe start 2019-09-18 00:25:24,883 INFO - Starting the service with id 'npc' # 关闭 > winsw.exe stop # 卸载 > winsw.exe uninstall ``` ###### 补充Linux下自启动,防止下次忘记了 编写脚本,我命名为`npsAutoStart.sh`,放在`/root`目录,其实就一行,但是需要加点东西,不然无法继续后续操作 ```shell #!/bin/sh # chkconfig: - 85 15 # description: Automatically start nps at boot. nps start ``` 赋予脚本可执行权限 ```shell chmod +x /root/npsAutoStart.sh ``` 有两种方法添加自启动脚本 **方法1:** 打开/etc/rc.d/rc/local文件,在末尾增加如下内容 ```shell /root/npsAutoStart.sh ``` > 在Centos7中,`/etc/rc.d/rc.local`的权限被降低了,所以需要执行如下命令赋予其可执行权限 > `chmod +x /etc/rc.d/rc.local` **方法二:** 将脚本移动到`/etc/rc.d/init.d`目录下 赋予脚本可执行权限 ```shell chmod +x /etc/rc.d/init.d/npsAutoStart.sh ``` 添加脚本到开机自动启动项目中 ```shell cd /etc/rc.d/init.d chkconfig --add npsAutoStart.sh chkconfig npsAutoStart.sh on ``` 大功告成 最后修改:2021 年 04 月 25 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏