屏幕锁定/关闭时,cmd.Run()会永久悬挂在Macos的golang中

I am running a golang app on Macos. It has some codes like the following:

for {
    time.Sleep(time.Second * 5)
    cmd := exec.Command("/usr/bin/osascript", "-e", `display dialog "hello" with title "hello"`)
    err := cmd.Run()
}

It works fine if I don't lock the screen (when the screen is always on). But the code err := cmd.Run() will hang forever if the screen is locked and off when that line executes. When I unlock the screen (turn it on), the for loop just hangs there forever and will never continue its execution.

I am not sure if this issue belongs to golang or how MacOS handles osascript. Can anyone please tell me how to workaround it? Thanks a lot.

PS: I use the same code in Linux and replace /usr/bin/osascript to /usr/bin/xmessage and this always works fine without any problems even if the screen is locked/off in Linux.

Edited:

My solution, use chrome instead:

cmd := exec.Command(`/Applications/Google Chrome.app/Contents/MacOS/Google Chrome`, "-new-window", "/path/hello.html")
查看全部
doupian9798
doupian9798
2019/03/24 07:22
  • macos
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复