Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8349515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:01:16+00:00 2026-06-09T08:01:16+00:00

Is there a way to terminate a process started with os.exec in Golang? For

  • 0

Is there a way to terminate a process started with os.exec in Golang? For example (from 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)

Is there a way to terminate that process ahead of time, perhaps after 3 seconds?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:01:18+00:00Added an answer on June 9, 2026 at 8:01 am

    Run and terminate an 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)
    }
    

    Run and terminate an exec.Process after a timeout:

    ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second)
    defer cancel()
    
    if err := exec.CommandContext(ctx, "sleep", "5").Run(); err != nil {
        // This will fail after 3 seconds. The 5 second sleep
        // will be interrupted.
    }
    

    See this example in the Go docs


    Legacy

    Before Go 1.7, we didn’t have the context package and this answer was different.

    Run and terminate an exec.Process after a timeout using channels and a goroutine:

    // 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")
    }
    

    Either the process ends and its error (if any) is received through done or 3 seconds have passed and the program is killed before it’s finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to terminate a phone call from an iOS app? The
Is there a way to launch a gnome-terminal from the command line (i.e., using
Is there way to get file from windows xp command prompt? I tried to
Is there a way to start a process using ssh that doesn't terminate when
Is there any way to terminate ajax execution, based on beforeSend result ? $.ajaxSetup({
is there some way to terminate all (Java) applications launched with Eclipse at once?
Is there any way to stop or terminate long running Oracle query in JDBC
The proccess I've started has a different terminal id from tty result. Is there
If a user executes foo.exe on his computer, is there a way to terminate
Is there a way to detect, from within the finally clause, that an exception

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.