Notes‎ > ‎

Mac OS X/BSD Scripts and Tools

posted Jul 23, 2011 6:31 AM by Amol Kolhe   [ updated Jul 23, 2011 6:50 AM ]

Memory Usage % on Mac

Usage: ./memory.sh

#!/bin/sh

case $1 in
"") ps -acev | awk '{print $12}' | grep [0-9] | awk 'BEGIN {sum=0}{sum+=$1} END {print "MEM % " sum}' ;;
[0-9])
LINES=$1
echo "%MEM\tProcess"; ps -acev | awk '{print $12 "\t\t" $13}' | sort -nr | head -$LINES
esac


Battery Remaining on a Mac

Usage: ./battery.sh

#!/bin/bash

maxcap=`ioreg -l | grep Capacity |awk '/MaxCapacity/{print $5}'`
curcap=`ioreg -l | grep Capacity |awk '/CurrentCapacity/{print $5}'`

curcap=`echo $curcap*1000|bc`
proc=`echo $curcap / $maxcap |bc`;

len=`echo ${#proc}`;

proc_num=${proc:0:3}
proc_dec=${proc:3:4}

proc_num_len=`echo ${#proc_num}`;

if [ $proc_num_len == "3" ]; then
if [ $proc_num != "100" ]; then
proc_num=${proc:0:2}
proc_dec=${proc:2:3}
fi
fi

echo $proc_num.$proc_dec%


CPU Usage % on a Mac

Usage: ./cpu.sh

#!/bin/sh
case $1 in
"") ps -acev | awk '{print $11}' | grep [0-9] | awk 'BEGIN {sum=0}{sum+=$1} END {print "CPU % " sum}' ;;
[0-9])
LINES=$1
echo "%CPU\t\tProcess"; ps -acev | awk '{print $11 "\t\t" $13}' | sort -nr | head -$LINES
esac

WorldClock script

Usage: ./worldclock.sh

#!/bin/sh
echo "New York | `date +%a\ %m/%d\ \|\ %H:%M`"
echo "India | `date -v+5H -v+30M -u +%a\ %m/%d\ \|\ %H:%M`"


Periodic System Functions

The preriodic command executes To get more information, check manpages for periodic

To execute all periodic scripts
sudo periodic daily weekly monthly

Log files:
/var/log/daily.out
/var/log/monthly.out
/var/log/weekly.out

Following files contain the schedule information for each periodic task:
/System/Library/LaunchDaemons/com.apple.periodic-daily.plist
/System/Library/LaunchDaemons/com.apple.periodic-monthly.plist
/System/Library/LaunchDaemons/com.apple.periodic-weekly.plist

Main Configuration File: /etc/defaults/periodic.conf