restart or shutdown golang apps programmatically

huangapple go评论103阅读模式
英文:

restart or shutdown golang apps programmatically

问题

所以我需要编写一个程序,可以通过编程方式运行、重新启动或关闭。我需要从终端执行以下操作:

  1. project> go run app.go
  2. 应用已启动,等待5秒...
  3. project>

另一种情况:

  1. project> go run app.go runner.go
  2. app.go启动应用
  3. runner.go检测到app.go
  4. 应用在10秒后从app.go重新启动

程序将类似于以下内容:

  1. package main
  2. import (
  3. "something"
  4. )
  5. func main() {
  6. something.KillPrograms({
  7. doingWhatever() //这里是我的程序
  8. }, 5000) //程序将仅运行5秒钟
  9. }

有没有什么库或者Go语言的东西可以做到这样的效果?谢谢。

英文:

so i need to make a program than can run, then restart or shutdown programmatically. I need to do like this from terminal:

  1. project> go run app.go
  2. apps started, waiting for 5 sec...
  3. project>

other scenario:

  1. project> go run app.go runner.go
  2. apps started from app.go
  3. runner detecting app.go from runner.go
  4. app restarted after 10 sec from app.go

The program will be something like below:

  1. package main
  2. import(
  3. "something"
  4. )
  5. func main () {
  6. something.KillPrograms({
  7. doingWhatever() //my program here
  8. }, 5000) //program will running for only 5 sec
  9. }

any library or anything from golang can do like that? thanks.

答案1

得分: 2

你可以尝试使用time.Sleep

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. // 使用goroutine将函数单独运行
  8. // 如果你想要同步执行,可以移除`go`
  9. go 做一些事情()
  10. // 程序将在5秒后退出
  11. // 即使你的函数还没有执行完毕
  12. time.Sleep(5 * time.Second)
  13. os.Exit(0)
  14. }

请注意,这是一个示例代码,其中的做一些事情()函数需要根据你的实际需求进行定义和实现。

英文:

You can try with time.Sleep

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. //use goroutine to run your function separately
  8. //if you want it's sync, you can remove `go`
  9. go doingWhatever()
  10. //after 5 seconds, the program will exit
  11. //even though your function has not been done yet
  12. time.Sleep(5 * time.Second)
  13. os.Exit(0)
  14. }

答案2

得分: 1

许多库可以处理程序的重启和监视。查看这个答案或者这个

我刚刚尝试了这段简单的代码,它会每2秒重新启动自己,并在此期间运行一个随机检查来关闭程序。

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "os"
  6. "os/exec"
  7. "runtime"
  8. "sync"
  9. "syscall"
  10. "time"
  11. )
  12. var wg sync.WaitGroup
  13. func main() {
  14. t1 := time.NewTimer(time.Second * 2)
  15. wg.Add(1)
  16. // 你可以通过编程方式处理重启
  17. go func() {
  18. defer wg.Done()
  19. <-t1.C
  20. fmt.Println("Timer expired")
  21. RestartSelf()
  22. }()
  23. fmt.Println(time.Now().Format("2006-Jan-02 ( 15:04:05)"))
  24. rand.Seed(time.Now().UnixNano())
  25. // 你可以通过编程方式处理关闭
  26. if rand.Intn(3) == 1 {
  27. fmt.Println("It is time to shut-down")
  28. os.Exit(0)
  29. }
  30. wg.Wait()
  31. }
  32. func RestartSelf() error {
  33. self, err := os.Executable()
  34. if err != nil {
  35. return err
  36. }
  37. args := os.Args
  38. env := os.Environ()
  39. // Windows 不支持 exec 系统调用。
  40. if runtime.GOOS == "windows" {
  41. cmd := exec.Command(self, args[1:]...)
  42. cmd.Stdout = os.Stdout
  43. cmd.Stderr = os.Stderr
  44. cmd.Stdin = os.Stdin
  45. cmd.Env = env
  46. err := cmd.Run()
  47. if err == nil {
  48. os.Exit(0)
  49. }
  50. return err
  51. }
  52. return syscall.Exec(self, args, env)
  53. }

输出结果:

  1. 2022-Mar-10 ( 17:59:53)
  2. Timer expired
  3. 2022-Mar-10 ( 17:59:55)
  4. Timer expired
  5. 2022-Mar-10 ( 17:59:57)
  6. It is time to shut-down
  7. 进程以退出码 0 结束
英文:

Many libraries can handle restart/watch of the program. check this answer or this one.

I just tried this simple code that will restart itself each 2 seconds and during that he will run a random check to shutdown.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;math/rand&quot;
  5. &quot;os&quot;
  6. &quot;os/exec&quot;
  7. &quot;runtime&quot;
  8. &quot;sync&quot;
  9. &quot;syscall&quot;
  10. &quot;time&quot;
  11. )
  12. var wg sync.WaitGroup
  13. func main() {
  14. t1 := time.NewTimer(time.Second * 2)
  15. wg.Add(1)
  16. // you can handle to restart programmatically
  17. go func() {
  18. defer wg.Done()
  19. &lt;-t1.C
  20. fmt.Println(&quot;Timer expired&quot;)
  21. RestartSelf()
  22. }()
  23. fmt.Println(time.Now().Format(&quot;2006-Jan-02 ( 15:04:05)&quot;))
  24. rand.Seed(time.Now().UnixNano())
  25. // you can handle the shut-down programmatically
  26. if rand.Intn(3) == 1 {
  27. fmt.Println(&quot;It is time to shut-down&quot;)
  28. os.Exit(0)
  29. }
  30. wg.Wait()
  31. }
  32. func RestartSelf() error {
  33. self, err := os.Executable()
  34. if err != nil {
  35. return err
  36. }
  37. args := os.Args
  38. env := os.Environ()
  39. // Windows does not support exec syscall.
  40. if runtime.GOOS == &quot;windows&quot; {
  41. cmd := exec.Command(self, args[1:]...)
  42. cmd.Stdout = os.Stdout
  43. cmd.Stderr = os.Stderr
  44. cmd.Stdin = os.Stdin
  45. cmd.Env = env
  46. err := cmd.Run()
  47. if err == nil {
  48. os.Exit(0)
  49. }
  50. return err
  51. }
  52. return syscall.Exec(self, args, env)
  53. }

output

  1. 2022-Mar-10 ( 17:59:53)
  2. Timer expired
  3. 2022-Mar-10 ( 17:59:55)
  4. Timer expired
  5. 2022-Mar-10 ( 17:59:57)
  6. It is time to shut-down
  7. Process finished with the exit code 0

huangapple
  • 本文由 发表于 2022年3月10日 11:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/71418671.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定