4
我編寫了一個腳本,用於檢查可用內存(使用vmstat和awk),然後在內存不足25%時釋放內存。然後,清除後,我的Mac說(是的愚蠢的機器人的聲音)大聲的可用內存的整數。我覺得它比一個消息框彈出不那麼侵入性,但我的妻子HATES它,因爲她在家工作了幾天,它嚇了她一跳。暫停直到登錄用戶空閒才能在OS X中運行shell腳本
我正在尋找一種方法來設置全局變量(甚至觸摸/刪除臨時文件),當檢測到擊鍵或鼠標移動時。這樣,當機器閒置時,機器人的聲音將會保持安靜。我一直在尋找大約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
它「凍結」的提示,然後打印出來,當鼠標移到一個數字或一個鍵被按下,但我不知道因此我無法修改它。如果可能,我寧願堅持使用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
我沒有看到'purge'在做什麼,它是在那裏定義的,我只是想念它嗎? 'mf = $(perl〜/ scripts/py/memfree.py)'??? perl for python?最後,這是一個相對複雜的問題。你可以把它縮減到一個較小的測試案例,顯示問題?祝你好運。 – shellter 2011-12-28 17:26:08
清除命令通過安裝XCode ap進行安裝。它免費。嘿對不起..那個perl/python的東西是一個混合。感謝您的幫助。 – benmar 2011-12-28 17:54:05