Ho un plist associato a questo launchd agent. C'è un agente elencato nella lista launchctl, e chiama il mio script bash, chiamato rc.wakeup, ogni volta che il Mac viene riattivato dal sonno. C'è solo una copia di questo script bash. Il codice nello script di bash non viene ripetuto. Nonostante questo, lo script di bash viene eseguito due volte nella sua interezza. Perché?
Ho creato uno script di bash di installazione e uno script di bash di disinstallazione. Ho eseguito questi in serie un tentativo di far funzionare entrambi gli script in modo che questo programma possa essere utilizzato da chiunque. Credo che l'installazione / la disinstallazione impropria di questi file abbia causato il problema, anche se non ho idea del perché.
Ecco il 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>de.bernhard-baehr.sleepwatcher</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/sleepwatcher</string>
<string>-V</string>
<string>-s /etc/rc.sleep</string>
<string>-w /etc/rc.wakeup</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
Ecco lo script di installazione che uso per installare il programma:
#!/bin/bash
sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
sudo cp ~/Desktop/mysecureview/sleepwatcher_2.2/sleepwatcher /usr/local/sbin
sudo cp ~/Desktop/mysecureview/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8
mkdir ~/mysecureview
mkdir ~/mysecureview/log
mkdir ~/mysecureview/pics
cp ~/Desktop/mysecureview/imagesnap-master/imagesnap ~/mysecureview/imagesnap
mkdir ~/Library/LaunchAgents
cp ~/Desktop/mysecureview/sleepwatcher_2.2/config/de.bernhard-baehr.sleepwatcher-20compatibility.plist ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist
launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist
sudo cp ~/Desktop/mysecureview/sleepwatcher_2.2/config/rc.wakeup /etc/rc.wakeup
sudo cp ~/Desktop/mysecureview/sleepwatcher_2.2/config/rc.wakeup /etc/rc.sleep
cp ~/Desktop/mysecureview/num.txt ~/mysecureview/num.txt
cp ~/Desktop/mysecureview/logs.txt ~/mysecureview/logs.txt
cp ~/Desktop/mysecureview/compile.bash ~/mysecureview/compile.bash
cp ~/Desktop/mysecureview/uninstall ~/mysecureview/uninstall
mkdir ~/mysecureview/logs.txt
echo "========================"
echo "~Installation Succesful~"
echo "========================"
ecco lo script di disinstallazione:
#!/bin/bash
launchctl unload ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist;
rm ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher-20compatibility.plist
sudo rm /etc/rc.wakeup
sudo rm /etc/rc.sleep
sudo killall sleepwatcher
sudo rm -rf /Library/StartupItems/SleepWatcher
launchctl unload de.bernhard-baehr.sleepwatcher
launchctl remove de.bernhard-baehr.sleepwatcher
rm ~/mysecureview/num.txt
rm ~/mysecureview/compile.bash
rm ~/mysecureview/imagesnap
rm ~/mysecureview/logs.txt
for i in {1..100}
do
p=".txt"
x="$i$p"
rm ~/mysecureview/log/$x
done
rmdir ~/mysecureview/log
echo "====================="
echo "~Uninstall Successful"
echo "====================="
Ecco lo script effettivo che viene eseguito quando il computer viene riattivato:
for user in 'echo 'show State:/Users/ConsoleUser' | scutil | awk '/kCGSSessionUserNameKey/ { print $3 }''; do
home='eval echo ~$user'
if [ -x "$home/.wakeup" ]; then
logger -t $0 "executing .wakeup of user $user"
su - $user -c "$home/.wakeup"
fi
done
#The following is the MySecureView script used to capture the picture and its information.
cd ~/mysecureview
# take a picture.
./imagesnap
#get the log index. Create a variable for and iterate the index.
numPics=$(cat ~/mysecureview/num.txt)
numPics=$[numPics+1]
jpg=".jpg"
index="$numPics$jpg"
#We only allow 100 log entries to exist at a time.
if [ $numPics == 100 ]; then
numPics=1
fi
#temp storage for the newest picture.
cp ~/mysecureview/snapshot.jpg ~/mysecureview/pics/$index
rm ~/mysecureview/snapshot.jpg
rm ~/mysecureview/date.txt
# temporary storage for the most recent log entry.
date=$(date)
entry="$numPics $date"
logNum="$numPics.txt"
echo "$entry" > ~/mysecureview/log/$logNum
#store the index for next use.
echo "$numPics" > ~/mysecureview/num.txt
#compile each and every log entry .txt file into the logs.txt file.
bash compile.bash