How to Use Batch Command in Linux

Batch is a command-line utility in Linux. It reads or scans commands from the standard input (stdin) or a given file and executes them when the system load level allows. More specifically, it runs these commands when the system load average falls below a certain limit. It sends all the results and messages from standard output and standard error(stderr) for scheduled commands. However, you can redirect them elsewhere if you prefer.

Let’s learn how the batch command works in Linux using some practical examples.

Table of Content

  • How Does Batch Command Work in Linux?
  • How to Install Batch Command on Linux?
  • Basic Usage of Batch Command in Linux
  • Echo a Command to batch
  • Executing Multiple Commands Using batch
  • Scheduling a Script Using batch Command
  • Checking the Queue of Pending Jobs/Commands
  • Removing Specific Job from Job Queue
  • Final Thoughts

How Does Batch Command Work in Linux?

To use the batch command in Linux, simply type “batch” in your terminal and hit the “Enter” key. In simple words, the batch command is executed without any parameters, like this:

After executing the batch command, specify the commands that you want to schedule and then press “CTRL + D” to signal the end of the input.

How to Install Batch Command on Linux?

The “batch” command belongs to the “at” package, which is not pre-installed on Linux distros, however, we can install it using the below-mentioned command:

Install Batch Command on Linux

Let’s execute the below-given command to verify the batch command’s installation:

Install Batch Command on Linux

Basic Usage of batch Command in Linux

First, execute the “batch” command in the terminal without any parameters to start scheduling a batch job, like this:

On successful execution, this command will lead you to the “at>” prompt, which is similar to the prompt you see when using the at command for scheduling tasks. Here, enter the input/command that you want to execute, for example:

At the end of input, press “CTRL+D” on your keyboard; as a result, you will see “<EOT>” on your screen, as follows:

Usage of batch Command in Linux

When you hit the “CTRL+D” on your keyboard, a confirmation message will appear, showing the job number and the approximate time the job is scheduled to execute.

Echo a Command to batch

You can echo a command to batch to schedule a command. For example, the below command schedules the creation of an empty text file via the batch command when the system load is low:

Usage of batch Command in Linux

Scheduling Multiple Commands Using batch

You can execute several commands using the batch command, as it lets you run as many commands as needed, either from script or interactively. To do this, use the below-mentioned syntax:

This command schedules the creation of a file named “sampleFile1.txt”, and writes “Hi Geeks!” to another file named sampleFile2.txt, and compresses them into a tarball to be executed during low system load.

Scheduling Multiple Commands Using batch

Scheduling a Script Using batch Command

You can create a shell script, specify commands of your choice in it, and schedule this script via batch command. To do this, go through the below-listed instructions:

Step 1: Create a Script

Let’s create a shell script using nano, and name it “batchExample.sh”:

Step 2: Write Commands into the Script

Now paste the following code into the “batchExample” script:

This script first creates a new empty file named batchExample.txt, then overwrites the content of the “example.txt” file with the “Hi Geeks, Welcome to tutorialspoint.com”. Finally, it compresses the data of the targeted directory into a gzip-compressed tarball:

Scheduling Multiple Commands Using batch

Step 3: Enable Executable Permissions for the Script

Let’s make the script executable by running the chmod command:

Enable Executable Permissions for the Script

Step 4: Schedule the Shell Script with batch Command

Finally, schedule the shell script with the batch command to run when the system usage is minimal:

Enable Executable Permissions for the Script

Checking the Queue of Pending Jobs/Commands

When handling complex batch processing and scheduling systems, it’s crucial to understand how job queues work. The batch command doesn’t have a built-in mechanism to check/manage the queue of pending jobs. However, this can be done using the “atq” command:

Checking the Queue of Pending Jobs/Commands

Removing Specific Job from Job Queue

You can remove a specific Job from the Job queue by using the atrm command followed by the job id to be removed. For example, the following atrm command removes a job with id 11:

Checking the Queue of Pending Jobs/Commands

Now you can verify the job’s removal by executing the atq command:

Checking the Queue of Pending Jobs/Commands

You can also remove multiple jobs from the queue by using their respective job IDs as follows:

Checking the Queue of Pending Jobs/Commands

This sums up the use of the batch command in Linux.

Final Thoughts

The batch command in Linux is a useful utility that enables us to schedule commands to run when the system load is low. It offers a convenient way of managing system resources by running tasks during off-peak times. In this way, the system remains responsive to other essential operations.

To use batch command in Linux, first, you need to install it on your system using a specific package manager. After installation, you can use the batch command without arguments or by echoing commands to it. This article illustrated several use cases of the batch command in Linux using suitable examples.

Scroll to Top