Linux at command
Linux at command is used to schedule a task to execute it once at a specified time, without editing a configuration file. It is an alternative to the cron job scheduler. It can be useful if we want to execute a process after some time, such as a shutdown system, taking backups, sending emails as reminders, and more.
The at command is capable of executing a command or script at a specified time and date, or at a given time interval. We can use minutes, hours, days, or weeks to specify the time. It also allows some keywords such as midnight or teatime (which is used for 'to 4 pm').
Note: It cannot be used for recurring tasks, for recurring tasks prefer the Linux crontab.
The atq and atrm commands are also a part of the at command. Let's understand both commands in brief.
The atq command: The atq command is used to display the user's pending tasks. If the user is a root user, it will list all the tasks. Otherwise, it will list the particular user's job. The format of displaying output is Task id, date, hour, queue, and username.
The atrm command: It is used to delete tasks, identified by their job number.
Various options are supported by the 'at' command to make it more specific. The options that are facilitated with the at command are as following:
To use the at command, we have to install it on our system. Let's understand how to install it.
Install at command in Linux (Ubuntu)
To install at command, execute the below command:
The above command will ask for the system password, type the password, and press ENTER key. It will start a daemon process and install the at command to your machine. Consider the below output:
Once the at command is installed, we must start and enable the atd service. To start and enable the atd service at the boot time, execute the below commands:
The above command will start and enable the atd service. Consider the below output:
Once the atd service is enabled on our machine, we can schedule any task or command. Now, we are ready to schedule our first task. Let's move forward to it.
Schedule the first task using at
To schedule a task, execute the at command followed by the time or a keyword.
For example, to schedule the task sh back up at 10.20 pm, execute the command as follows:
Press CTRL+D or ^d keys to complete the task. The above command will schedule the backup.sh at 10.20 pm. Consider the below output:
We can also use the below command for the same process:
The above command will perform the backup.sh at 10.20 pm. Consider the below output:
List the scheduled tasks
We can list all the scheduled tasks by using the atq command. If we execute it from the root user, it will list all user's jobs. But, if we are not a root user, it will only list the particular user's jobs. Execute the command as follows:
Consider the below output:
From the above output, we can see that we have scheduled only one task. It is displaying the task id, date and time, and user name, respectively.
Remove scheduled task
We can remove a scheduled task by its task id with atrm command. To remove a scheduled job, execute the atrm command followed by job id as follows:
The above command will remove the task having task id 2. consider the below command:
From the above output, we can see if a job is successfully removed, it will not display any output. To verify whether the job is successfully removed or not, execute the atq command.
Check the content of the scheduled task
We can list the task by executing the atq command. But if we want to know that what script or command is scheduled, we need to execute the at command with '-c' option followed by task id. Execute the command as follows:
The above command will display the content of the task having task id 2.
Examples of the at command
Some useful examples of the at command are as following:
Example1: Schedule task at 8:00 AM.
To schedule the task at 8:00 AM, execute the command as follows:
Example2: Schedule task at 8:00 AM on the coming Tuesday.
To schedule task at 8:00 AM on coming Tuesday, execute the command as follows:
Example3: Schedule task at 8:00 AM on coming 15'th of April.
To schedule a task at 8:00 AM on coming 15'th April, execute the below command:
Example4: Schedule task at 8:00 AM tomorrow.
To schedule a task at 8:00 AM tomorrow, execute the below command.
Example5: Schedule tasks to execute just after 2 hours.
To schedule a task to execute just after 2 hours, execute the below command:
If you get stuck anywhere while using the 'at' command, you can take help from your terminal by reading the manual. To display the manual of at command, execute the man command as follows:
The above command will display the manual, which contains information about the at command as well as supported options. Consider the below output:
To read more scroll the output and to exit from the manual, press 'q' key.