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

  • Home
  • SEARCH
  • 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 8873191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:24:12+00:00 2026-06-14T18:24:12+00:00

given the following example: // test.go package main import ( fmt os/exec ) func

  • 0

given the following example:

// test.go
package main

import (
        "fmt"
        "os/exec"
)

func main() {
    cmd := exec.Command("login")
    in, _ := cmd.StdinPipe()
    in.Write([]byte("user"))
    out, err := cmd.CombinedOutput()
    if err != nil {
         fmt.Println("error:", err)
        }
    fmt.Printf("%s", out)
}

How can I detect that the process is not going to finish, because it is waiting for user input?

I’m trying to be able to run any script, but abort it if for some reason it tries to read from stdin.

Thanks!

  • 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-14T18:24:13+00:00Added an answer on June 14, 2026 at 6:24 pm

    Detecting that the process is not going to finish is a difficult problem. In fact, it is one of the classic “unsolvable” problems in Computer Science: the Halting Problem.

    In general, when you are calling exec.Command and will not be passing it any input, it will cause the program to read from your OS’s null device (see documentation in the exec.Cmd fields). In your code (and mine below), you explicitly create a pipe (though you should check the error return of StdinPipe in case it is not created correctly), so you should subsequently call in.Close(). In either case, the subprocess will get an EOF and should clean up after itself and exit.

    To help with processes that don’t handle input correctly or otherwise get themselves stuck, the general solution is to use a timeout. In Go, you can use goroutines for this:

    // Set your timeout
    const CommandTimeout = 5 * time.Second
    
    func main() {
      cmd := exec.Command("login")
    
      // Set up the input
      in, err := cmd.StdinPipe()
      if err != nil {
        log.Fatalf("failed to create pipe for STDIN: %s", err)
      }
    
      // Write the input and close
      go func() {
        defer in.Close()
        fmt.Fprintln(in, "user")
      }()
    
      // Capture the output
      var b bytes.Buffer
      cmd.Stdout, cmd.Stderr = &b, &b
    
      // Start the process
      if err := cmd.Start(); err != nil {
        log.Fatalf("failed to start command: %s", err)
      }
    
      // Kill the process if it doesn't exit in time
      defer time.AfterFunc(CommandTimeout, func() {
        log.Printf("command timed out")
        cmd.Process.Kill()
      }).Stop()
    
      // Wait for the process to finish
      if err := cmd.Wait(); err != nil {
        log.Fatalf("command failed: %s", err)
      }
    
      // Print out the output
      fmt.Printf("Output:\n%s", b.String())
    }
    

    In the code above, there are actually three main goroutines of interest: the main goroutine spawns the subprocess and waits for it to exit; a timer goroutine is sent off in the background to kill the process if it’s not Stopped in time; and a goroutine that writes the output to the program when it’s ready to read it.

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

Sidebar

Related Questions

Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
Given that i have the following url in my site: http://localhost/test/example.php?a=1&b=2 How can i
Given the following Java which is loaded into the database using loadjava: package grassie.example;
Given the following example layout: test/ test.py formats/ __init__.py format_a.py format_b.py What I try
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
I want to Make selenium script which move slider given on following site Example
I was following the example given in the OpenCV for video displaying, just took
In the following contrived example, data is given a warning due to it being

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.