暂停直到登录用户空闲以在OS X中运行shell脚本

时间:2011-12-28 07:35:09

标签: macos bash osx-lion python-idle

我编写了一个脚本来检查可用内存(使用vmstat和awk),然后在少于25%的空闲时清除内存。然后在清除之后,我的Mac说(是愚蠢的机器人声音)大声可用内存的整数。我发现它比消息框弹出窗口更少侵入,但是我的妻子讨厌它,因为她有几天在家工作,这让她很烦恼。

我正在寻找一种在检测到击键或鼠标移动时设置全局变量(甚至触摸/删除临时文件)的方法。这样,机器空闲时机器人的声音会很安静。我一直在寻找约3个小时没有运气。我用这个片段走在了正确的轨道上:


set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'") 
set initIdleTime to idleTime repeat while idleTime ≥ initIdleTime   
set idleTime to (do shell script "ioreg -c IOHIDSystem | perl -ane 'if (/Idle/) {$idle=(pop @F)/1000000000; print $idle,\"\";last}'") 
end repeat

它会“冻结”提示,然后在移动鼠标或按下某个键时输出一个数字,但我不知道perl所以我无法修改它。如果可能的话,我宁愿坚持使用bash。

这是脚本,以防有人想要使用它们。有些我修改了第一个,复制了第二个并写了第三个。我在cron中设置第三个每10分钟运行一次。

#!/bin/bash

# -- guns
# 
# Purge inactive memory with rapid i/o
# 
# 26 November 2007
# 'killall $command' is still printing to the terminal

# declare variables before= after= memPurged= pageSize= pagesPurged= AvailMem= timeout=5 # in seconds command="du /" 

showUsage="Usage: $(basename $0) [-h] [-t seconds] [-c "command"]"

function printError()  { echo >&2 $showUsage echo >&2  echo >&2 "See '$(basename $0) -h' for more details." }

function printHelp()  { echo "Purge inactive memory with rapid i/o." echo  echo $showUsage echo  echo "Supported Options" echo "-----------------" echo "-t{seconds}tSet the amount of time for the command to run. Default" echo "ttis 5 seconds." echo "-c{"command"}tSet the command to run. Default is 'du /'" echo "-httDisplay this help and exit" }

function getMemFree() {
# the "$3-0" bit is a dirty way to chop the period at the end vm_stat | awk '/Pages free/ {intMemFree=$3-0; print intMemFree}' }

function getPageSize() { vm_stat | awk '/page size/ {print $8}' }

# 
# Main
# 

while getopts ":ht:c:" option; do case $option in h ) printHelp; exit;; t ) timeout=$OPTARG;; c ) command=$OPTARG;; ?) echo >&2 "Invalid option!n"; printError; exit 3 esac done shift $(($OPTIND-1))

before=$(getMemFree)

purge

# set variables after=$(getMemFree) let pagesPurged=$after-$before pageSize=$(getPageSize)




    # calculate and print let memPurged=($pagesPurged * $pageSize / 2**20) 
let availMem=($after * $pageSize / 2**20) printf "%d MB purged." $memPurged 
say $availMem

#!/usr/bin/python

import subprocess
import re

# Get process info
ps = subprocess.Popen(['ps', '-caxm', '-orss,comm'], stdout=subprocess.PIPE).communicate()[0]
vm = subprocess.Popen(['vm_stat'], stdout=subprocess.PIPE).communicate()[0]

# Iterate processes
processLines = ps.split('\n')
sep = re.compile('[\s]+')
rssTotal = 0 # kB
for row in range(1,len(processLines)):
    rowText = processLines[row].strip()
    rowElements = sep.split(rowText)
    try:
        rss = float(rowElements[0]) * 1024
    except:
        rss = 0 # ignore...
    rssTotal += rss

# Process vm_stat
vmLines = vm.split('\n')
sep = re.compile(':[\s]+')
vmStats = {}
for row in range(1,len(vmLines)-2):
    rowText = vmLines[row].strip()
    rowElements = sep.split(rowText)
    vmStats[(rowElements[0])] = int(rowElements[1].strip('\.')) * 4096

print '%d' % ( vmStats["Pages free"]/1024/1024 )

最后

#!/bin/bash

mf=$(perl ~/scripts/py/memfree.py)

if [ "$mf" -lt 2000 ]
then
sh2 ~/scripts/memclean.sh
else
exit
fi

1 个答案:

答案 0 :(得分:3)

WINRAR!

http://hints.macworld.com/article.php?story=20040330161158532

#!/bin/sh

echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

我希望这有助于其他人。谢谢你们。

Ben Mar