Back to Knowledege base

Determining When a Printer Was Last Used

Methods for checking printer activity history when no built-in last-used timestamp exists

There are several ways to determine printer activity, ranging from checking the current state through to parsing historical log files. The most reliable method depends on whether logging has been enabled for the printer in question.

Method 1: Check Printer Log Files

This is the most reliable approach for historical data. Xi-Text can maintain an audit trail of every job printed on a given printer, but only if the logfile keyword has been configured in the printer's setup file.

Finding the log file

Each printer has a definition directory under the printers directory (by default /usr/spool/printers/). Within that directory, setup files control the printer's behaviour for each form type. The log file path is specified by the logfile keyword in the relevant setup file.

To find the log file for a printer:

bash

# Determine the printer's current form type
FORMTYPE=$(splist -F "%f" printername | cut -d'.' -f1)

# Navigate to the printer definition directory
cd /usr/spool/printers/printername

# Check for a form-specific setup file, fall back to default
if [ -f "$FORMTYPE" ]; then
    SETUP="$FORMTYPE"
elif [ -f "default" ]; then
    SETUP="default"
else
    echo "No setup file found for $FORMTYPE or default"
    exit 1
fi

# Extract the logfile path
LOGFILE=$(grep "logfile=" "$SETUP" | cut -d'=' -f2)

if [ -z "$LOGFILE" ]; then
    echo "No logfile configured in $SETUP"
    exit 1
fi

Note that different form types can have different log files for the same printer. If the printer has been used with multiple form types over its lifetime, there may be several log files to check.

Reading the log file

Log file entries are single lines of pipe-separated fields:

09/06|11:52:18|11:56:53|0:05:13|jmc|CS|a|947|tty1|laser|150|3|14408

The fields are, in order:

PositionContent
1Date (dd/mm or mm/dd depending on timezone)
2Time job started printing
3Time job finished printing
4Elapsed time since job was enqueued
5User who submitted the job
6Job title
7Form type
8Bytes printed
9Device
10Printer name
11Priority
12Page count (including banner page)
13Job number (or host:jobnumber for remote jobs)

To see the most recent activity:

bash

tail -5 /usr/spool/printers/printername/.log

To extract just the date and time of the last job printed:

bash

tail -1 /usr/spool/printers/printername/.log | cut -d'|' -f1-3

Future versions of Xi-Text will maintain the same field order. New fields may be appended to the end of the line, so scripts that process log files should not assume a fixed number of fields.

Method 2: Check the Current Printer State

The splist command shows the real-time state of each printer. Whilst this does not tell you when a printer was last used, it does tell you whether it is currently active:

bash

splist -H -F "%p %s %j %u"

This shows the printer name, state with message, job number being printed, and the user who owns that job. A printer that is actively printing will show the printing state along with the job number and user.

The possible states relevant to activity assessment are:

Printing : The printer is currently in use. The job number and user are shown.

Idle : The printer is started and ready to print but has no jobs waiting. It was recently active enough to have been started.

Halted : The printer has been manually stopped. This is the initial state and also the state after an administrator halts the printer. A printer that has been halted for months is likely unused.

Error : An unrecoverable error has occurred. Persistent error states often indicate hardware that has been removed or network addresses that are no longer reachable.

Offline : The printer has been detected as offline. Like the error state, a persistent offline condition suggests the physical device is no longer available.

Method 3: Check Whether Any Jobs Target the Printer

Even if a printer is not currently printing, there may be jobs on the queue waiting for it. This indicates that someone expects the printer to be available:

bash

sqlist -F "%P" | grep -c "printername"

A count of zero means no jobs are currently destined for that printer. To see the details of any jobs that are targeting it:

bash

sqlist -H -q printername -F "%N %u %h %W"

This shows the job number, user, title, and submission time for all jobs directed to the named printer. Jobs with old submission times that are still waiting may themselves be stale.

Method 4: Check the System Error Log

The system error log file (spshed_reps in the spool directory, by default /usr/spool/spd/spshed_reps) records printer errors and state changes. Whilst it is not a comprehensive activity log, it can provide clues about recent printer activity:

bash

grep "printername" /usr/spool/spd/spshed_reps | tail -10

This is most useful for printers that have gone into error or offline states, as those events are logged with timestamps.

A Practical Script for Checking Last Activity

The following script combines the log file method with a fallback to queue and state checks when logging is not configured. It is written in POSIX shell for compatibility across Unix platforms:

sh

#!/bin/sh
# last-printer-activity.sh - Report last activity for a Xi-Text printer
# Usage: last-printer-activity.sh printername

PRINTER=$1

if [ -z "$PRINTER" ]; then
    echo "Usage: $0 printername"
    exit 1
fi

# Check printer exists
STATE=$(splist -F "%t" "$PRINTER" 2>/dev/null)
if [ -z "$STATE" ]; then
    echo "Printer '$PRINTER' not found"
    exit 1
fi

echo "Printer: $PRINTER"
echo "Current state: $STATE"

# Try to find and read the log file
PTRDIR="${SPOOLPT:-/usr/spool/printers}/$PRINTER"

if [ -d "$PTRDIR" ]; then
    FORMTYPE=$(splist -F "%f" "$PRINTER" | cut -d'.' -f1)

    if [ -f "$PTRDIR/$FORMTYPE" ]; then
        SETUP="$PTRDIR/$FORMTYPE"
    elif [ -f "$PTRDIR/default" ]; then
        SETUP="$PTRDIR/default"
    else
        SETUP=""
    fi

    if [ -n "$SETUP" ]; then
        LOGPATH=$(grep "logfile=" "$SETUP" | cut -d'=' -f2)

        if [ -n "$LOGPATH" ]; then
            # Resolve relative paths against printer directory
            case "$LOGPATH" in
                /*) ;;
                *)  LOGPATH="$PTRDIR/$LOGPATH" ;;
            esac

            if [ -f "$LOGPATH" ]; then
                LASTLINE=$(tail -1 "$LOGPATH")
                LDATE=$(echo "$LASTLINE" | cut -d'|' -f1)
                LSTART=$(echo "$LASTLINE" | cut -d'|' -f2)
                LEND=$(echo "$LASTLINE" | cut -d'|' -f3)
                LUSER=$(echo "$LASTLINE" | cut -d'|' -f5)
                LTITLE=$(echo "$LASTLINE" | cut -d'|' -f6)

                echo "Last printed: $LDATE $LSTART-$LEND"
                echo "User: $LUSER"
                echo "Title: $LTITLE"
                ENTRIES=$(wc -l < "$LOGPATH")
                echo "Total log entries: $ENTRIES"
                exit 0
            else
                echo "Log file configured but not found: $LOGPATH"
            fi
        else
            echo "No logfile keyword in setup file"
        fi
    else
        echo "No setup file found in $PTRDIR"
    fi
else
    echo "Printer directory not found: $PTRDIR"
fi

# Fallback: check queue for jobs targeting this printer
JOBCOUNT=$(sqlist -F "%P" 2>/dev/null | grep -c "$PRINTER")
echo "Jobs currently targeting this printer: $JOBCOUNT"

if [ "$JOBCOUNT" -gt 0 ]; then
    echo "Queued jobs:"
    sqlist -H -q "$PRINTER" -F "%N %u %h %W"
fi

Enabling Logging for Future Tracking

If a printer does not have logging configured, it can be enabled by adding the logfile keyword to the printer's setup file. The setup file is located in the printer's definition directory.

To add logging to an existing printer:

  1. Halt the printer first:

bash

sphalt printername
  1. Edit the setup file (either the form-specific file or default):

bash

vi /usr/spool/printers/printername/default
  1. Add the logfile keyword:
logfile=.usage

A relative path like .usage creates the log file in the printer's definition directory. An absolute path can be used to write to a different location.

  1. Restart the printer:

bash

spstart printername

Different form types can have different log files. If the printer is used with multiple form types and you want comprehensive tracking, add logfile= to each setup file.

Managing Log File Growth

Log files grow indefinitely and are not automatically rotated or purged. On a busy printer, the log file can become quite large over time.

Before archiving or purging a log file, halt the printer to prevent data being written during the operation. A basic rotation procedure:

sh

#!/bin/sh
# rotate-printer-log.sh - Archive and rotate a printer log file
# Usage: rotate-printer-log.sh printername logfile

PRINTER=$1
LOGFILE=$2
ARCHIVE="${LOGFILE}.$(date +%Y%m%d)"

# Check if printer is halted
PSTATE=$(splist -F "%t" "$PRINTER")
case "$PSTATE" in
    halted|offline|error) RESTART=no ;;
    *)
        sphalt "$PRINTER"
        # Wait for printer to finish current job
        while true; do
            PSTATE=$(splist -F "%t" "$PRINTER")
            case "$PSTATE" in
                halted|offline|error) break ;;
            esac
            sleep 5
        done
        RESTART=yes
        ;;
esac

# Archive and truncate
cp "$LOGFILE" "$ARCHIVE"
: > "$LOGFILE"

# Restart if it was running
if [ "$RESTART" = "yes" ]; then
    spstart "$PRINTER"
fi

echo "Archived to $ARCHIVE"

Best Practices

Enable logfile on all printers as a matter of course. The overhead is negligible and the audit trail is invaluable for housekeeping, capacity planning, and troubleshooting.

Schedule periodic log rotation to prevent unbounded growth, particularly on high-volume printers.

When reviewing printer activity across the whole system, script a loop over all printers to produce a summary report:

sh

splist -F "%p" | while read PTR; do
    echo "--- $PTR ---"
    sh last-printer-activity.sh "$PTR"
    echo ""
done

This gives a quick overview of which printers are actively used and which have been idle, making housekeeping decisions much more straightforward.

Identifying and Removing Redundant Scheduled Jobs
A systematic approach to reviewing batch job schedules, diagnosing unused jobs, and safely cleaning up the job queue