Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
cancel-cups - CUPS Print Job Cancellation Tool
The cancel-cups command is a specialized utility for canceling print jobs in the Common UNIX Printing System (CUPS). It provides fine-grained control over print job management, allowing users and administrators to remove jobs from print queues, stop ongoing print operations, and manage printer states. This command is essential for troubleshooting printing issues, managing printer resources, and maintaining efficient print workflows in Linux environments. The tool supports both user-level job cancellation and administrative control over all print queues in the system.
Basic Syntax
cancel-cups [OPTIONS] [DESTINATION] [JOB_ID...]
Common Options
Job Selection Options
-a- Cancel all jobs for the current user-u USER- Cancel all jobs for specified user-h HOST- Specify alternate CUPS server-P PRINTER- Specify printer destination
Administrative Options
-U USERNAME- Specify username for authentication-p PASSWORD- Specify password for authentication-E- Enable encryption for the connection-v- Show verbose messages
Filter Options
--all- Cancel all jobs (admin only)--completed- Cancel completed jobs--pending- Cancel pending jobs only--processing- Cancel currently processing jobs
Usage Examples
Basic Job Cancellation
Canceling Specific Jobs
# Cancel a specific job by ID
cancel-cups 123
# Cancel multiple jobs
cancel-cups 123 124 125
# Cancel a job on specific printer
cancel-cups -P laserjet 123
# Cancel job on remote server
cancel-cups -h printserver.example.com 123
User Job Management
# Cancel all jobs for current user
cancel-cups -a
# Cancel all jobs for specific user
cancel-cups -u john
# Cancel all jobs for user on specific printer
cancel-cups -u jane -P office_printer
Printer-specific Operations
# Cancel all jobs on specific printer
cancel-cups -P laserjet
# Cancel jobs on remote printer
cancel-cups -h printserver -P office_printer
# Cancel all pending jobs on printer
cancel-cups -P main_printer --pending
Administrative Operations
System-wide Job Management
# Cancel all jobs on system (admin only)
sudo cancel-cups --all
# Cancel all completed jobs
sudo cancel-cups --completed
# Cancel all jobs for specific user across all printers
sudo cancel-cups -u problem_user --all
# Force cancellation of processing jobs
sudo cancel-cups --processing
Remote Server Management
# Connect to remote CUPS server with authentication
cancel-cups -h remote-server -U admin -P secret123 123
# Cancel jobs on remote server with encryption
cancel-cups -h secure-server -E -U admin 456
# Cancel all jobs on remote server
cancel-cups -h remote-server --all
Practical Examples
Desktop User Scenarios
Personal Print Management
# Check current print jobs
lpq
# Cancel a mistaken print job
cancel-cups 123
# Cancel all documents I sent today
cancel-cups -a
# Cancel job stuck in queue
cancel-cups --processing 156
# Check if job was cancelled
lpstat -o 123
Multi-printer Environments
# Cancel job on color printer (more expensive)
cancel-cups -P color_printer 789
# Move job from busy to available printer (indirect cancellation)
lpmove 789 backup_printer
# Cancel all jobs on problematic printer
cancel-cups -P problem_printer
# Cancel personal job but keep others
cancel-cups -P shared_printer -u $USER
System Administration
Troubleshooting Print Queues
# Check all print queues
lpstat -a
# Cancel all stuck jobs
sudo cancel-cups --all --processing
# Cancel jobs causing printer errors
sudo cancel-cups -P error_printer --all
# Clear old completed jobs to free memory
sudo cancel-cups --completed
# Monitor and automatically cancel problematic jobs
watch -n 5 "lpstat -o | grep stalled | awk '{print \$1}' | xargs -r sudo cancel-cups"
Print Server Maintenance
# Stop all printing for maintenance
sudo cancel-cups --all
# Cancel jobs before printer firmware update
sudo cancel-cups -P updating_printer --all
# Clear queues after network outage recovery
for printer in $(lpstat -p | cut -d' ' -f2); do
sudo cancel-cups -P $printer --all
done
# Emergency stop for confidential document leak
sudo cancel-cups -u breach_user --all
User Print Quota Management
# Monitor user with excessive printing
lpstat -u heavyuser
# Cancel jobs exceeding quota
sudo cancel-cups -u heavyuser --pending
# Automated quota enforcement script
#!/bin/bash
USER_LIMIT=50
for user in $(lpstat -u | grep -o '\([^)]*\)' | sort -u); do
job_count=$(lpstat -u $user | wc -l)
if [ $job_count -gt $USER_LIMIT ]; then
echo "User $user exceeded quota with $job_count jobs"
sudo cancel-cups -u $user --pending
fi
done
Advanced Usage
Complex Filtering and Selection
Conditional Job Cancellation
# Cancel jobs older than 1 hour
lpstat -o | awk '$5 < "'$(date -d '1 hour ago' '+%H:%M:%S')'" {print $1}' | xargs -r sudo cancel-cups
# Cancel large documents (>10MB)
lpstat -l | awk '/size/ && $4 > 10485760 {getline; print $1}' | xargs -r sudo cancel-cups
# Cancel jobs from specific application
lpstat -o | grep "libreoffice" | awk '{print $1}' | xargs -r cancel-cups
# Cancel jobs with specific title pattern
lpstat -o | grep "CONFIDENTIAL" | awk '{print $1}' | xargs -r sudo cancel-cups
Batch Operations
# Cancel all jobs on printers with "error" status
lpstat -p | grep "disabled" | awk '{print $2}' | xargs -I {} sudo cancel-cups -P {}
# Move and cancel jobs in bulk
for job in $(lpstat -o -P old_printer | awk '{print $1}'); do
echo "Moving job $job to new printer"
lpmove $job new_printer 2>/dev/null || sudo cancel-cups $job
done
# Clean up completed jobs older than 24 hours
lpstat -W completed -o | awk '$6 < "'$(date -d '1 day ago' '+%m-%d')'" {print $1}' | xargs -r sudo cancel-cups
Integration and Automation
Shell Scripts
Automated Print Queue Management
#!/bin/bash
# Advanced print queue manager with cancel-cups
LOG_FILE="/var/log/print_manager.log"
MAX_JOBS_PER_USER=20
STUCK_JOB_TIMEOUT=3600 # 1 hour
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}
# Check for users exceeding job limits
check_user_limits() {
for user in $(lpstat -u | grep -o '\([^)]*\)' | sort -u); do
job_count=$(lpstat -u $user | grep -v "completed" | wc -l)
if [ $job_count -gt $MAX_JOBS_PER_USER ]; then
log_message "User $user exceeded limit with $job_count jobs"
sudo cancel-cups -u $user --pending
log_message "Cancelled pending jobs for $user"
fi
done
}
# Check for stuck jobs (processing too long)
check_stuck_jobs() {
current_time=$(date +%s)
lpstat -o | while read line; do
if echo "$line" | grep -q "processing"; then
job_id=$(echo $line | awk '{print $1}')
# This is a simplified check - in reality, you'd parse the job start time
sudo cancel-cups $job_id 2>/dev/null
if [ $? -eq 0 ]; then
log_message "Cancelled stuck job $job_id"
fi
fi
done
}
# Main monitoring loop
while true; do
check_user_limits
check_stuck_jobs
sleep 300 # Check every 5 minutes
done
Print Job Security Filter
#!/bin/bash
# Security-focused print job canceller
KEYWORDS="SECRET CONFIDENTIAL INTERNAL RESTRICTED"
SUSPICIOUS_USERS="guest temp test"
monitor_sensitive_jobs() {
for keyword in $KEYWORDS; do
lpstat -o | grep -i "$keyword" | while read line; do
job_id=$(echo $line | awk '{print $1}')
user=$(echo $line | grep -o '\([^)]*\)' | sed 's/[()]//g')
log_message "ALERT: Sensitive job detected - ID: $job_id, User: $user"
sudo cancel-cups $job_id
# Notify security team
echo "Print job containing '$keyword' cancelled. Job ID: $job_id, User: $user" | \
mail -s "Print Security Alert" security@company.com
done
done
}
monitor_suspicious_users() {
for user in $SUSPICIOUS_USERS; do
job_count=$(lpstat -u $user 2>/dev/null | wc -l)
if [ $job_count -gt 0 ]; then
log_message "Suspicious user $user has $job_count print jobs"
sudo cancel-cups -u $user --all
echo "All print jobs for suspicious user $user have been cancelled" | \
mail -s "Suspicious Print Activity" security@company.com
fi
done
}
Integration with Other Tools
Log Analysis and Job Tracking
# Integrate with CUPS error logs
tail -f /var/log/cups/error_log | grep "job" | while read line; do
if echo "$line" | grep -qi "error\|failed\|unable"; then
job_id=$(echo "$line" | grep -o 'job-id=[0-9]*' | cut -d'=' -f2)
if [ -n "$job_id" ]; then
echo "Error detected for job $job_id, cancelling..."
sudo cancel-cups $job_id
fi
fi
done
# Combine with printer status monitoring
lpstat -p | grep "disabled" | awk '{print $2}' | while read printer; do
echo "Printer $printer is disabled, cancelling jobs..."
sudo cancel-cups -P $printer --all
done
Troubleshooting
Common Issues
Permission Problems
# Permission denied when cancelling jobs
# Solution: Check user permissions and use sudo if needed
sudo cancel-cups --all
# Authentication failed with remote server
# Solution: Use correct authentication credentials
cancel-cups -h remote-server -U admin -p password 123
# SSL/TLS connection issues
# Solution: Enable encryption or check certificate
cancel-cups -h remote-server -E -U admin 123
Job Not Found
# Job ID doesn't exist
# Solution: Check current job list first
lpstat -o
cancel-cups <correct_job_id>
# Job already completed
# Solution: Use --completed flag to remove completed jobs
sudo cancel-cups --completed
# Job on different printer
# Solution: Specify correct printer
cancel-cups -P correct_printer 123
Network Issues
# Can't connect to CUPS server
# Solution: Check server connectivity and firewall
ping printserver.example.com
telnet printserver.example.com 631
# Remote server not responding
# Solution: Use timeout and retry logic
timeout 10 cancel-cups -h remote-server 123 || \
echo "Server unreachable, retrying later..."
Debug Mode and Verbose Output
Diagnostic Commands
# Show verbose cancellation messages
cancel-cups -v 123
# Debug CUPS communication
CUPS_DEBUG_LEVEL=2 cancel-cups 123
# Check CUPS server status
lpstat -h remote-server -r
# Verify job exists before cancelling
lpstat -o 123 && cancel-cups 123
Log Monitoring
# Monitor CUPS error log for cancellation issues
tail -f /var/log/cups/error_log | grep -i "cancel"
# Check access log for cancellation attempts
tail -f /var/log/cups/access_log | grep "CANCEL"
# Monitor system log for authentication failures
tail -f /var/log/auth.log | grep -i cups
Related Commands
lp- Submit print jobs to CUPSlpq- Show printer queue statuslpr- Print fileslprm- Remove print jobslpstat- Show printer status informationlpadmin- Configure CUPS printerslpmove- Move print jobs between printerscupsdisable- Stop printerscupsenable- Start printersaccept- Accept jobs for printersreject- Reject jobs for printers
Best Practices
- Always check job status before cancelling using
lpstat -o - Use job-specific cancellation instead of blanket cancellation when possible
- Verify permissions before attempting administrative cancellations
- Log cancellation activities for audit purposes
- Use authentication when connecting to remote CUPS servers
- Test cancellation on non-critical jobs first
- Implement confirmation prompts for bulk cancellation operations
- Monitor printer status before and after cancellation operations
- Use verbose mode for debugging complex cancellation scenarios
- Document automation scripts that include job cancellation logic
Performance Tips
- Batch multiple job IDs in a single command for better performance
- Use user-specific cancellation (-u) instead of enumerating individual jobs
- Schedule maintenance during low-usage periods for bulk cancellations
- Cache printer lists when performing multiple operations
- Use connection pooling for remote server operations
- Implement timeout handling for network operations
- Prioritize stuck jobs for immediate cancellation
- Use grep filters to reduce the number of jobs processed
- Limit concurrent cancellation operations on busy servers
- Monitor system load during large-scale cancellation operations
The cancel-cups command is a powerful tool for managing print jobs in CUPS environments, providing both basic user-level controls and advanced administrative capabilities. Its comprehensive options and integration with other CUPS utilities make it essential for maintaining efficient and secure printing operations in Linux systems.