My *nix world

Start CUPS only when you need it

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

Start cups when need it

#!/bin/bash

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

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

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

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

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

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

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

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

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

The following two tabs change content below.
Start CUPS only when you need it

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
Start CUPS only when you need it

Latest posts by Eugen Mihailescu (see all)

Tagged on: ,

Leave a Reply

Your email address will not be published. Required fields are marked *