[{"data":1,"prerenderedAt":238},["ShallowReactive",2],{"tag-服务-1":3},{"title":4,"articles":5,"hot_article":27,"month_arch":86,"total":234,"cover":235},"服务 - 独孤影的博客",[6,17],{"id":7,"title":8,"uri":9,"author":10,"tags":11,"created_at":14,"view_count":15,"content":16},106,"服务程序实现自己更新自己","how-a-service-upgrade-itself","duguying",[12,13],"服务","自更新","2023-10-13 17:42:30",379,"\u003Cp>作为一个广泛存在于众多设备上的常驻程序如何更新？他可以是一个agent，也可以是一个service，可以运行在众多容器中作为基础支持的程序，也可以运行在路由器、空调、洗衣机等家用设备或者物联设备上的支持程序。或许有些程序可以通过第三方平台进行更新，比如某些服务程序，配有专门的发版平台。但是更多的是无法通过发版平台发布的程序，比如是linux上的一个自启动的service，这个linux是你家里的路由器。这时候就需要程序自己更新自己了。\u003C\u002Fp>\n\n\u003Cp>程序更新通常有下列步骤，下载新的程序，替换旧的可执行文件和配置文件，重启程序（退出旧进程，拉起新进程）。\u003C\u002Fp>\n\n\u003Cp>程序自己如何去做这些操作？通常下载和替换文件并不难，只需要有一个接口能查询到新的程序版本和下载地址，很容易实现下载新版程序和替换。唯一需要注意一点的是重启程序这一步骤，这一步骤在业务程序进程上是无法实现的，因为它要拉起一个业务进程(worker)，并且退出自己，在它拉起业务进程的时候，如果自己本身就是业务进程(worker)，这样就会有两个业务进程(worker)，这对于大多数服务是不允许的，会出错。那么我们可以专门设计一个守护进程(watcher)仅用于程序的更新与重启。\u003C\u002Fp>\n\n\u003Cp>因此watcher进程里，更新程序和重启程序有如下流程\n\u003Cimg src=\"https:\u002F\u002Fimage.duguying.net\u002F2023\u002F10\u002Fhhy70tzc.webp\" alt=\"hhy70tzc\" \u002F>\u003C\u002Fp>\n\n\u003Cp>下载替换与杀旧进程(worker)放在一个线程中，另一个线程只负责监视worker进程是否存在，如果worker进程不存在，则拉起worker进程。\u003C\u002Fp>\n\n\u003Cp>同一个程序中，如何区分worker进程与watcher进程？我这里使用的是特定环境变量来区分的。终端中拉起程序，默认启动的是watcher进程，watcher进程启动后检测到worker进程不存在，那么它将会通过\u003Ccode class=\"inline-code\">exec.Command\u003C\u002Fcode>拉起进程，并且注入特定环境变量，如下\u003C\u002Fp>\n\n\u003Cpre style=\"color:#e6edf3;background-color:#0d1117;white-space:pre-wrap;word-break:break-word;\">\u003Ccode>\u003Cspan style=\"display:flex;\">\u003Cspan>cmd \u003Cspan style=\"color:#ff7b72;font-weight:bold\">:=\u003C\u002Fspan> exec.\u003Cspan style=\"color:#d2a8ff;font-weight:bold\">Command\u003C\u002Fspan>(procName, os.Args[\u003Cspan style=\"color:#a5d6ff\">1\u003C\u002Fspan>:]\u003Cspan style=\"color:#ff7b72;font-weight:bold\">...\u003C\u002Fspan>)\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>cmd.Env = append(os.\u003Cspan style=\"color:#d2a8ff;font-weight:bold\">Environ\u003C\u002Fspan>(), \u003Cspan style=\"color:#a5d6ff\">&#34;PROC_FLAG=worker&#34;\u003C\u002Fspan>)\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>cmd.Stdout = os.Stdout\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>cmd.Stderr = os.Stderr\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>\u003Cspan style=\"color:#ff7b72\">if\u003C\u002Fspan> e \u003Cspan style=\"color:#ff7b72;font-weight:bold\">:=\u003C\u002Fspan> cmd.\u003Cspan style=\"color:#d2a8ff;font-weight:bold\">Start\u003C\u002Fspan>(); \u003Cspan style=\"color:#79c0ff\">nil\u003C\u002Fspan> \u003Cspan style=\"color:#ff7b72;font-weight:bold\">!=\u003C\u002Fspan> e {\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>\tlog.\u003Cspan style=\"color:#d2a8ff;font-weight:bold\">Printf\u003C\u002Fspan>(\u003Cspan style=\"color:#a5d6ff\">&#34;ERROR: %v\\n&#34;\u003C\u002Fspan>, e)\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>\t\u003Cspan style=\"color:#ff7b72\">return\u003C\u002Fspan>\n\u003C\u002Fspan>\u003C\u002Fspan>\u003Cspan style=\"display:flex;\">\u003Cspan>}\n\u003C\u002Fspan>\u003C\u002Fspan>\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>而且程序启动的时候，首先也会判断该环境变量是否存在，如果存在该环境变量，那么就进入业务进程的业务逻辑。如果不存在该环境变量，则进入守护进程的守护逻辑。\u003C\u002Fp>\n\n\u003Cp>这样一来就能够实现程序自己更新自己了。\u003C\u002Fp>\n",{"id":18,"title":19,"uri":19,"author":10,"tags":20,"created_at":24,"view_count":25,"content":26},18,"windows服务注册工具nssm",[21,22,12,23],"nssm","windows","注册","2014-10-31 21:34:39",9054,"\u003Cp>在windows系统上注册一个服务通常你要这么做：\u003Cbr\u002F>\u003C\u002Fp>\u003Cp>首先，如果这个服务程序是你写的话，你必须在程序中编写ServiceMain服务主函数用来启动服务，并且要在程序中注册服务启动函数，函数原型如下。\u003Cbr\u002F>\u003C\u002Fp>\u003Cpre class=\"brush:cpp;toolbar:false\">VOID&nbsp;WINAPI&nbsp;ServiceMain(\n&nbsp;&nbsp;DWORD&nbsp;dwArgc,\n&nbsp;&nbsp;LPTSTR*&nbsp;lpszArgv\n);\u003C\u002Fpre>\u003Cp>然后，你需要修改注册表相关信息，在注册表项&quot;HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\&quot;中自动增加项[服务名]，并在其下成功添加各个键值：\u003Cbr\u002F>\u003C\u002Fp>\u003Cblockquote>&quot;DisplayName&quot; &nbsp; =服务显示名 &nbsp; &nbsp; (字符串值)\u003Cbr\u002F>\u003Cspan style=\"font-size: 12.5px;\">&quot;Description&quot; &nbsp; &nbsp; &nbsp;=服务描述 &nbsp; &nbsp; &nbsp; (字符串值)\u003Cbr\u002F>\u003C\u002Fspan>\u003Cspan style=\"font-size: 12.5px;\">&quot;ImagePath&quot; &nbsp; &nbsp; &nbsp; =应用程序路径 &nbsp; (可扩充字符串值)\u003Cbr\u002F>\u003C\u002Fspan>\u003Cspan style=\"font-size: 12.5px;\">&quot;ObjectName&quot; &nbsp; &nbsp;=&quot;LocalSystem&quot; &nbsp;(字符串值)\u003Cbr\u002F>\u003C\u002Fspan>\u003Cspan style=\"font-size: 12.5px;\">&quot;Type&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=10(16进制) &nbsp; &nbsp; (dword值)\u003Cbr\u002F>\u003C\u002Fspan>\u003Cspan style=\"font-size: 12.5px;\">&quot;Start&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=2(16进制) &nbsp; &nbsp; &nbsp;(dword值)\u003Cbr\u002F>\u003C\u002Fspan>\u003Cspan style=\"font-size: 12.5px;\">&quot;ErrorControl&quot; &nbsp; &nbsp; =1(16进制) &nbsp; &nbsp; &nbsp;(dword值)\u003Cbr\u002F>\u003C\u002Fspan>\u003C\u002Fblockquote>\u003Cp>或者直接执行\u003Ccode>instsrv [服务名] [应用程序路径]\u003C\u002Fcode>，自动添加注册表信息。(不过貌似只有服务器版的windows才有instsrv命令)\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size: 12.5px;\">完成上述操作后，如果不出意外你可以在服务管理器services.msc中可以看到你的服务。当然你如果想卸载服务，至少得把上面的第二步反过来做一遍，即清理注册表。\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size: 12.5px;\">现在有一个更好的工具可以完成这项任务，\u003Ca href=\"http:\u002F\u002Fnssm.cc\u002Fdownload\" target=\"_blank\" title=\"http:\u002F\u002Fnssm.cc\u002Fdownload\">nssm\u003C\u002Fa>。\u003C\u002Fspan>\u003C\u002Fp>\u003Cp>\u003Cspan style=\"font-size: 12.5px;\">nssm是一个命令行工具，也支持gui。\u003Cbr\u002F>\u003C\u002Fspan>\u003C\u002Fp>\u003Cpre class=\"brush:bat;toolbar:false\">nssm&nbsp;install&nbsp;&lt;ServerName&gt;&nbsp;&lt;Program&nbsp;Path&gt;&nbsp;&nbsp;#安装\nnssm&nbsp;remove&nbsp;&lt;ServerName&gt;&nbsp;confirm&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#卸载\u003C\u002Fpre>\u003Cp>\u003Cimg src=\"\u002F\u002Fduguying.oss-cn-qingdao.aliyuncs.com\u002F2014\u002F10\u002F31\u002Fnssm_usage_demo.png\" title=\"\" alt=\"nssm_usage_demo.png\"\u002F>\u003Cbr\u002F>\u003Cspan style=\"font-size: 12.5px;\">\u003C\u002Fspan>命令行使用\u003C\u002Fp>\u003Cp>\u003Cimg src=\"\u002F\u002Fduguying.oss-cn-qingdao.aliyuncs.com\u002F2014\u002F10\u002F31\u002Fnssm_gui_usage_install.png\" title=\"\" alt=\"nssm_gui_usage_install.png\"\u002F>\u003Cbr\u002F>GUI使用(安装)\u003C\u002Fp>\u003Cp>\u003Cimg src=\"\u002F\u002Fduguying.oss-cn-qingdao.aliyuncs.com\u002F2014\u002F10\u002F31\u002Fnssm_gui_usage_remove.png\" title=\"\" alt=\"nssm_gui_usage_remove.png\"\u002F>\u003Cbr\u002F>GUI使用(卸载)\u003C\u002Fp>\u003Cp>使用nssm来安装windows服务是不需要主程序中注册服务的，也就是说想将一个程序变为服务程序有了nssm后你无需修改代码。\u003C\u002Fp>",{"title":28,"list":29},"最热文章",[30,36,42,48,50,56,62,68,74,80],{"id":31,"title":32,"uri":33,"author":10,"created_at":34,"view_count":35},28,"docker容器目录与主机目录同步","\u002Farticle\u002Fdocker容器目录与主机目录同步","2014-12-31 12:34:08",15573,{"id":37,"title":38,"uri":39,"author":10,"created_at":40,"view_count":41},21,"beego中使用fis","\u002Farticle\u002Fbeego中使用fis","2014-12-03 10:47:56",9253,{"id":43,"title":44,"uri":45,"author":10,"created_at":46,"view_count":47},20,"用github登录，oauth开发","\u002Farticle\u002F用github登录，oauth开发","2014-11-23 13:33:37",9115,{"id":18,"title":19,"uri":49,"author":10,"created_at":24,"view_count":25},"\u002Farticle\u002Fwindows服务注册工具nssm",{"id":51,"title":52,"uri":53,"author":10,"created_at":54,"view_count":55},49,"用latex排版中文书籍","\u002Farticle\u002F用latex排版中文书籍","2016-10-31 11:12:02",8645,{"id":57,"title":58,"uri":59,"author":10,"created_at":60,"view_count":61},19,"texlive中文书签乱码","\u002Farticle\u002Ftexlive中文书签乱码","2014-11-15 02:54:01",8466,{"id":63,"title":64,"uri":65,"author":10,"created_at":66,"view_count":67},6,"Windows上的软件包管理器chocolatey","\u002Farticle\u002FWindows上的软件包管理器chocolatey","2014-08-10 15:55:22",8012,{"id":69,"title":70,"uri":71,"author":10,"created_at":72,"view_count":73},3,"利用开源和免费资源搭建CI环境","\u002Farticle\u002F利用开源和免费资源搭建CI环境","2014-07-04 16:54:58",7740,{"id":75,"title":76,"uri":77,"author":10,"created_at":78,"view_count":79},16,"php-fpm网站目录权限配置","\u002Farticle\u002Fphp-fpm网站目录权限配置","2014-10-18 01:36:10",7276,{"id":81,"title":82,"uri":83,"author":10,"created_at":84,"view_count":85},27,"sublime text 2插件开发，外部修改文件无法重新加载","\u002Farticle\u002Fsublime text 2插件开发，外部修改文件无法重新加载","2014-12-26 20:24:13",7055,{"title":87,"list":88},"按月归档",[89,93,96,99,102,105,108,112,116,119,122,125,129,132,135,139,142,145,148,152,155,158,161,164,167,170,173,176,179,182,185,188,191,194,197,200,203,206,209,212,216,219,222,225,228,231],{"title":90,"uri":91,"view_count":92},"2026年7月","\u002Farchive\u002F2026\u002F7\u002F1","[1]",{"title":94,"uri":95,"view_count":92},"2026年1月","\u002Farchive\u002F2026\u002F1\u002F1",{"title":97,"uri":98,"view_count":92},"2025年4月","\u002Farchive\u002F2025\u002F4\u002F1",{"title":100,"uri":101,"view_count":92},"2024年6月","\u002Farchive\u002F2024\u002F6\u002F1",{"title":103,"uri":104,"view_count":92},"2024年5月","\u002Farchive\u002F2024\u002F5\u002F1",{"title":106,"uri":107,"view_count":92},"2024年4月","\u002Farchive\u002F2024\u002F4\u002F1",{"title":109,"uri":110,"view_count":111},"2023年12月","\u002Farchive\u002F2023\u002F12\u002F1","[6]",{"title":113,"uri":114,"view_count":115},"2023年11月","\u002Farchive\u002F2023\u002F11\u002F1","[2]",{"title":117,"uri":118,"view_count":92},"2023年10月","\u002Farchive\u002F2023\u002F10\u002F1",{"title":120,"uri":121,"view_count":92},"2023年7月","\u002Farchive\u002F2023\u002F7\u002F1",{"title":123,"uri":124,"view_count":115},"2023年6月","\u002Farchive\u002F2023\u002F6\u002F1",{"title":126,"uri":127,"view_count":128},"2023年5月","\u002Farchive\u002F2023\u002F5\u002F1","[3]",{"title":130,"uri":131,"view_count":115},"2023年4月","\u002Farchive\u002F2023\u002F4\u002F1",{"title":133,"uri":134,"view_count":115},"2023年3月","\u002Farchive\u002F2023\u002F3\u002F1",{"title":136,"uri":137,"view_count":138},"2023年2月","\u002Farchive\u002F2023\u002F2\u002F1","[5]",{"title":140,"uri":141,"view_count":92},"2023年1月","\u002Farchive\u002F2023\u002F1\u002F1",{"title":143,"uri":144,"view_count":115},"2022年12月","\u002Farchive\u002F2022\u002F12\u002F1",{"title":146,"uri":147,"view_count":138},"2022年11月","\u002Farchive\u002F2022\u002F11\u002F1",{"title":149,"uri":150,"view_count":151},"2022年10月","\u002Farchive\u002F2022\u002F10\u002F1","[4]",{"title":153,"uri":154,"view_count":111},"2022年9月","\u002Farchive\u002F2022\u002F9\u002F1",{"title":156,"uri":157,"view_count":138},"2022年8月","\u002Farchive\u002F2022\u002F8\u002F1",{"title":159,"uri":160,"view_count":92},"2019年7月","\u002Farchive\u002F2019\u002F7\u002F1",{"title":162,"uri":163,"view_count":92},"2019年2月","\u002Farchive\u002F2019\u002F2\u002F1",{"title":165,"uri":166,"view_count":92},"2018年9月","\u002Farchive\u002F2018\u002F9\u002F1",{"title":168,"uri":169,"view_count":92},"2018年8月","\u002Farchive\u002F2018\u002F8\u002F1",{"title":171,"uri":172,"view_count":92},"2017年11月","\u002Farchive\u002F2017\u002F11\u002F1",{"title":174,"uri":175,"view_count":92},"2017年8月","\u002Farchive\u002F2017\u002F8\u002F1",{"title":177,"uri":178,"view_count":92},"2017年5月","\u002Farchive\u002F2017\u002F5\u002F1",{"title":180,"uri":181,"view_count":92},"2017年4月","\u002Farchive\u002F2017\u002F4\u002F1",{"title":183,"uri":184,"view_count":92},"2016年11月","\u002Farchive\u002F2016\u002F11\u002F1",{"title":186,"uri":187,"view_count":92},"2016年10月","\u002Farchive\u002F2016\u002F10\u002F1",{"title":189,"uri":190,"view_count":115},"2016年9月","\u002Farchive\u002F2016\u002F9\u002F1",{"title":192,"uri":193,"view_count":92},"2016年8月","\u002Farchive\u002F2016\u002F8\u002F1",{"title":195,"uri":196,"view_count":115},"2016年7月","\u002Farchive\u002F2016\u002F7\u002F1",{"title":198,"uri":199,"view_count":138},"2016年6月","\u002Farchive\u002F2016\u002F6\u002F1",{"title":201,"uri":202,"view_count":92},"2015年12月","\u002Farchive\u002F2015\u002F12\u002F1",{"title":204,"uri":205,"view_count":92},"2015年10月","\u002Farchive\u002F2015\u002F10\u002F1",{"title":207,"uri":208,"view_count":115},"2015年2月","\u002Farchive\u002F2015\u002F2\u002F1",{"title":210,"uri":211,"view_count":92},"2015年1月","\u002Farchive\u002F2015\u002F1\u002F1",{"title":213,"uri":214,"view_count":215},"2014年12月","\u002Farchive\u002F2014\u002F12\u002F1","[8]",{"title":217,"uri":218,"view_count":115},"2014年11月","\u002Farchive\u002F2014\u002F11\u002F1",{"title":220,"uri":221,"view_count":138},"2014年10月","\u002Farchive\u002F2014\u002F10\u002F1",{"title":223,"uri":224,"view_count":128},"2014年9月","\u002Farchive\u002F2014\u002F9\u002F1",{"title":226,"uri":227,"view_count":128},"2014年8月","\u002Farchive\u002F2014\u002F8\u002F1",{"title":229,"uri":230,"view_count":128},"2014年7月","\u002Farchive\u002F2014\u002F7\u002F1",{"title":232,"uri":233,"view_count":115},"2013年9月","\u002Farchive\u002F2013\u002F9\u002F1",2,{"url":236,"title":237},"https:\u002F\u002Fimage.duguying.net\u002F2022\u002F09\u002Fij6x8o4s.webp","老家 摄于2020年秋",1790781592172]