Skip to content

Instantly share code, notes, and snippets.

@jacobsalmela
Created August 12, 2014 12:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacobsalmela/f6ffacf9dfe6cf0c5da9 to your computer and use it in GitHub Desktop.
Save jacobsalmela/f6ffacf9dfe6cf0c5da9 to your computer and use it in GitHub Desktop.
(OS X) Fix paused printers
#!/bin/bash
#----------AUTHOR------------
# Jacob Salmela
# 7 November 2013
# https://github.com/jakesalmela/
#----------RESOURCES---------
# You need terminal-notifier to be installed
# I used brew install terminal-notifier
# You could also get it at github
# https://github.com/alloy/terminal-notifier
#---------DESCRIPTION--------
# Finds paused or disabled printers and re-enables them
#-----------USAGE------------
# To run:
#
# --Locally:
# sudo ./Fix-Paused-Printers.sh
# --Casper Suite:
# Upload to Casper Admin and run via Casper Remote
# --Apple Remote Desktop:
# Copy and paste this script and run as root
#----------VARIABLES---------
# OS Version for sending Notifications if using 10.8 or higher
osVersion=$(sw_vers -productVersion)
#----------FUNCTIONS---------
#####################################
function findDisabledPrintersAndFix()
{
# If no disabled printers exist
if [ -z $(lpstat -p | awk '/disabled/ {print $2}') ] &> /dev/null
# Then just echo message and be done
then echo "The printer(s) are nominal."
# Puts all of the current printer statuses into a variable
printerStatus=$(lpstat -p | awk '{print $1, $2, $3, $4}')
# List status of all printers so admin can see that the script worked
echo -e "$printerStatus"
return 3
exit
# Otherwise something is wrong and the for loop should start
else echo "DISABLED: Something is wrong with the printer(s)..."
fi
# for the output of the lpstat command...
for OUTPUT in $(lpstat -p | awk '/disabled/ {print $2}')
# ...do the following commands
# The lpstat command above displays printer information
# That is piped into awk and searches for the term disabled
# Finally it displays only the 2nd field which is the printer name
do
# Cancel all jobs
echo -e "\tCancelling jobs for $OUTPUT"
cancel -a
# Disable CUPS and sleep for two seconds
echo -e "\tDisabling CUPS…"
cupsdisable $OUTPUT
# sleep 1
echo -e "\tCUPS disabled."
# Re-enable CUPS and sleep for two more seconds
echo -e "\tRe-enabling CUPS…"
cupsenable $OUTPUT
# sleep 1
echo -e "\tCUPS enabled."
# Complete the for loop
done
# Puts all of the current printer statuses into a variable
printerStatus=$(lpstat -p | awk '{print $1, $2, $3, $4}')
# List status of all printers so admin can see that the script worked
echo -e "$printerStatus" 2> /dev/null
}
#########################
function notifyUserOfFix()
{
# Check to see if terminal-notifier is installed and is executable
if [ -x /Applications/Terminal\ Notifier.app/Contents/MacOS/terminal-notifier ];then
# If it is, send the user a notification and optional audible alert
# Uncomment the -execute line and the backslash on the line above it to make the alert audbile
echo "Sending notification to user that the printer is fixed..."
/Applications/Terminal\ Notifier.app/Contents/MacOS/terminal-notifier \
-group printers \
-title "\"Printer(s) fixed\"" \
-subtitle "\"It should be working now.\"" \
-message "\"You will need to re-submit your print jobs\"" #\
# -execute $(say "Try printing now." &> /dev/null)
echo "Notification sent to user."
# If notifier does not exist, just exit
else
echo "I fixed the printer(s), but could not notify the user."
echo "terminal-notifier is not installed or not executable. Exiting..."
# Return a non-zero code meaning it did not complete successfully
return 1
exit
# Close the if statement
fi
}
#---------------------------------#
#---------------------------------#
#----------script starts----------#
#---------------------------------#
#---------------------------------#
findDisabledPrintersAndFix
if [ $? -eq 0 ];then
case $osVersion in
10.6.*) echo "Notifications not supported on $osVersion";;
10.7.*) echo "Notifications not supported on $osVersion";;
10.8.*) notifyUserOfFix;;
10.9.*) notifyUserOfFix;;
*) echo "No actions available for $osVersion.";;
esac
else
echo "The printer(s) seem OK."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment