From the course: CompTIA Linux+ (XK0-005) Cert Prep

Manage one-time jobs with at - Linux Tutorial

From the course: CompTIA Linux+ (XK0-005) Cert Prep

Manage one-time jobs with at

- [Narrator] There are two different types of scheduled jobs. One-time jobs and recurring jobs. For one-time jobs, we can use a service called at. The at service runs jobs at a certain time, or in the case of a batch job, when the CPU load average drops below 0.8. The syntax for at is, at space followed by time format. At supports a wide variety of time formats, including simple 12 and 24 hour clock times, like 4:25 AM or 16:45. It supports general terms like midnight, noon, tomorrow, and now plus a specified number of minutes, hours, or days. It even supports teatime, which is apparently at 4:00 PM. If we don't want to schedule a one-time task for right now, we can specify a time and date. The time format always has to come before the date. The at service is usually installed by default. Let's be sure by installing it using Yum, and if there's a newer version, it will update it. Type into a terminal, sudo space yum space install space, dash y space at, and hit Enter, and then type in your password. We also want to ensure the service is running and survives a reboot. Type in sudo space systemctl space start space atd, and hit Enter, and then bring your line back and change start to enable, and hit Enter again. This makes sure the at service is running, and will start on boot. To create an at job that will run in five minutes, we would type in clear, then at space now space plus 5min and hit Enter. This will give us a prompt where we can type in Linux commands to run. Type in mk dir space tilde slash documents dot bak, and hit Enter. Type in rsync space dash a space tilde slash documents slash space tilde slash documents dot bak. Make sure you put the trailing slash on the second line. It's necessary for rsync to copy the files inside of documents to the back of directory. When done, press Control + D. To verify our at job, type in atq. Starting in the left hand column, we see the at job number, in the second column, the time and date, then the queue letter, and lastly the username. We can view the at file using at space dash c and the at job number. Type an at space dash c space, the at job number, which in my case is 4 and hit Enter. This shows the contents of the at job, including the Shell environment. The commands we typed in will appear at the bottom. If you'd like to cancel an at job, use the atrm command. Type in atrm space and the at job number. Again, my number is four, and hit Enter. And we can verify with atq. The at service also allows us to create batch jobs. The difference between an at job and a batch job is that the at job runs at a specified time. Batch jobs run when the system load average drops below 0.8, ensuring they only get run when the system isn't busy. To create a batch job, type in clear, then type in batch and hit Enter. Now insert the commands you wish. For this example, we'll do something simple. Type in touch space tilde slash batch file dot txt. Press Control + D to save it. Type in atq to get a list of jobs. If our computer is busy, the batch job won't run. If the system load average drops below 0.8, the batch file will run. Atq shows batch jobs as well as at jobs. If it doesn't show anything, then the job must have run. To verify this, we can check to see if the file was created using ls. Type in ls space dash l space tilde slash batch file dot txt and hit Enter. The existence of this file means our batch job did run.

Contents