Category Archives: Scripting

Un-pausing printers in OS X as a non-administrator

A sore spot for me is the print server in OS X, talk about something that should be simple. The biggest headache we have had with printing in a 1-to-1 environment are pushing drivers out to different clients and that student and faculty printer keep on pausing and because they are not an administrator they cannot un-pause them, we are going to address the second issue here. I know you can add them to the lpadmin group and be done with it, but I don’t like giving people access unless absolutely necessary when I am responsible for the uptime and reliability of the machines.

In stead we added a bit of code to our login hooks so that if a printer does get paused once the end user restarts it will empty the printer queues and un-pause any paused printers. It is a very simple concept and very useful, and no ARD to un-pause a printer, just tell them to restart which they probably need to do anyway. Below is the unPausePrinters.sh:

#!/bin/bash

# Author: Joseph J. Viscomi    E-Mail: jjviscomi [at] gmail [dot] com || jviscomi [at] brehm [dot] org
# Date: 3/11/2011
# Description: This script should be used in a login hook. Upon loging on it clears any
#              print jobs in the printer queues, and unpauses any paused printers.

#EMPTYS ALL PRINTER QUEUES THAT ARE PAUSED
sudo cancel -a `lpstat -t | grep disabled | awk '{print $2}'`
#UNPAUSES PAUSED PRINTERS
sudo cupsenable `lpstat -t | grep disabled | awk '{print $2}'`

Better workaround when unable to authenticate to WGM in OS X

I oversee a large OpenLDAP / OpenDirectory network, and when implementing Apple’s OpenDirectory into the mix we came across some very stage errors and bugs. When dealing with OpenDirectory we found that it was not well documented and the fixes to a lot of our problems were to demote and promote or reboot. Both of these fail to explain why and introduce down time into the system, this was un acceptable. So one of our biggest pains was the authentication to the directory was not working via WGM or when attempting to do an authenticated bind. However when a restart was preformed (as was recommend by Apple) everything starting working correctly again. This seem to occur randomly with out any real concrete event causing it, after extensive review of our logs.

With further inspection I was able to craft a simple bash script which flushed out & restarted the services that were having the problems. This prevented downtime and was simple enough to schedule via launchd on our servers or to simple run it when needed. Now this is NOT a fix but a better work around then restarting the entire server or the ridiculous demote and promote of the server (If someone suggests this then they have no idea what they are talking about, you might as well as do a full reinstall).

Below the flushodm.sh script can be easily run to restore most normal server operations.

#!/bin/bash

# Author: Joseph J. Viscomi    E-Mail: jjviscomi [at] gmail [dot] com || jviscomi [at] brehm [dot] org
# Date: 3/23/2011
# Description: This script can be used to flush the state of OpenDirectory Service without
#              restarting the server. This should be run as root or using sudo.

# KILLS Directory Service Process - It will automatically restart.
dscacheutil -flushcache
kill -9 `ps ax | grep DirectoryService | grep -v grep | awk '{ print $1 }'`

# KILLS Password Service - It will automatically restart.
dscacheutil -flushcache
kill -9 `ps ax | grep /usr/sbin/PasswordService | grep -v grep | awk '{ print $1 }'`

# FLUSHES mDNSResponder
dscacheutil -flushcache
kill -HUP `ps ax | grep /usr/sbin/mDNSResponder | grep -v grep | awk '{ print $1}'`
dscacheutil -flushcache

#FORCE REPLICATION - JUST FOR GOOD MEASURE
slapconfig -replicatenow

How to convert a document to PDF using bash in OS X

Below we have a script called 2PDF.sh. It was motivated to automatically have our servers convert digital book format (xml or html) files to PDF’s so that the resources can be shared amongst our faculty and students, since PDF’s worked well with our existing assistive software.

#!/bin/bash

# First make sure that you are passing the file as the first
# argument, and it exists.
if [ $# = 1 ] && [ -e $1 ]; then

  #Print the file as a PDF to stdout using CUPS, redirect stderr so no error output is visible.
  cupsfilter $1 2> /dev/null

fi

This very simple script is run on one of our OS X (10.6) servers when a new digital book is uploaded. This script is run with the file passed as its first argument. Since the output is directed to stdout we can redirect it anywhere, giving us maximum flexibility.

So this script might be run as follows:

./2PDF.sh popular_mechanics.xml > popular_mechanics.pdf

This quite simply just takes the file popular_mechanics.xml file converts it to a pdf and redirects its output to a new file called popular_mechanics.pdf.

Our script is more complicated that what is presented but I just wanted to focus on how in OS X and other platforms that use CUPS can utilize bash to script PDF creation with no additional software or external scripts.

Performance Optimization WordPress Plugins by W3 EDGE