batch Command in Linux



The batch command is used to read commands from standard input or a specified file and execute them when system load levels permit, in other words, when the load average drops below 0.8 or the value specified in the invocation of atrun.

Other similar commands −

  • at Command − The at command allows you to schedule the execution of commands at a specific time in the future. You provide a time (e.g., "tomorrow at 4:00 PM" or "in 2 hours") and the command you want to run. The task is then queued for execution at the specified time.
  • atq Command − The atq command lists pending jobs in the queue. It displays information about scheduled tasks, including job numbers, dates, hours, and job classes. If you're the superuser, it lists everyone's jobs; otherwise, it shows only your own.
  • atrm Command − The atrm command deletes scheduled jobs based on their job numbers. You only specify the job number you want to remove.

Jobs scheduled with the at command are executed only once, making it an ideal tool for managing one-time tasks that need to be completed at an exact time.

Batch also schedules jobs, but it waits for system load levels to drop below a certain threshold before executing them. It's useful for non-urgent tasks that can wait until system resources are available.

Table of Contents

Here is a comprehensive guide to the options available with the batch command −

Batch Command Options

The following are various options you can use with the batch command.

Options Description
-V Prints the version number to standard error.
-q queue

Specifies the queue to use. The valid queue designations range from lowercase 'a' to 'z' and uppercase 'A' to 'Z'.

The default queue for at is 'a', and for batch, it's 'b'. Queues with higher letters run with increased niceness. The special queue '=' is reserved for jobs currently running.

-m When this option is used, at sends mail to the user when the job has completed, even if there was no output.
-f file Instead of reading the job from standard input, this option allows you to read the job from a specified file.
-I This is an alias for the atq command, which lists pending jobs in the queue.
-d An alias for the atrm command, which removes a job from the queue.
-v Displays the time when the job will be executed.
-c This option displays the jobs listed on the command line to standard output. It's useful for viewing the details of scheduled jobs.
-t time_arg

With this option, you can submit a job to be run at a specific time specified by the time_arg argument.

The format for time_arg follows that of the touch utility's -t time option argument, which is typically [[CC]YY]MMDDhhmm.

For example, if you want to schedule a job for July 10, 2024, at 3:30 PM, you would use a time_arg like 202407101530.

SHELL

The value of the SHELL environment variable at the time of at invocation will determine which shell is used to execute the at job commands.

If SHELL is unset when you invoke the at command, the user's login shell (the default shell for the user) will be used to run the scheduled commands.

However, if SHELL is set when you invoke at, it must contain the path to a shell interpreter executable. In this case, at will use the specified shell to execute the commands at the specified time.

How to use at Command?

The at command allows you to schedule tasks to run at specific times. Here's a simplified explanation of its syntax −

at [OPTION] runtime

runtime − Specify the date and time when you want the job to execute. You can then enter the command to be executed from the standard input.

For instance, let's create a job that starts at 8 am −

at 08:00
Use of at Command 2

Next, press Enter and input one or more commands you want to execute −

/home/user/myscript.sh

When you're done, press Ctrl-D to exit the prompt and save the job. This command will display the job number and the execution time and date.

Use of at Command 3

Alternatively, you can use the echo command and pipe the command to at. Here is how you can get started −

echo "hello_world" | at 08:00
Use of at Command 1

Remember to replace "hello_world" with the actual command you want to execute. This method is useful when you want to schedule a single command without creating a separate script file.

How to Execute Commands from a File?

To execute commands from a file rather than standard text, use the "-f" flag followed by the path to the file −

at 06:00 -f file1.txt
Execute Commands from File 1

You can also use the "-m" flag to send an email notification even if there is no output in your command −

at 09:00 -m
Execute Commands from File 2

If you want to suppress the email notification, which is sent by default when the command produces output, you can use the "-M" option −

at 09:00 -M
Execute Commands from File 3

How to use Batch Command?

To create a job using the batch command, launch your terminal and run the following command:

echo "Hello from the batch job!"  | batch

This job will be queued and executed when the system load allows. You can replace "Hello from the batch job!" with your actual command.

use of Batch Command

How to Check the Status of a Job?

To check the status of the job you created, use the atq or at "-l" command. This will display a list of pending jobs along with their job IDs.

 atq
Check Status of Job

How to Remove Pending Jobs?

To remove a specific job from the queue, you can use the atrm or at "-r" command followed by the job number. This ensures that the job won't be executed.

atrm 13
Remove Pending Jobs

How to Specify Time Execution?

The at command supports various time specifications. You can specify the time, date, and increment from the current time −

  • Date − You can indicate the date using various formats such as the month name followed by the day and an optional year (e.g., "July 5" or "July 5, 2024"), strings like "today," "tomorrow," or the name of a weekday, and numeric formats like MMDD[CC]YY, MM/DD/[CC]YY, DD.MM.[CC]YY, or [CC]YY-MM-DD.
  • Time − To specify a time, you can use either the 24-hour format (HH:MM) or the 12-hour format with "am" or "pm" (e.g., 10:30am or 6:45pm). You can also use friendly strings like "now," "midnight," "noon," or "teatime" (which corresponds to 4:00pm). If you schedule a task for a time that has already passed, at will automatically reschedule it for the same time on the next day.
  • Increment − The at command also allows you to define increments using the now + count format. Count represents a numerical value, and time-unit can be either minutes, hours, days, or weeks. This feature enables more flexible scheduling for one-time tasks.

In the following sections, we will have some examples that show how you can combine time, data, and increment to schedule jobs.

To schedule a job for the coming Monday at a time twenty minutes later than the current time, use the following syntax −

at monday +20 minutes
Specify Time Execution 1

To schedule a job for July 10, 2024, at 10:00 AM, run −

at 10:00AM 2024-07-10
Specify Time Execution 2

To schedule a job to run two hours from now, run −

at now +2  hours
Specify Time Execution 3

To schedule a job to run at 3 pm four days from now, run −

at 3pm + 4 days
Specify Time Execution 4

How to Specify Queue?

To specify a queue for a job, use the -q option, followed by the desired queue name. For example, if you want to schedule a job in the "T" queue, run −

at tuesday +3 hours -q T

This schedules the job to run on the next Monday, two hours from the current time, specifically in the "T" queue.

Specify Queue For Job

Conclusion

Both at and batch are handy commands that you can use to schedule jobs. When you have a non-standard task to perform, schedule it using the at command. If you want to run a task only when the system load is low enough, consider using the batch command.

Advertisements