Linux batch Command
batch is a command tool in Linux used to execute tasks when the 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 the task based on the current load. 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: Put tasks into a dedicated queue (usually the "batch" queue)
- Automatic triggering: Execute tasks in the queue when the system load is below the threshold
- Sequential execution: Execute tasks in order according to the first-in, first-out (FIFO) principle
Basic Syntax
The basic syntax of the batch command is:
batch [选项] [时间规格]
Common options
| Option | Description |
|---|---|
-f 文件 |
Read commands from a specified file instead of standard input |
-m |
Send an email to the user after the task completes |
-q 队列 |
Specify a particular queue to use (the batch queue by default) |
-v |
Display the task execution time |
Time specification formats
Although batch does not strictly require a time argument, it accepts the same time formats 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
$ batch
at
&
gt;
echo
"This will run when system is idle"
&
gt;
&
gt; ~
/
batch_test.log
at
&
gt;
date
&
gt;
&
gt; ~
/
batch_test.log
at
&
gt;
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)"
&
gt;
&
gt; ~
/
backup.log
tar
-czf
~
/
backup-$
(
date
%
Y
%
m
%
d
)
.tar.gz ~
/
Documents
echo
"Backup completed at $(date)"
&
gt;
&
gt; ~
/
backup.log
Submit the task:
Example
$ batch
-f
myscript.sh
job
6
at Thu Mar
2
14
:05:00
2023
Example 3: View 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: Delete tasks from 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 lines:
OPTS="-l 1.5" # 设置负载阈值为1.5
- Restart the atd service:
sudo systemctl restart atd
View the 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.
Notes
- 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 of the current shell; set them explicitly in the script when necessary
- Output handling: The standard output and errors of a task 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 a very long time; consider using nohup or tmux
- System services: 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 substantial 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 scenarios | 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 utilize idle resources |
cron |
Periodic scheduling | Tasks that repeat periodically | Does not consider system load |
nohup |
Executes immediately | Long-running tasks | Continuously occupy resources |
Frequently Asked Questions
Q1: How to view the output of a batch task?
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 was my batch task not executed?
A: Possible reasons:
- The system load remains above the threshold
- The atd service is not running
- The task was canceled by the administrator
- The user does not have permission to use batch
Q3: Can batch tasks access a graphical interface?
A: No. batch tasks run in a non-interactive environment and cannot access a GUI or display device.
Q4: How to set the priority of a batch task?
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; it can:
- Intelligently utilize idle system resources
- Automatically execute tasks when the load is low
- Simplify background task management
- Improve system resource utilization
Mastering the batch command can help system administrators and developers manage system resources more efficiently, especially for non-urgent background processing tasks.
Linux Command Reference