Bash Script – Using at Jobs

In the world of Bash scripting, scheduling tasks to run at specific times is a common need. Imagine you want to automatically back up your important files every night or run a script at a particular moment in the future. This is where the at command comes to the rescue.

In this blog post, we’ll take a close look at how to use at to schedule one-time tasks in Bash. We’ll explore its syntax, advanced features like relative and absolute time expressions, and learn how to manage and modify scheduled tasks. By the end of this article, you’ll have a solid understanding of how to harness the power of at for job scheduling in your Bash scripts. So, let’s dive in and make your script scheduling a breeze!

Job Scheduling

In the world of Bash scripting, job scheduling is like setting up a to-do list for your computer. It’s a way to tell your computer to perform specific tasks automatically at certain times. Sometimes, you only want a task to happen once, like running a script to create a backup or sending an email at a precise moment. That’s where the at command comes in handy.

Introduction to the at Command

The at command is a useful tool in Bash scripting that allows you to schedule one-time tasks. It’s like setting an appointment for your computer. You tell it when to do something, and it takes care of the rest. Whether it’s running a script, sending a message, or any other task, at makes it easy to schedule these actions at a specific time and date. In this article, we’ll delve into how you can use the at command to make your Bash scripts more powerful and automated.

Scheduling One-Time Tasks

Have you ever had a task that you needed to do on your computer just once, but you didn’t want to sit around and wait for the right moment to do it manually? This is where scheduling one-time tasks becomes really helpful. It’s like telling your computer, “Hey, I want you to do this specific thing at this exact time, and you don’t have to worry about it anymore.”

Introduction to the at Command for One-Time Scheduling

The at command in Bash is your go-to tool for scheduling one-time tasks. It’s like a digital alarm clock for your computer. You can set it to execute a command or run a script at a precise moment. No more staying up late or setting reminders; your computer will do the work for you.

Syntax and Basic Usage of at for One-Time Tasks

Using the at command is straightforward. Here’s the basic syntax:

at <time and date> <<<'<command or script>'

Let’s break this down:

<time and date>: This is where you specify when you want your task to run. You can use various time formats, like “HH:MM” for hours and minutes, or you can use phrases like “tomorrow” or “next week.”

<command or script>: Here, you write the command or script you want to run at the specified time. For example, if you want to create a backup script to run at 3:00 PM tomorrow, you’d write something like:

at 3:00 PM <<< '/path/to/'

Once you’ve set this up, your computer will automatically execute the backup script at the specified time and date. It’s a simple and effective way to automate tasks without manual intervention.

Advanced at Features

So far, we’ve learned how to schedule one-time tasks using the at command in Bash, specifying the time and command to run. But at offers some advanced features that make it even more flexible and powerful.

Using Relative Time Expressions with at

Sometimes, you don’t want to set an exact time but instead schedule a task relative to the current time. at allows you to do just that using relative time expressions. For example, you can say “run this task 30 minutes from now” or “execute this command in 2 hours.”

Examples of Relative Time Expressions

Here are some examples of relative time expressions:

To run a task 30 minutes from now:

at now + 30 minutes <<< '<command>'

To schedule a task in 2 hours:

at now + 2 hours <<< '<command>'

Scheduling Tasks Using Absolute Timestamps

While relative time expressions are handy, sometimes you need precision. You can also schedule tasks using absolute timestamps, specifying the exact date and time when the task should run.

Formatting Timestamps Correctly

When using absolute timestamps, it’s essential to format them correctly. The format typically looks like this: “HH:MM MM/DD/YY” for hours, minutes, month, day, and year. For example:

at 14:30 10/15/23 <<< '<command>'

Handling Time Zone Differences

Time zones can be tricky when scheduling tasks. If you’re working on a system with different time zones, it’s crucial to handle them correctly to avoid scheduling mishaps.

Tips for Dealing with Time Zone Issues

Here are some tips:

Always specify the time zone explicitly when scheduling tasks using the -t option. For example:

at -t Europe/London 15:00 10/15/23 <<< '<command>'

Be aware of server or system time zone settings and account for any differences when scheduling tasks.

Listing and Managing at Jobs

Once you’ve scheduled tasks using the at command, it’s essential to know how to manage them efficiently. In this section, we’ll explore how to list, remove, and modify your at jobs.

How to List All Scheduled at Jobs

To see a list of all the scheduled at jobs on your system, you can use the atq command. It provides a clear overview of what tasks are waiting to be executed.

Using the atq Command

Here’s how to use the atq command:


This command will display a list of job IDs along with their scheduled execution times.

Removing Unwanted at Jobs

If you want to cancel a scheduled at job, you can use the atrm command, followed by the job ID.

Using the atrm Command to Delete Specific Jobs

For example, if you want to cancel a job with ID 2, you would use:

atrm 2

This will remove the specified job from the queue, preventing it from running.

Modifying Existing at Jobs

Sometimes, you might need to make changes to a scheduled at job, like updating the command or changing the execution time. To do this, you can create a new at job with the desired changes and then remove the original job.

Techniques for Making Changes to Scheduled Tasks

For instance, if you initially scheduled a job to run a script at 3:00 PM but now want it to run at 4:00 PM, you can follow these steps:

Create a new at job with the updated time:

at 4:00 PM <<< '<new command>'

Find the job ID of the original task using atq.

Use atrm to remove the original job:

atrm <job ID>

Practical Examples

Now that we’ve explored the at command’s ins and outs, let’s dive into some real-world examples of how you can use it to automate tasks in your everyday computing.

Automated Backups

Imagine you want to create automatic backups of your important files every day at midnight. You can achieve this with at. Here’s how:

at midnight <<< 'tar -czvf /path/to/backup/backup_$(date +%Y%m%d).tar.gz /path/to/files'

This command schedules a backup job to run every day at midnight, compressing the specified files into a timestamped archive.

System Maintenance

System maintenance tasks often need to happen during periods of low activity. For instance, you want to clean up temporary files and optimize your system every Sunday at 3:00 AM:

at 3:00 AM Sunday <<< 'sudo apt-get autoclean && sudo apt-get autoremove && sudo systemctl restart apache2'

This at job ensures that your system is automatically maintained and optimized at the specified time.

Automated Reports

Suppose you have a script that generates daily reports for your business. You can schedule it to run every day at 9:00 AM using at:

at 9:00 AM <<< '/path/to/'

By doing this, you’ll receive your daily reports without having to run the script manually.

Best Practices and Considerations

Using the at command for job scheduling is a valuable skill, but there are some best practices and considerations to keep in mind to ensure your tasks run efficiently and securely.

Tips for Efficient and Reliable Job Scheduling with at

Specify Full Paths: Always provide full paths to commands and scripts within your at job. This ensures that the system knows exactly where to find them.

at 9:00 AM <<< '/usr/local/bin/'

Use Absolute Timestamps: When scheduling tasks, consider using absolute timestamps for clarity and precision, especially when dealing with critical operations.

at 15:30 10/15/23 <<< '<command>'

Error Handling and Logging

It’s essential to account for errors that may occur during task execution. You can use redirection to capture and log any potential errors:

at 9:00 AM <<< '/path/to/ > /path/to/output.log 2>&1'

In this example, any output, including errors, from the script will be redirected to output.log. This way, you can review the log for any issues that may arise during task execution.

Security Considerations When Using at in Scripts

Be Cautious with Privileges: Avoid running at jobs with elevated privileges unless necessary. Running tasks with superuser (root) privileges can be risky if not handled carefully.

Validate Input: If your at job takes input from users or external sources, ensure that you validate and sanitize the input to prevent any security vulnerabilities.

Restrict Access: Limit access to the at command and its related files to authorized users to prevent unauthorized scheduling and execution of tasks.


In this article, we’ve unlocked the power of the at command in Bash scripting. We learned how to schedule one-time tasks with precision, explored advanced features like relative time expressions, and discovered how to manage and modify at jobs effectively. With practical examples such as automated backups and system maintenance, we’ve seen how at can simplify our computing lives.

Remember to follow best practices, handle errors gracefully, and consider security measures when using at in scripts. By mastering this command, you’ll streamline your automation tasks and make your computing experience more efficient and reliable. So, go ahead and start scheduling your tasks with confidence using at!

Frequently Asked Questions (FAQs)

What is the at command in Bash?

The at command is a tool in Bash that lets you schedule one-time tasks to run at specific times and dates automatically.

How do I schedule a task with at?

Can I schedule tasks relative to the current time?

How do I list all my scheduled at jobs?

What if I want to remove a scheduled at job?

Is it possible to modify an existing at job?

What are some practical use cases for at?

How do I handle errors and logging when using at?

Are there security considerations when using at in scripts?

What’s the key takeaway from using at in Bash scripting?


Leave a Reply

Your email address will not be published. Required fields are marked *

five × two =