身為 PHP 開發老兵。使用過命令列對 nginx、PHP-FPM 進行啟動/重啟/停止等操作。印象非常深刻。讓我用 C/C 開發這樣的系統肯定是沒精力搞了。然而,自從 Golang 進入了我的視野之後。我發現這一切都變得非常的容易。
1)產生守護程式
直接上程式碼:
package main import ( "os" "os/exec" "path/filepath" ) func main() { //判 断当其是否是子进程,当父进程return之后,子进程会被 系统1 号进程接管 if os.Getppid() != 1 { // 将命令行参数中执行文件路径转换成可用路径 filePath, _ := filepath.Abs(os.Args[0]) cmd := exec.Command(filePath, os.Args[1:]...) // 将其他命令传入生成出的进程 cmd.Stdin = os.Stdin // 给新进程设置文件描述符,可以重定向到文件中 cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Start() // 开始执行新进程,不等待新进程退出 return } }
對Linux 系統熟悉的人應該知道:使用者建立的守護程式會被Linux 系統的1 號程式接管。換句話說,上面的程式碼只能在 Linux 系統運作。 Unix 系統我沒玩過。所以,也不能給具體的建議。
我在網路上看到還有其他的方法實作守護程式的建立。但是,我覺得只有上面原始碼的方式我覺得不錯。並且成功用於專案當中。
例如:
os.StartProcess() 创建守护进程。 syscall.RawSyscall() 创建守护进程。
唯獨 exec.Command
建立守護程式的方式最高級。封裝得最好。推薦使用這種試。
2) 守護程式啟動/重新啟動/停止
在第 1 點當中,我們已經成功啟動了一個守護程式。但是,我們不可能使用 kill 指令去結束它。然後,再啟動吧。所以,我們要用業界專業的手法:訊號。
任何進程在運行中都能接收到我們發送給它的訊號。關於 Linux 的訊號有很多。大家可以自己 Google 搜尋關鍵字:Linux 訊號。
直接上原始碼:
package main import "fmt" import "os" import "os/signal" import "syscall" func main() { // Go signal notification works by sending `os.Signal` // values on a channel. We'll create a channel to // receive these notifications (we'll also make one to // notify us when the program can exit). sigs := make(chan os.Signal, 1) done := make(chan bool, 1) // `signal.Notify` registers the given channel to // receive notifications of the specified signals. signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) // This goroutine executes a blocking receive for // signals. When it gets one it'll print it out // and then notify the program that it can finish. go func() { sig := <p>有三個關鍵點:<br>1)註冊訊號<br>2)接收訊號<br>3)處理訊號。 </p><p>只要把創建守護程式與信號量處理整合一起,就能實現指令去管理守護程式了。 </p><p>更多golang相關技術文章,請造訪<a href="https://www.php.cn/be/go/" target="_blank"><strong>golang</strong></a>教學欄位! </p>
以上是解析Golang怎麼創建守護程式和平滑重啟的詳細內容。更多資訊請關注PHP中文網其他相關文章!

whentestinggocodewithinitfunctions,useexplicitseTupfunctionsorseParateTestFileSteSteTepteTementDippedDependendendencyOnInItfunctionsIdeFunctionSideFunctionsEffect.1)useexplicitsetupfunctionStocontrolglobalvaribalization.2)createSepEpontrolglobalvarialization

go'serrorhandlingurturnserrorsasvalues,與Javaandpythonwhichuseexceptions.1)go'smethodensursexplitirorhanderling,propertingrobustcodebutincreasingverbosity.2)

AnefactiveInterfaceingoisminimal,clear and promotesloosecoupling.1)minimizeTheInterfaceForflexibility andeaseofimplementation.2)useInterInterfaceForabStractionToswaPimplementations withoutchangingCallingCode.3)

集中式錯誤處理在Go語言中可以提升代碼的可讀性和可維護性。其實現方式和優勢包括:1.將錯誤處理邏輯從業務邏輯中分離,簡化代碼。 2.通過集中處理錯誤,確保錯誤處理的一致性。 3.使用defer和recover來捕獲和處理panic,增強程序健壯性。

Ingo,替代詞InivestoIniTfunctionsIncludeCustomInitializationfunctionsandsingletons.1)customInitializationfunctions hownerexpliticpliticpliticconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconconcontirization curssetupssetupssetups.2)單次固定無元素限制ininconconcurrent

Gohandlesinterfacesandtypeassertionseffectively,enhancingcodeflexibilityandrobustness.1)Typeassertionsallowruntimetypechecking,asseenwiththeShapeinterfaceandCircletype.2)Typeswitcheshandlemultipletypesefficiently,usefulforvariousshapesimplementingthe

Go語言的錯誤處理通過errors.Is和errors.As函數變得更加靈活和可讀。 1.errors.Is用於檢查錯誤是否與指定錯誤相同,適用於錯誤鏈的處理。 2.errors.As不僅能檢查錯誤類型,還能將錯誤轉換為具體類型,方便提取錯誤信息。使用這些函數可以簡化錯誤處理邏輯,但需注意錯誤鏈的正確傳遞和避免過度依賴以防代碼複雜化。

tomakegoapplicationsRunfasterandMorefly,useProflingTools,leverageConCurrency,andManageMoryfectily.1)usepprofforcpuorforcpuandmemoryproflingtoidentifybottlenecks.2)upitizegorizegoroutizegoroutinesandchannelstoparalletaparelalyizetasksandimproverperformance.3)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境