Linux Comm Batch
[ Linux Command Encyclopaedia](#)\n\n* * *\n\nbatch is a command tool in Linux systems used to execute tasks when system load is low. It belongs to the at command family and is specifically designed to automatically run batch jobs when the system is idle.\n\nUnlike the at command, batch does not require specifying a specific execution time. Instead, the system decides when to run the task based on the current load. When the system average load is below 0.8 (configurable), batch will automatically execute tasks in the queue.\n\n* * *\n\n## How batch Command Works\n\nbatch works through the following mechanisms:\n\n1. **Load Detection**: The system continuously monitors average load (load average)\n2. **Task Queue**: Places tasks in a dedicated queue (typically the "batch" queue)\n3. **Automatic Trigger**: Executes queued tasks when system load falls below the threshold\n4. **Sequential Execution**: Executes tasks in First-In-First-Out (FIFO) order\n\n* * *\n\n## Basic Syntax\n\nThe basic syntax format for the batch command is:\n\nbatch \n\n### Common Options\n\n| Option | Description |\n| --- | --- |\n| `-f file` | Read commands from specified file instead of standard input |\n| `-m` | Send email to user after task completion |\n| `-q queue` | Use specific queue (default is batch queue) |\n| `-v` | Display task execution time |\n\n### Time Specification Format\n\nAlthough batch does not strictly require a time parameter, it can accept the same time formats as the at command:\n\nHH:MM Specific time (24-hour format)\nmidnight Midnight (00:00)\nnoon Noon (12:00)\nteatime Afternoon tea time (16:00)\nAM/PM AM/PM indicator\nnow + time Relative time (e.g., now + 2 hours)\n\n* * *\n\n## Usage Examples\n\n### Example 1: Basic Usage\n\n1. Enter the batch command\n2. Enter the command to execute (press Ctrl+D to end input)\n\n## Example\n\n$ batch\n\n at> echo"This will run when system is idle">> ~/batch_test.log\n\n at> date>> ~/batch_test.log\n\n at> # Press Ctrl+D\n\n job 5 at Thu Mar 2 14:00:00 2023\n\n### Example 2: Read Commands from File\n\nCreate a task script file:\n\n## Example\n\n$ cat myscript.sh\n\n#!/bin/bash\n\necho"Starting backup at $(date)">> ~/backup.log\n\ntar-czf ~/backup-$(date +%Y%m%d).tar.gz ~/Documents\n\necho"Backup completed at $(date)">> ~/backup.log\n\nSubmit the task:\n\n## Example\n\n$ batch -f myscript.sh\n\n job 6 at Thu Mar 2 14:05:00 2023\n\n### Example 3: View Tasks in Queue\n\n## Example\n\n$ atq\n\n5 Thu Mar 2 14:00:00 2023 a userid batch\n\n6 Thu Mar 2 14:05:00 2023 a userid batch\n\n### Example 4: Delete Tasks from Queue\n\n$ atrm 5 # Delete task with ID 5\n\n* * *\n\n## Advanced Configuration\n\n### Modify Load Threshold\n\nBy default, batch executes tasks when system average load is below 0.8. You can adjust this threshold by modifying the atd service configuration:\n\n1. Edit the configuration file (location may vary by distribution):\n\nsudo nano /etc/atd.conf\n1. Modify or add the following line:\n\nOPTS="-l 1.5" # Set load threshold to 1.5\n1. Restart the atd service:\n\nsudo systemctl restart atd\n\n### View System Average Load\n\n## Example\n\n$ uptime\n\n14:10:30 up 2 days, 3:15, 3 users, load average: 0.45, 0.60, 0.72\n\nThe three numbers represent the system average load over the past 1 minute, 5 minutes, and 15 minutes respectively.\n\n* * *\n\n## Notes\n\n1. **Permission Control**: By default, /etc/at.allow and /etc/at.deny files control which users can use the batch command\n2. **Environment Variables**: batch tasks do not inherit all environment variables from the current shell; explicitly set them in the script if needed\n3. **Output Handling**: Task standard output and errors are sent to users via email by default; it is recommended to redirect to files\n4. **Long-running Tasks**: batch is not suitable for tasks that run for too long; consider using nohup or tmux\n5. **System Service**: Ensure the atd service is running (`systemctl status atd`)\n\n* * *\n\n## Practical Application Scenarios\n\n1. **System Maintenance**: Execute disk cleanup, log rotation and other maintenance tasks during off-peak hours\n2. **Resource-intensive Tasks**: Big data processing, compiling large projects and other jobs that require large amounts of system resources\n3. **Scheduled Reports**: Generate daily/weekly system usage reports\n4. **Automated Backup**: Execute backup operations when the system is idle\n5. **Batch Processing**: Process multiple similar tasks in the queue\n\n* * *\n\n## Comparison with Related Commands\n\n| Command | Execution Time | Applicable Scenario | Resource Utilization |\n| --- | --- | --- | --- |\n| `at` | Specified specific time | Tasks requiring precise time execution | Does not consider system load |\n| `batch` | When system is idle | Non-urgent background tasks | Intelligently utilizes idle resources |\n| `cron` | Periodic schedule | Regularly recurring tasks | Does not consider system load |\n| `nohup` | Immediate execution | Long-running tasks | Continuously occupies resources |\n\n* * *\n\n## Frequently Asked Questions\n\n### Q1: How to view batch task output?\n\nA: By default, output is sent via email. You can also redirect to a file in the command:\n\nbatch
YouTip