此launchd守护程序是一个系统守护程序(不是用户代理程序),旨在从睡眠状态唤醒时运行脚本文件
安装代码:
#!/bin/sh
#find current working directory. store as $curr. use to reference anything in $curr/mysecureview.
curr=$(pwd)
echo "+copy the plist to the system daemons directory."
cp $curr/sleepwatcher/config/sleepwatcher.system.plist /System/Library/LaunchDaemons/sleepwatcher.system.plist
echo "+create the /etc/mysecureview directory to contain all program files."
sudo mkdir /etc/mysecureview
echo "+copy the log file to contain the compiled set of log entries."
sudo cp $curr/log.txt /etc/mysecureview/log.txt
echo "+create the file to contain the individual set of pre-compiled log-entries."
sudo mkdir /etc/mysecureview/logs
echo "+copy the shell script to be used at bootup/wakeup"
sudo cp $curr/sleepwatcher/config/rc.wakeup /etc/mysecureview/rc.wakeup
echo "+move imagesnap"
sudo cp $curr/imagesnap-master/imagesnap /etc/mysecureview/imagesnap
#establishing root ownership of /etc/mysecureview/
#sudo chmod 700 /etc/mysecureview
#echo "+establishing root ownership of /etc/mysecureview/"
echo "========================"
echo "~Installation Succesful~"
echo "========================"
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>sleepwatcher.system</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/sleepwatcher</string>
<string>-V</string>
<string>-w /etc/mysecureview/rc.wakeup</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
脚本本身:
#!/bin/sh
sudo cd /etc/mysecureview/
sudo ./imagesnap
./imagesnap拍摄照片并将其放在同一目录中。该文件名为“snapshot.jpg”。我搜索了整个mac,并没有任何带有此名称的.jpg。我认为问题在于创建或安装plist,但在launchd上搜索OSX开发人员页面并没有多大帮助。
我在这里看到了一些问题,有些是致命的,有些则不那么严重。
sudo cd
没有做任何有用的事情,因为cd
发生在子流程中。实际上,由于脚本应该以root身份运行,因此使用sudo
是不必要的。这是幸运的,因为如果需要sudo
它将无法工作,因为那里没有人输入管理员密码来授权它。cp
命令将失败,但随后脚本将运行并宣布“~Instal Succesful~”。此外,在脚本本身你只是假设cd
成功;您应该始终检查cd
命令是否失败,因为如果它失败,则脚本的其余部分将以意外的方式运行,并带来潜在的危险结果。RunAtLoad
和KeepAlive
键设置为true后,守护程序将在启动时立即运行,然后一旦完成它将一次又一次地运行...您需要更改启动守护程序.plist以便它只在适当的时间启动脚本,或者让脚本本身在适当的时候在imagesnap
的背景中挂起。sudo launchctl load /Library/LaunchDaemons/daemonname.plist
添加到安装脚本中(并确保仅在安装的其余部分成功时才运行)。
您可能还想检查是否已安装旧版本的守护程序,如果是,则在更换之前将其卸载(sudo launchctl unload ...
)。<key>StandardOutPath</key><string>/tmp/sleepwatcher.out</string>
和<key>StandardErrorPath</key><string>/tmp/sleepwatcher.err</string>
之类的内容添加到守护程序plist文件中。请注意,在重新启动计算机或使用sudo launchctl unload
和sudo launchctl load
之前,对plist的更改不会生效。要通过macOS上的Launchd执行脚本,您需要:
~/Library/LaunchAgents
Launchctl
来管理任务由于您不确定脚本是否成功执行,因此您可以在plist文件中指定输出文件:
<!-- Output error messages -->
<key>StandardErrorPath</key>
<string>/Users/myname/path/to/stderr.log</string>
<!-- Output messages -->
<key>StandardOutPath</key>
<string>/Users/myname/path/to/stdout.log</string>
要获得更详细的信息和说明,您可以阅读此post。