Home > Process Management > batch

batch: Execute commands when system load is low

The batch command waits until the system's average load drops below 0.8, and then executes the specified commands. This is useful for efficiently using system resources and handling background tasks without interfering with critical operations.

Overview

batch belongs to the at command family and is a scheduler that monitors system load and executes tasks when the load is low. It is primarily used for automatically processing resource-intensive tasks during off-peak hours. batch internally uses the 'b' queue of the at command, and jobs in this queue are only executed when the system load average is below 0.8.

Key Features

  • System load-based execution: Starts jobs when the average load is below 0.8
  • Schedules jobs with syntax similar to the at command
  • Suitable for processing resource-intensive tasks in the background
  • Can notify users via email upon job completion (utilizing at options)

Key Options

The batch command itself does not have many direct command-line options and primarily accepts commands to be executed from standard input or a file. The following are the main ways to pass commands to batch.

Command Input Methods

Generated command:

Try combining the commands.

Description:

`batch` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Examples of how to use the batch command to execute specific tasks when the system load is low.

Schedule a simple command via standard input

batch
ls -l > /tmp/batch_output.txt
<EOT>

When the system load becomes low, this command saves a list of files in the current directory to /tmp/batch_output.txt.

Schedule multiple commands via pipe

echo -e "echo 'Batch job started at $(date)'\nls -a\necho 'Batch job finished'" | batch

This example schedules multiple lines of commands to batch using the echo command.

Schedule script file execution

echo '#!/bin/bash\necho "Hello from batch!" > /tmp/batch_hello.txt\ndate >> /tmp/batch_hello.txt' > my_script.sh
chmod +x my_script.sh
batch -f my_script.sh

This example creates a script file named my_script.sh and then schedules it with batch. (The my_script.sh file must have execute permissions.)

Installation

The batch command is part of the `at` package. It may not be installed by default on most Linux distributions. If needed, you can install it using the following commands.

Debian/Ubuntu based systems

sudo apt update && sudo apt install at

CentOS/RHEL/Fedora based systems

sudo yum install at

Tips & Considerations

Useful tips and points to consider when using the batch command.

Key Considerations

  • **Load Threshold**: batch starts jobs when the system load average is below 0.8. This value can vary based on system configurations like `/etc/at.deny` or `/etc/at.allow` files and is influenced by the `atd` daemon's settings.
  • **Environment Variables**: Jobs executed by batch may not inherit the environment variables of the current shell. Therefore, it is recommended to explicitly set necessary paths (PATH) and other environment variables within your scripts.
  • **Log Checking**: If jobs do not execute as expected or encounter errors, you can check system logs (e.g., `/var/log/syslog`, `/var/log/messages`, or `journalctl -u atd`) to diagnose the cause of the problem.
  • **Job Management**: You can view scheduled batch jobs using the `atq` command and cancel them using the `atrm` command.

Same category commands