Linux Scripting: Closing an open file in a running process

#!/usr/bin/env bash

TMP_FILE=/var/tmp/.closefd.$$
trap "rm -f ${TMP_FILE}" EXIT QUIT INT 

function usage() {
  echo "$(basename $0) <pid> <file descriptor>"
  exit 1
}

if [ $# -ne 2 ]; then
  usage
elif [ $(echo $1$2 | grep -cP '^\d+$') -eq 0 ]; then
  usage
else
  pid=$1
  fd=$2
fi

cat << EOF >> ${TMP_FILE}
#!/usr/bin/gdb -x
attach $pid
p close($fd)
quit
EOF

chmod +x ${TMP_FILE}
${TMP_FILE}

exit $?
Posted in Miscellaneous | Leave a comment

Apple 10.6.x (Snow Leopard) + TimeMachine + Samba

  1. Run the following script to enable samba network devices and generate a sparsebundle image:
    #!/bin/env bash
    
    HOST="$(hostname -s)"
    BUNDLE="${HOST}.sparsebundle"
    SIZE=""
    
    if [ -d "${BUNDLE}" ]; then
     echo "A bundle directory '${BUNDLE}' already exists in the current directory - please remove it before proceeding"
     exit 1
    elif [ -e "${BUNDLE}" ]; then
     echo "A file with the name '${BUNDLE}' already exists in the current directory - please remove it before proceeding"
     exit 1
    fi
    
    echo "-- Enabling Unsupported Network Volumes (e.g., Samba Shares)"
    defaults write com.apple.systempreferences TMShowUnsupportedNetworkVolumes 1
    
    echo "-- Preparing to build a Sparse Bundle Image:"
    while [ $(echo ${SIZE} | egrep -c '^[0-9]+$') -eq 0 ]; do
     echo -en "   * What is the maximum size you want to enforce on your backup image (in Gigabytes, e.g., 100): "
     read SIZE
    done
    
    echo -n "   * Building... "
    hdiutil create -size ${SIZE}G -fs HFS+J -volname 'Time  Machine Backups' -type SPARSEBUNDLE "${BUNDLE}"
    
    echo "-- Determining Hardware UUID..."
    UUID=$(/usr/sbin/system_profiler 2>&- | grep 'Hardware UUID'  | awk '{ print $3 }')
    
    echo "-- Generating TimeMachine plist..."
    cat << EOF >> "${BUNDLE}/com.apple.TimeMachine.MachineID.plist"
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
     <dict>
     <key>com.apple.backupd.HostUUID</key>
     <string>${UUID}</string>
     </dict>
    </plist>
    EOF
    
    echo
    echo "Bundle is now ready for use. Please move the bundle to the network location that you will use for backups."
    echo "The bundle is located at: ${PWD}/${BUNDLE}"
    echo
  2. copy your new sparsebundle folder to your network share and point your TimeMachine at the share volume.
  3. Force a backup (click on the TimeMachine icon and choose “Backup Now”)
  4. It should start verifying your backup image and then proceed to backing up your computer.
Posted in Miscellaneous | Leave a comment

Standing Resolute

I saw the most amazing thing on my way in to work today – a bird standing firm, in the middle of the sidewalk on 6th and 42nd, against the torrent of morning drones on their way into work.

In the couple of minutes I could spare to watch this amazing little creature, not once did it give way to anyone who found it in their path – no, it remained resolute in it’s timid curiosity of those creatures around it yet refusing to give an inch as creatures hundreds of times it’s size barreled towards it. What a humbling sight watching people give way to a creature so small that it would disappear underneath the average foot of most grown adults.

Was the bird suicidal? Maybe. Or, maybe it was fed up with giving in to the tidal forces that would find most other birds of it’s stature seeking immediate refuge on a nearby powerline…

It could just be pre-autumnal reflection kicking in but, I feel like there’s something to be learned here…

Posted in Miscellaneous | Tagged | Leave a comment