Mac OS X:ユーティリティスクリプト(bash scripts)シリーズ-2


今回はMac大侠が作成したスクリプトを抜粋し、このスクリプトは7 pm後、ユーザーが使用していないときにシャットダウンしようとし、ユーザーがログインしたらスリープモードにしようとした.管理者はStartupItemに参加して実行することができる.ネットワーク環境をよりグリーンにする.

#!/bin/sh

# Sleep or shutdown script
# tryin' to be 'green'.....

# look for exception file
if [ -f "/var/db/.dontSleep" ]; then
exit 0
fi

# if we're a laptop, exit.
# No shutting down laptops (or waking them up unbidden!)
IS_LAPTOP=`/usr/sbin/system_profiler SPHardwareDataType | grep "Model" | grep "Book"`
if [ "$IS_LAPTOP" != "" ]; then
exit 0
fi

# check the time; exit if it's between 5 am and 7 pm
current_hour=`/bin/date +%H`
if [ $current_hour -gt 5 -a $current_hour -lt 19 ]; then
exit 0
fi

# now check idle time;
# exit if we've been idle less than 20 minutes
idleTime=`ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=int((pop @F)/1000000000); print $idle,"/n"; last}'`
if [ $idleTime -lt 1200 ]; then
exit 0
fi

# tell Power Manager to wake us up or turn us on at 6am M-F
pmset repeat wakeorpoweron MTWRF 06:00:00

# check to see if a user's logged into the console
login_status=`/usr/bin/who | /usr/bin/awk '{ print $2 }'`
for i in $login_status; do
if [ $i = "console" ]; then
# someone's logged in, sleep
osascript -e 'tell application "System Events" to sleep'
exit 0
fi
done

# if we got this far, it's OK to shut down.
/sbin/shutdown -h now
exit 0