Wednesday, March 16, 2016

Week 22: Day 057 - Scheduling Tasks


Today we will be learning about timing tasks, with tools mainly such as cron. I've been aware of that tool for a while, and it's very useful for timing actions on your machine, many times being something along the lines of shutting down your computer at a scheduled time.

What is cron? Cron is a service which runs processes at specific times. Some system tasks are already using cron, probably without your awareness, such as "logrotate" which automatically gets rid of old logs at a certain time. To see what's going on with cron right now at this second, type "systemctl status crond -l". The most important part is the first part, because it shows that it's loaded. These are some examples of how you schedule cron properly:
  •  * 11 * * * Any minute between 11:00 and 11:59 (probably not what you want)
  • 0 11 * * 1-5 Every day at 11 a.m. on weekdays only
  • 0 7-18 * * 1-5 Every hour on weekdays on the hour
  • 0 */2 2 12 5 Every 2 hours on the hour on December second and every Friday in December
Instead of editing /etc/crontab you can create a configuration file for it seperately, and this will be put into /etc/cron.d then put the scripts for those cron commands into /etc/cron.hourly or cron.daily or cron.monthly, you get the point! User specific files are created with crontab -e (username). This is extremely useful if you don't want to do the job for all users. Then there's anacron which is really a configuration file for cron and where these script files are put. If you want to configure this file go to /etc/ancrontab it's a file not a directory. To restrict who can use cron make a file in /etc/cron.allow and add users on there to whitelist them so they may use it. Then make a similar one in /etc/cron.deny which will deny cron privileges to whomever is attempting use it. Furthermore here's how! In this exercise, you apply some of the cron basics. You schedule cron jobs using different mechanisms:

1. Open a root shell. Type cat /etc/crontab to get an impression of the contents of the /etc/crontab configuration file.

From what I see using cat, this file has got a neat layout showing you how to determine times and dates.

2. Type crontab -e. This opens an editor interface that by default uses vi as its editor. Add the following line:
0 2 * * 1-5 logger message from root

By default this uses "vi", and it opened up crontab automatically.

3. Use the vi command :wq! to close the editing session and write changes.

Same save command to exit as vim.

4. Use cd /etc/cron.hourly. In this directory, create a script file with the name eachhour that contains the following line:
logger This message is written at $(date)

The file does not need an extension as long as it's executable, but as a norm use .sh

5. Use chmod +x eachhour to make the script executable; if you fail to make it executable, it will not work.

This makes it executable automatically, very important to note, instead of manually doing it.

6. Now enter the directory /etc/cron.d and in this directory create a file with the name eachhour. Put the following contents in the file:
11 * * * * root logger This message is written from /etc/cron.d

7. Save the modifications to the configuration file and go work on the next section. (For optimal effect, perform the last part of this exercise after a couple of hours.)

Hopefully it will work haha.

8. After a couple of hours, type grep written /var/log/messages and read the messages that have been written which verifies correct cron operations.

Can't really verify this, but it's probably true.
------------------------------------------------------------------------------------------------------------

Finally, let's talk about scheduling jobs with atd. This is another method of timing processes on your system. In this exercise, you learn how to schedule jobs using the atd service:

1. Type systemctl status atd. In the line that starts with Loaded:, this command should show you that the service is currently loaded and enabled, which means that it is ready to start receiving jobs.

It's enabled and displays that the job spooling tools are active and working properly.

2. Type at 15:00 (or replace with any time near to the time at which you are working on this exercise).

It changed the $ to at> which is also done by python. I probably need to type in a special command.

3. Type logger message from at. Use Ctrl+D to close the at shell.

This created a new job.

4. Type atq to verify that the job has indeed been scheduled.

This indeed scheduled a job. I believe that this tool atd is a really useful tool to shortcut editing and creating script files for cron, and simply typing in commands to atd.
------------------------------------------------------------------------------------------------------------

Review Questions

1. Where do you configure a cron job that needs to be executed once every 2 weeks?

You configure it at /etc/cron.weekly

2. How do you specify the execution time in a cron job that needs to be executed twice every month, on the 1st and the 15th of the month at 2 p.m.?

0 13 1,15 * *

3. How do you specify cron execution time for a job that needs to run every 2 minutes on every day?

*/2

4. How do you specify a job that needs to be executed on September 19 and every Thursday in September?

70 -1 19 sep 4*

5. Which three valid day indicators can you use to specify that a cron job needs to be executed on Sunday?

0, 7, or sun

6. Which command enables you to schedule a cron job for user lisa?

"crontab -e" then type "(job scheduling) lisa /cron.d"

7. How do you specify that user boris is never allowed to schedule jobs through cron?

echo boris >> /etc/cron.deny

8. You need to make sure that a job is executed every day, even if the server at execution time is temporarily unavailable. How do you do this?

Trick question

9. Which service must be running to schedule at jobs?

cron.d

10. Which command enables you to find out whether any current at jobs are scheduled for execution?

atq

5 comments:

  1. 2. No. it's 0 14 1,15 * *
    4. wtf? * * 19,* 9 thu
    8. add @reboot, so when server is up job will be done
    9. No, it's atd
    Cheers!

    ReplyDelete
    Replies
    1. The textbook I was learning this from sucked. I started using the official Red Hat book towards the end of my studies, and the explanations were more clear. Regardless, thanks for your feedback!

      Delete
  2. Lol, I was using Red Hat text book at first, but then I switched to Sender van Vugt cert guide. Official red hat guide is cool because it's quite direct and stright, but another one is way more comprehensive for me. Have you passed exam already? Sorry for my english. =)

    ReplyDelete
    Replies
    1. I tried it but didn't pass. It's a very difficult exam, and I had barely any knowledge of Linux back in January when I started studying for it. My teacher suggested this for me, and it completely put me off course of Network+ which I initially intended to get. Good luck on your test!

      Delete
    2. Thank you, good luck to you too! Can you remember any tasks from the exam? If you want I can share my experience about passing LPIC exams (1 and 2).

      Delete