Mac OS X: 实用脚本程序(bash scripts)系列-2

这次摘录一个Mac大侠编写的脚本,这个脚本在7pm后,在没有用户使用时试图关机,如果有用户登陆则试图进入休眠模式. 管理员可以在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

你可能感兴趣的:(exception,OS,脚本,bash,profiler,script#)