第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Golang中終止以os / exec開頭的進(jìn)程

在Golang中終止以os / exec開頭的進(jìn)程

Go
藍(lán)山帝景 2021-04-27 21:26:25
有沒有辦法終止Golang中以os.exec開始的進(jìn)程?例如(來自http://golang.org/pkg/os/exec/#example_Cmd_Start),cmd := exec.Command("sleep", "5")err := cmd.Start()if err != nil {    log.Fatal(err)}log.Printf("Waiting for command to finish...")err = cmd.Wait()log.Printf("Command finished with error: %v", err)是否有辦法提前(可能在3秒后)終止該過程?
查看完整描述

3 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗 獲得超13個贊

終止運(yùn)行exec.Process:


// Start a process:

cmd := exec.Command("sleep", "5")

if err := cmd.Start(); err != nil {

    log.Fatal(err)

}


// Kill it:

if err := cmd.Process.Kill(); err != nil {

    log.Fatal("failed to kill process: ", err)

}

exec.Process超時后終止運(yùn)行:


// Start a process:

cmd := exec.Command("sleep", "5")

if err := cmd.Start(); err != nil {

    log.Fatal(err)

}


// Wait for the process to finish or kill it after a timeout (whichever happens first):

done := make(chan error, 1)

go func() {

    done <- cmd.Wait()

}()

select {

case <-time.After(3 * time.Second):

    if err := cmd.Process.Kill(); err != nil {

        log.Fatal("failed to kill process: ", err)

    }

    log.Println("process killed as timeout reached")

case err := <-done:

    if err != nil {

        log.Fatalf("process finished with error = %v", err)

    }

    log.Print("process finished successfully")

}

該過程結(jié)束并且在done3秒鐘內(nèi)收到了錯誤(如果有的話),并且該程序在完成之前被終止了。


查看完整回答
反對 回復(fù) 2021-05-17
  • 3 回答
  • 0 關(guān)注
  • 291 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號