Linux batch command
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:
- Load detection: The system continuously monitors the load average.
- Task queue: Puts tasks into a dedicated queue (usually the "batch" queue).
- Automatic triggering: Executes tasks in the queue when system load falls below the threshold.
- 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
- Enter the batch command
- Enter the command to execute (press Ctrl+D to end input)
Example
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
#!/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
job 6 at Thu Mar 2 14:05:00 2023
Example 3: Viewing tasks in the queue
Example
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:
- Edit the configuration file (the location may vary by distribution):
sudo nano /etc/atd.conf
- Modify or add the following line(s):
OPTS="-l 1.5" # 设置负载阈值为1.5
- Restart the atd service:
sudo systemctl restart atd
View system load average
Example
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
- Permission control: By default, the /etc/at.allow and /etc/at.deny files control which users can use the batch command.
- Environment variables: batch tasks do not inherit all environment variables from the current shell; set them explicitly in the script when necessary.
- 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.
- Long-running tasks: batch is not suitable for tasks that run for too long; consider using nohup or tmux.
- System service: Ensure the atd service is running (
systemctl status atd)
Practical application scenarios
- System maintenance: Perform maintenance tasks such as disk cleanup and log rotation during off-peak hours.
- Resource-intensive tasks: Jobs that require a lot of system resources, such as big data processing and compiling large projects.
- Scheduled reports: Generate daily/weekly system usage reports.
- Automated backups: Perform backup operations when the system is idle.
- 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
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.
Other extensions
Linux Command Reference