#!/usr/bin/sh #---------------------------------------------------------------------------------- # /var/install/bin/lprng_power-queue-control - view log file # # Copyright (c) 2010-2024 The Eisfair Team, team(at)eisfair(dot)org # # Creation: 2018-06-08 jed # Last Update: $Id$ # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #---------------------------------------------------------------------------------- # debug mode: true/false #debug=true if ${debug:-false} then exec 2>/tmp/$(basename ${0})-trace$$.log set -x ask_debug=true export ask_debug fi config_file=/etc/config.d/lprng_power stop_file=/var/run/lprng_power.stop interval=30 if [ -f ${config_file} ] then # load configuration . ${config_file} # start control process if [ "${START_LPRNG_POWER}" = 'yes' ] then rm -f ${stop_file} # check if an active configuration exists queue_list='' idx=1 while [ ${idx} -le ${LPRNG_POWER_N} ] do eval active='$LPRNG_POWER_'${idx}'_ACTIVE' if [ "${active}" = 'yes' ] then eval queue='$LPRNG_POWER_'${idx}'_PRINTER_CAPNAME' lpq -P${queue} | grep -q "ERROR:" if [ $? -ne 0 ] then if [ -z "${queue_list}" ] then queue_list="${queue}" else queue_list="${queue_list} ${queue}" fi fi fi idx=`expr ${idx} + 1` done if [ -n "${queue_list}" ] then # active queues exist while [ 1 ] do { # check if stop of process has been requested if [ -f ${stop_file} ] then # exit loop rm -f ${stop_file} exit 0 fi for LPQ in ${queue_list} do # 1. get queue status # 2. remove all control characters from output, see # http://www.lprng.com/PrintingCookbook/#RF1179COMMANDS # 3. revert line order # 4. grep all lines form the beginning until the first match # 5. remove matching and empty lines job_log=`lpq -P${LPQ} | tr -d '\000-\007\008\009' | tac | awk '1; /job .* removed at/{exit}' | grep -E -v "job .* removed at|^[ \t]*$"` if [ -n "${job_log}" ] then # Status: opening device '/dev/usb/lp1' at 23:42:38.564 device_path=`echo "${job_log}" | grep "Status: opening device" | sed 's#^.* device .\(.*\). at.*#\1#'` # Status: cannot open '/dev/usb/lp1' - 'No such file or directory' # Status: cannot open '192.168.1.104%9100' - 'Connection refused' echo "${job_log}" | grep -E -i -q "Status: cannot open .${device_path}.*No such file or directory|Status: cannot open .*Connection refused" if [ $? -eq 0 ] then # 'cannot open' message found, force switch-on of device /usr/bin/lprng_power-access --queue ${LPQ} --on-autooff fi fi done } > /dev/null sleep ${interval} done & fi fi fi