Last active
January 9, 2025 15:31
-
-
Save lee8oi/ec404fa99ea0f6efd9d1 to your computer and use it in GitHub Desktop.
Using os.StartProcess() in Go for platform-independent system command execution.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"os/exec" | |
) | |
func Start(args ...string) (p *os.Process, err error) { | |
if args[0], err = exec.LookPath(args[0]); err == nil { | |
var procAttr os.ProcAttr | |
procAttr.Files = []*os.File{os.Stdin, | |
os.Stdout, os.Stderr} | |
p, err := os.StartProcess(args[0], args, &procAttr) | |
if err == nil { | |
return p, nil | |
} | |
} | |
return nil, err | |
} | |
func main() { | |
if proc, err := Start("ping", "-c 3", "www.google.com"); err == nil { | |
proc.Wait() | |
} | |
if proc, err := Start("zsh"); err == nil { | |
proc.Wait() | |
} | |
} |
Hi, thank you for sharing.
Do you know how i can capture the output stream from the StartProcess command in realtime?
It's been years since I've played with all this. Gotta catch back up. Browsing the package documentation might provide some insight. Maybe ask your AI for some guidance? If I catch back up and figure anything out I'll try to update my response here.
…-------- Original Message --------
On 9/10/24 07:50, rsvix wrote:
@rsvix commented on this gist.
---------------------------------------------------------------
Hi, thank you for sharing.
Do you know how i can capture the output stream from the StartProcess command in realtime?
—
Reply to this email directly, [view it on GitHub](https://gist.github.com/lee8oi/ec404fa99ea0f6efd9d1#gistcomment-5184743) or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AAGWUWZPS46IT7R3GWCFIETZV3TKNBFKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DFVRZXKYTKMVRXIX3UPFYGLK2HNFZXIQ3PNVWWK3TUUZ2G64DJMNZZDAVEOR4XAZNEM5UXG5FFOZQWY5LFVAZDANJTGA4TCMFHORZGSZ3HMVZKMY3SMVQXIZI).
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for [iOS](https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675) or [Android](https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub).
Thank you @lee8oi
I've tried using io pipes, MultiReader and bufio.NewScanner, but with no luck
For anyone reading, im trying to do something like this, but using os.StartProcess instead of exec.Command
multi := io.MultiReader(stdout, stderr)
if err := cmd.Start(); err != nil {
return err
}
in := bufio.NewScanner(multi)
for in.Scan() {
log.Printf(in.Text()) // write each line to the log or anything else
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
os/Exec is a higher level implementation of the os.Process, with os.StartProcess, you can get the ProcessState with which you can get more control over the process than os/exec. For example, I had to run a commandline process which locks and when it crashes golang hangs until the lock is removed, but with processState.Release option I was able to release the process resources thereby allowing my code to run to completion. I could have used the os/exec.Start and os/exec.Wait() but it did not work for me.