batch

process managementlinux
The batch command is one of the most frequently used commands in Linux/Unix-like operating systems. batch The batch command reads commands from standard input or a specified file and executes them when system load levels permit. Unlike at, which executes commands at a specific time, batch executes commands when the system load average drops below a threshold.

Quick Reference

Command Name:

batch

Category:

process management

Platform:

linux

Basic Usage:

batch [options] [arguments]

Common Use Cases

    Syntax

    batch [file]

    Options

    Option Description
    file Read commands from the specified file instead of standard input

    Note: The batch command has very few options itself, but it uses the at daemon (atd) for job scheduling. To see information about scheduled batch jobs, use the atq command. To remove scheduled batch jobs, use the atrm command.

    Examples

    How to Use These Examples

    The examples below show common ways to use the batch command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Schedule a command to run when system load permits
    batch
    echo "tar -czf /backup/archive.tar.gz /home/user/data" > /dev/null 2>&1
    [Ctrl+D]
    # Schedule a CPU-intensive task batch find / -type f -name "*.log" -exec grep "ERROR" {} \; > /tmp/error_report.txt [Ctrl+D]
    # Use a script file as input batch < backup_script.sh
    # Chain multiple commands batch apt-get update && apt-get upgrade -y echo "System updated at $(date)" >> /var/log/system-updates.log [Ctrl+D]

    Advanced Examples:

    # Combine with redirection to create a log file
    batch
    find / -name "*.mp3" > /tmp/music_files.txt 2>/tmp/music_find_errors.log
    [Ctrl+D]
    # Schedule resource-intensive data processing batch for file in /data/*.csv; do python3 /scripts/process_data.py "$file" >> /var/log/processing.log 2>&1 done [Ctrl+D]
    # Run a command with specific environment variables batch export JAVA_OPTS="-Xmx4g" /opt/application/bin/rebuild-index.sh [Ctrl+D] # Combine with sudo to run privileged commands (if sudo is configured) batch sudo updatedb sudo find / -xdev -type f -mtime -1 > /tmp/recent_files.txt [Ctrl+D]

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    How Batch Works:

    The batch command is part of the at job scheduling system. It functions as follows:

    • Commands are queued and executed when the system load average falls below 1.5 (or a value specified in the atd configuration).
    • Jobs are scheduled in queue "b" by default (visible with atq).
    • If the system load is already below the threshold, the job may start almost immediately.
    • The atd daemon must be running for batch jobs to execute.
    • All output from commands is mailed to the user unless redirected.

    Comparison with at:

    • at: Executes commands at a specific time.
    • batch: Executes commands when the system load permits (no specific time).
    • Both use the same underlying mechanism and share the atq and atrm commands for queue management.
    • Both are affected by the same permission controls (/etc/at.allow and /etc/at.deny).

    System Administration Considerations:

    • The load average threshold can be modified by editing the atd service configuration.
    • Batch is ideal for scheduling system maintenance tasks that should not impact system performance.
    • The atd daemon must be enabled and running: systemctl status atd
    • Access to batch is controlled by the same files as at: /etc/at.allow and /etc/at.deny
    • If neither file exists, typically only the superuser can use batch (depends on system configuration).
    • An empty /etc/at.deny file allows all users to use batch, which is common on many systems.

    Practical Usage Tips:

    • Always redirect output to a file when using batch to avoid email notifications.
    • Include full paths to commands and files since batch may run with a different environment.
    • Set any needed environment variables within the batch job itself.
    • Use the atq command to check if your batch job is in the queue.
    • If you need to cancel a batch job, use atrm with the job number shown by atq.
    • Consider using a shebang line (#!/bin/bash) if your job requires specific shell features.

    Related Commands

    These commands are frequently used alongside batch or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the batch command works in different scenarios.

    $ batch
    View All Commands