In this article, we’ll explore the at command and how to use it to streamline your system administration tasks.
The content of this article includes:
Linux at Command
The at command in Linux is a powerful tool for scheduling tasks to execute them at a certain defined time. It allows users to specify the exact date and time when a command should be run. The at command allows users to manage different tasks automatically.
The at command also includes the
- atq Command
- atrm Command
The “atq” is used to display pending tasks for a specific user, while root users can view all tasks. The output format shows the task ID, date, hour, queue, and username.
On the other hand, “atrm” is used to delete tasks using their job number.
These commands provide additional functionality and control for managing scheduled tasks with the at command in Linux.
Installing at Command in Linux
The Linux at command might not be by default installed in your system. To install at on Linux, use the below-given command:
Understanding the at command
The at command is used to schedule jobs to run at a specific time in the future, without the need for a continuous process running in the background. The command works by adding jobs to a queue, which are then executed at the designated time.
The syntax of the at command is as follows:
Where:
- f filename: Specifies a file containing the commands to run
- m: Sends email notification when the job is completed
- q queue: Specifies the job queue to use
- v: Verbose output
- time: The time at which to run the job
You can schedule jobs using a variety of time formats, including absolute times, relative times, and times specified as minutes past the hour.
Absolute time expressions include:
- YYMMDDhhmm[s]: This format specifies the abbreviated year, month day, hour, minutes and also seconds option.
- CCYYMMDDhhmm[s]: This format includes the complete year, month day, hour, minutes and seconds.
- now: This specifies the current time or immediate execution of at command.
- midnight: This equals to 00:00 AM.
- noon: This equals to 12:00 PM.
- teatime: This time is taken as 4 PM.
- AM: Time before noon or 12:00 PM.
- PM: Time after noon or 12:00 PM.
- today: This specifies the current day.
- tomorrow: This specifies the day after today.
For example, the given below command will print “Hello” at 4PM:
To specify a relative time expression using at command, we have to specify the command by adding the “+” plus sign in any one of these:
- minutes
- hours
- days
- weeks
- months
- years
For example, to schedule a task at 5 minutes after current time, use:
Schedule a Bash Script Using at Command
Using the at command is straightforward. Simply enter the command followed by the time at which you want the job to run and the command you want to execute.
For example, to run a script, follow the below-given syntax:
Note: I have created a bash script that will append the text “Hello World” inside a file “myfile.txt” whenever it’s executed.
We can verify the content inside the file using the cat command to ensure the script is run successfully at the given time.
Interactively Scheduling a Job Using at Command
Using the at command in Linux, users can interactively schedule tasks by specifying the desired execution time and entering the corresponding commands at the prompt. This process is like creating events in a calendaring app.
For example, we can create a memo text and schedule it like a remainder on a specific time. Run the following command to schedule a memo that adds “Linuxhint” in a text file at specified time:
After executing the above command, add the following command to add a text “Linuxhint” inside a file.
To exit the at prompt, users can press CTRL+D on their keyboard.
Note: The above command creates a text file and appends the text inside the file.
After the task has run, one can verify its execution by using the cat command on the corresponding file.
Scheduling a Job with at
The at command in Linux allows users to schedule tasks by piping commands to it using echo without using the interactive prompt.
For example, users can specify the delay time in minutes and the “now” notation to schedule a task, as shown in the following example:
This command schedules a task to run 1 minute from the current time and appends the text “Welcome to Linuxhint” inside the file when executed.
Using this method, we can schedule tasks without the need for interactive input.
After one minute, run the cat command to verify the schedule execution:
List all Running Jobs
To list all running jobs, you can run:
Or:
All running scheduled jobs will be listed with their Job ID.
Removing a Schedule Job Using at Command
To remove a scheduled job, we must know the Job ID of a running process. Following syntax will be followed to remove a scheduled job.
For example, to remove the current schedule jobs having job ID 4, use:
Remove the at Command
To remove the at command from Linux system, use:
Conclusion
The at command is a useful utility for scheduling jobs on Linux systems. It allows users to automate tasks, which can save time and increase productivity. The at command allows users to specify the exact time and date for a job to be executed, making it a powerful tool for managing complex tasks. For a detailed description of at command read the above-mentioned guidelines.