Start CUPS only when you need it


Please note that this blog has been moved.

Now it has its own domain: mynixworld.info🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

Another thing that is a “must-to-have” on any desktop computer is the printing system. In Linux we have CUPS. But what if you print just one time per week (I mean not so often), why should you keep running CUPS all the time? This not only consumes RAM but also CPU. In fact, any resident program (like a Linux daemon) will consume RAM+CPU. Little bit from here, little bit from there, when you have to sum all of these applications we will find many-many MB of consumed RAM. I found a simple to use solution for that:

  • write a simple bash script (eg printer) that will start the CUPS daemon whenever you need the printer or when you want to stop the CUPS (do not need the printer any more)
  • define on your desktop a keyboard shortcut (like Ctrl+Alt+P) that will start/stop the CUPS daemon

The script:

#!/bin/bash

CUPS=/etc/init.d/cupsd
STATUS=$(${CUPS} status|awk '/status:/ {print $NF}')

if [ "$STATUS" != "started" ];then
	ACTION="start"
	ACTION_STATUS="started"
	ICON="devices/printer.png"
else
	ACTION="stop"
	ACTION_STATUS="stopped"
	ICON="status/printer-error.png"
fi

PROMPT="To $ACTION CUPS daemon you must provide sudo password"
MAX_TRY=3

while [ "$STATUS" != "$ACTION_STATUS" ] && [ $MAX_TRY -gt 0 ];do
	if [ "$ACTION_STATUS" == "stopped" ];then
		DEFAULT_PRN=$(lpstat -d|awk '{print $NF}')
	fi

	gksu -m "$PROMPT" ${CUPS} $ACTION
	MSG=$ACTION_STATUS

	if [ "$ACTION_STATUS" == "started" ];then
                DEFAULT_PRN=$(lpstat -d|awk '{print $NF}')
        fi
	let MAX_TRY=$MAX_TRY-1
	STATUS=$(${CUPS} status|awk '/status:/ {print $NF}')
done

if [ "$STATUS" == "$ACTION_STATUS" ];then
	/usr/bin/notify-send -i "/usr/share/icons/Tango/48x48/$ICON" "Printer $DEFAULT_PRN" "CUPS has been $MSG"
else
	/usr/bin/notify-send -i "/usr/share/icons/Tango/48x48/$ICON" "Printer $DEFAULT_PRN" "Unfortunatly CUPS daemon is still '$STATUS'.\nCheck the log events for more info..."	
fi

After CUPS is started/stopped a notification (thanks to libnotify) will be printed-out on your screen:
printer

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s