koldfront

Re: Turning the printer on and off automatically

🕤︎ - 2021-10-20

Thanks for your blog post. The at command in the posthook is important. I spawned a background child job, but the posthook was active until the child process ended, which was not what I wanted. With the at it works well.

I am wondering though if your solution has a race condition:

Assume you have the printer_off script run by the posthook finding out that the print queue is empty.

Not a new job could come in and the prehook for the new job powers on the printer.

Now the printer power off in the posthook gets executed (the print queue was checked before the new job came in).

rd@home:~$ cat /usr/local/bin/printjob_add.sh 
#!/bin/bash

# update active job count and update printer power state

# $1 Filename
# $2 adder


if [ -e $1 ]; then
    COUNTER=$(tail -1 $1);
else
    COUNTER=0;
fi;
date --rfc-3339=ns > $1
COUNTER=$((COUNTER + $2))
echo $COUNTER >> $1


if [ $COUNTER -eq 0 ]; then
    knxtool groupswrite ip:127.0.0.1 2/6/0 0 > /dev/null 2> /dev/null
    sleep 1
else
    knxtool groupswrite ip:127.0.0.1 2/6/0 1 > /dev/null 2> /dev/null
    sleep 1
fi
rd@home:~$ 

and then called this script with a file lock in the prehook:

flock -x $LASTPRINTFILE /usr/local/bin/printjob_add.sh $LASTPRINTFILE 1

and the posthook:

echo flock -x $LASTPRINTFILE /usr/local/bin/printjob_add.sh $LASTPRINTFILE -1 | at now + 10 minutes

(using your nice at solution :-))

Thanks for your post again Rainer

Add comment

To avoid spam many websites make you fill out a CAPTCHA, or log in via an account at a corporation such as Twitter, Facebook, Google or even Microsoft GitHub.

I have chosen to use a more old school method of spam prevention.

To post a comment here, you need to:

¹ Such as Thunderbird, Pan, slrn, tin or Gnus (part of Emacs).

Or, you can fill in this form:

+=