2016-11-01 163 views
0

我有一个启动代理,只要连接了USB设备就运行bash脚本。我希望它只运行一次,但是当我连接设备时,脚本每10秒继续运行一次。启动代理每10秒运行一次脚本

这里是plist中:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd > 
<plist version="1.0"> 
<dict> 
    <key>Label</key> 
    <string>com.example.program</string> 
    <key>Program</key> 
    <string>/Users/Peter/Desktop/test1.sh</string> 
    <key>LaunchEvents</key> 
    <dict> 
     <key>com.apple.iokit.matching</key> 
     <dict> 
      <key>com.apple.device-attach</key> 
      <dict> 
       <key>idProduct</key> 
       <integer>1476</integer> 
       <key>idVendor</key> 
       <integer>1356</integer> 
       <key>IOProviderClass</key> 
       <string>IOUSBDevice</string> 
       <key>IOMatchLaunchStream</key> 
       <true/> 
      </dict> 
     </dict> 
    </dict> 
</dict> 
</plist> 

下面是脚本:

#!/bin/bash 
open -a "Spotify"; 
sleep 11; 

我加了睡眠11,因为我读了剧本需要运行至少10秒钟,让launchd的思考它完成了它的任务。然而,这并没有帮助。

在运行launchctl list com.example.program终端给出:

"LimitLoadToSessionType" = "Aqua"; 
"Label" = "com.example.program"; 
"TimeOut" = 30; 
"OnDemand" = true; 
"LastExitStatus" = 0; 
"Program" = "/Users/Peter/Desktop/test1.sh"; 

回答

0

默认情况下,launchd会启动程序时,他们推出的条件得到满足,然后重新启动他们,如果他们退出或崩溃。为防止它重新启动脚本,只需将<key>KeepAlive</key><false/>添加到.plist中(然后卸载并重新加载它)。可能还需要添加以阻止杀死“剩余”Spotify进程的启动(尽管我认为它将在没有此功能的情况下运行)。

+0

不幸的是,添加这些键并没有什么区别。有趣的是,当我在终端中运行launchctl list com.example.program时,我没有看到这些新键。这是正常的吗? –

+0

@JohnSmith这听起来像你需要重新加载.plist。尝试'launchctl unload/path/to/com.example.program.plist',然后'launchctl unload/path/to/com.example.program.plist'。 (注意:*不要*使用'sudo';这会把它当作一个守护进程,而不是一个代理。) –

+0

我重新加载它。它不起作用。 –