script icon indicating copy to clipboard operation
script copied to clipboard

How can I check the progress of a script.Exec() command?

Open aaronpkelly opened this issue 2 years ago • 2 comments

I have a long running Exec() query:

script.Exec("/home/aaron/big-script.sh")

This command takes a long time, but is always successful.

How can I check the progress of this Exec query? How do I know if it's still in-progress, or has finished running it's command?

I don't think I can check the exit status of the Pipe (as far as I can see), because that will only be set if there's an error

aaronpkelly avatar May 20 '23 11:05 aaronpkelly

You can use Wait to wait for the pipe to finish:

script.Exec("/home/aaron/big-script.sh").Wait()

Alternatively, if the script produces output, you can copy it to the terminal while waiting:

script.Exec("/home/aaron/big-script.sh").Stdout()

bitfield avatar May 20 '23 11:05 bitfield

One of the things I like to do when waiting for a long time is let the users know. -- hey it's gonna take a few. then I give it a spinner. This gives the impression that the computer or process didn't lockup -- that it's working on it. There are many different spinner functions, but test first not all of them work well with the user's terminal such as putty.

quit:=make(chan bool)
fmt.Println("going to run big-script.sh This is going to take a while.")
go spinner(quit)
script.Exec("big-script.sh").Stdout()
quit <- true

Spinner Function:

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/artyom/spinner"
)

func spin(quit chan bool) {
	fmt.Println("")
	spnr := spinner.New(os.Stdout, "  Working: ")
	for {
		select {
		case <-quit:
			spnr.Clear()
			return
		default:
			spnr.Spin()
		}
		time.Sleep(250 * time.Millisecond)
	}
}

rmasci avatar Aug 04 '23 01:08 rmasci