您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Golang怎么創(chuàng)建守護(hù)進(jìn)程和平滑重啟”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
直接上代碼:
package main import ( "os" "os/exec" "path/filepath" ) func main() { //判 斷當(dāng)其是否是子進(jìn)程,當(dāng)父進(jìn)程return之后,子進(jìn)程會(huì)被 系統(tǒng)1 號(hào)進(jìn)程接管 if os.Getppid() != 1 { // 將命令行參數(shù)中執(zhí)行文件路徑轉(zhuǎn)換成可用路徑 filePath, _ := filepath.Abs(os.Args[0]) cmd := exec.Command(filePath, os.Args[1:]...) // 將其他命令傳入生成出的進(jìn)程 cmd.Stdin = os.Stdin // 給新進(jìn)程設(shè)置文件描述符,可以重定向到文件中 cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Start() // 開(kāi)始執(zhí)行新進(jìn)程,不等待新進(jìn)程退出 return } }
對(duì) Linux 系統(tǒng)熟悉的人應(yīng)該知道:用戶(hù)創(chuàng)建的守護(hù)進(jìn)程會(huì)被 Linux 系統(tǒng)的 1 號(hào)進(jìn)程接管。換句話(huà)說(shuō),上面的代碼只能在 Linux 系統(tǒng)運(yùn)行。Unix 系統(tǒng)我沒(méi)有玩過(guò)。所以,也不能給出具體的建議。
我在網(wǎng)上看到還有其他的方法實(shí)現(xiàn)守護(hù)進(jìn)程的創(chuàng)建。但是,我覺(jué)得只有上面源碼的方式我覺(jué)得不錯(cuò)。并且成功用于項(xiàng)目當(dāng)中。
比如:
os.StartProcess() 創(chuàng)建守護(hù)進(jìn)程。 syscall.RawSyscall() 創(chuàng)建守護(hù)進(jìn)程。
唯獨(dú) exec.Command
創(chuàng)建守護(hù)進(jìn)程的方式最高級(jí)。封裝得最好。推薦使用這種試。
在第 1 點(diǎn)當(dāng)中,我們已經(jīng)成功啟動(dòng)了一個(gè)守護(hù)進(jìn)程。但是,我們不可能使用 kill 命令去結(jié)束它。然后,再啟動(dòng)吧。所以,我們要用業(yè)界專(zhuān)業(yè)的手法:信號(hào)。
任何進(jìn)程在運(yùn)行中都能接收到我們發(fā)送給它的信號(hào)。關(guān)于 Linux 的信號(hào)有很多。大家可以自己 Google 搜索關(guān)鍵詞:Linux 信號(hào)。
直接上源碼:
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 := <-sigs fmt.Println() fmt.Println(sig) done <- true }() // The program will wait here until it gets the // expected signal (as indicated by the goroutine // above sending a value on `done`) and then exit. fmt.Println("awaiting signal") <-done fmt.Println("exiting") }
有三個(gè)關(guān)鍵點(diǎn):
1)注冊(cè)信號(hào)
2)接收信號(hào)
3)處理信號(hào)。
只要把創(chuàng)建守護(hù)進(jìn)程與信號(hào)量處理整合一起,就能實(shí)現(xiàn)命令去管理守護(hù)進(jìn)程了。
“Golang怎么創(chuàng)建守護(hù)進(jìn)程和平滑重啟”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。