doupian9798
2019-03-24 07:22
浏览 102
已采纳

屏幕锁定/关闭时,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")

图片转代码服务由CSDN问答提供 功能建议

我正在Macos上运行golang应用程序。 它具有以下类似的代码:

  for {
 time.Sleep(time.Second * 5)
 cmd:= exec.Command(“ / usr / bin  / osascript“,”-e“,`显示对话框” hello“,标题为” hello“`)
 err:= cmd.Run()
} 
   
 
 <  p>如果我不锁定屏幕(屏幕始终处于打开状态),它会很好地工作。 但是如果屏幕被锁定并在执行该行时关闭屏幕,则代码 err:= cmd.Run()将永远挂起。 当我解锁屏幕(打开它)时, for 循环只会永远挂在那里,永远不会继续执行。   
 
 

我不确定此问题是否属于golang或MacOS如何处理osascript。 谁能告诉我如何解决呢? 非常感谢。

PS:我在Linux中使用相同的代码,并将 / usr / bin / osascript 替换为 / usr / bin / xmessage < / code>,即使在Linux中锁定/关闭屏幕,它也可以正常工作而不会出现任何问题。

编辑:

我的解决方案,改用chrome:

 <  code> cmd:= exec.Command(`/ Applications / Google Chrome.app/Contents/MacOS/Google Chrome`,“ -new-window”,“ /path/hello.html")
 
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dou12754 2019-03-24 07:48
    已采纳

    It seems to be related to how MacOS puts processes in idle when the screen locks. It makes the osasscriptchild process never finish execution and block the for loop.

    One thing you can do is run the command with a timeout context. I have tried and it works. Execution will resume when the screen is unlocked and timeout expires.

    Example:

    package main
    
    import (
        "context"
        "fmt"
        "os/exec"
        "time"
    )
    
    func main() {
        for {
            time.Sleep(time.Second * 5)
    
            // run your command with a timeout
            ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
            cmd := exec.CommandContext(
                ctx,
                "/usr/bin/osascript",
                "-e",
                `display dialog "hello" with title "hello"`,
            )
    
            err := cmd.Run()
            if err != nil {
                fmt.Println(err)
            }
            // don't forget to cancel your context to avoid context leak
            cancel()
        }
    }
    

    Alternatively, if you don't want a timeout you can check if the screen is locked before trying to call display dialog.

    package main
    
    import (
        "context"
        "fmt"
        "os"
        "os/exec"
        "strings"
        "time"
    )
    
    func main() {
        for {
            time.Sleep(time.Second * 5)
    
            ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
            cmd := exec.CommandContext(
                ctx,
                "python",
                "-c",
                "import sys,Quartz; d=Quartz.CGSessionCopyCurrentDictionary(); print d",
            )
    
            var err error
            var b []byte
            if b, err = cmd.CombinedOutput(); err != nil {
                cancel()
                continue
            }
            cancel()
    
            // if screen is not locked
            if !strings.Contains(string(b), "CGSSessionScreenIsLocked = 1") {
                cmd = exec.Command(
                    "/usr/bin/osascript",
                    "-e",
                    "display dialog \"Hello\"",
                )
                cmd.Stdout = os.Stdout
                cmd.Stderr = os.Stderr
    
                err = cmd.Run()
                if err != nil {
                    fmt.Println("err: ", err)
                }
            }
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题