Linux batch command

Linux 命令大全Linux Command Reference


batch is a command-line tool in Linux systems used to execute tasks when system load is low. It is part of the at command family, specifically designed to automatically run batch jobs when the system is idle.

Unlike the at command, batch does not require a specific execution time; instead, the system decides when to run tasks based on current load conditions. When the system load average is below 0.8 (configurable), batch automatically executes the tasks in the queue.


How the batch command works

batch works through the following mechanisms:

  1. Load detection: The system continuously monitors the load average.
  2. Task queue: Puts tasks into a dedicated queue (usually the "batch" queue).
  3. Automatic triggering: Executes tasks in the queue when system load falls below the threshold.
  4. Sequential execution: Executes tasks sequentially according to the first-in, first-out (FIFO) principle.

Basic syntax

The basic syntax format of the batch command is:

batch [选项] [时间规格]

Common options

Option Description
-f 文件 Reads commands from the specified file instead of standard input.
-m Sends email to the user after task completion.
-q 队列 Specifies a particular queue to use (the default is the batch queue).
-v Displays the task execution time.

Time specification format

Although batch does not strictly require a time parameter, it accepts the same time format as the at command:

HH:MM    特定时间(24小时制)
midnight 午夜(00:00)
noon     中午(12:00)
teatime  下午茶时间(16:00)
AM/PM    上午/下午标识
now + 时间  相对时间(如 now + 2 hours)

Usage examples

Example 1: Basic usage

  1. Enter the batch command
  2. Enter the command to execute (press Ctrl+D to end input)

Example

$ batch
at> echo "This will run when system is idle" >> ~/batch_test.log
at> date >> ~/batch_test.log
at>   # Press Ctrl+D
job 5 at Thu Mar  2 14:00:00 2023

Example 2: Reading commands from a file

Create a task script file:

Example

$ cat myscript.sh
#!/bin/bash
echo "Starting backup at $(date)" >> ~/backup.log
tar -czf ~/backup-$(date +%Y%m%d).tar.gz ~/Documents
echo "Backup completed at $(date)" >> ~/backup.log

Submit the task:

Example

$ batch -f myscript.sh
job 6 at Thu Mar  2 14:05:00 2023

Example 3: Viewing tasks in the queue

Example

$ atq
5       Thu Mar  2 14:00:00 2023 a userid batch
6       Thu Mar  2 14:05:00 2023 a userid batch

Example 4: Deleting tasks in the queue

$ atrm 5  # 删除任务ID为5的任务

Advanced configuration

Modify the load threshold

By default, batch executes tasks when the system load average is below 0.8. You can adjust this threshold by modifying the configuration of the atd service:

  1. Edit the configuration file (the location may vary by distribution):
sudo nano /etc/atd.conf
  1. Modify or add the following line(s):
OPTS="-l 1.5"  # 设置负载阈值为1.5
  1. Restart the atd service:
sudo systemctl restart atd

View system load average

Example

$ uptime
 14:10:30 up 2 days,  3:15,  3 users,  load average: 0.45, 0.60, 0.72

The three numbers represent the system load average over the past 1 minute, 5 minutes, and 15 minutes, respectively.


Precautions

  1. Permission control: By default, the /etc/at.allow and /etc/at.deny files control which users can use the batch command.
  2. Environment variables: batch tasks do not inherit all environment variables from the current shell; set them explicitly in the script when necessary.
  3. Output handling: The standard output and errors of tasks are sent to the user by email by default; it is recommended to redirect them to a file.
  4. Long-running tasks: batch is not suitable for tasks that run for too long; consider using nohup or tmux.
  5. System service: Ensure the atd service is running (systemctl status atd)

Practical application scenarios

  1. System maintenance: Perform maintenance tasks such as disk cleanup and log rotation during off-peak hours.
  2. Resource-intensive tasks: Jobs that require a lot of system resources, such as big data processing and compiling large projects.
  3. Scheduled reports: Generate daily/weekly system usage reports.
  4. Automated backups: Perform backup operations when the system is idle.
  5. Batch jobs: Process multiple similar tasks in the queue.

Comparison with related commands

Command Execution time Applicable scenario Resource utilization
at Specify a specific time Tasks executed at a precise time Does not consider system load
batch When the system is idle Non-urgent background tasks Intelligently utilizes idle resources
cron Periodic scheduling Tasks that repeat periodically Does not consider system load
nohup Immediate execution Long-running tasks Continuously consumes resources

FAQ

Q1: How do I view the output of batch tasks?

A: By default, the output is sent by email. You can also redirect it to a file in the command:

batch < output.log 2>&1
EOF

Q2: Why didn't my batch task execute?

A: Possible reasons:

  • System load remains above the threshold
  • The atd service is not running
  • The task was cancelled by an administrator
  • The user does not have permission to use batch

Q3: Can batch tasks access the graphical interface?

A: No. batch tasks run in a non-interactive environment and cannot access the GUI or display devices.

Q4: How do I set the priority of batch tasks?

A: You can use the nice command to adjust the priority:

Example

batch &lt;&lt;EOF
nice -n 10 command
EOF

Summary

The batch command is a powerful batch processing tool in Linux systems. It can:

  • Intelligently utilize idle system resources
  • Automatically execute tasks during low load
  • Simplify background task management
  • Improve system resource utilization

Mastering the batch command can help system administrators and developers manage system resources more efficiently, and it is especially suitable for executing non-urgent background processing tasks.


Linux 命令大全Linux Command Reference

Other extensions