Sometimes it is necessary to have tasks execute at specific times. Automating tasks to run at specific times can be a very necessary administrative function. Even on a home system tasks can be automated to reduce your time from ‘babysitting’ your system.
There are a few ways to automate a single instruction or set of instructions. One way may be more beneficial to run specific tasks. The way you use to schedule tasks will mainly depend on when you want the task run.
The three ways to schedule tasks are:
If you have a repetitive task that needs to be executed at the same specific time then you should use CRON. CRON allows you to schedule tasks at a specific time and day, say 1:00 P.M every Monday. Tasks can be set to execute at specific times every day such as 15 minutes past every hour from 8 A.M. to 5:00 P.M. Tasks can be scheduled by the minute, hour, day, week and month.
The file to change the CRON jobs is located in ‘/etc’ and called ‘crontab’. Looking at the ‘/etc’ folder and running ‘ls cron*’ you will see results similar to Figure 1. If you edit the ‘crontab’ file using a text editor, you will see something similar to Figure 2.
If you want to edit the ‘crontab’ file, you must have Root privileges.
Let’s look at the ‘crontab’ file in a little more detail as it exists before jobs are added (as shown in Figure 2).
The first five lines are commented and should be left alone. The next two lines, with information, are ‘shell’ and ‘path’ statements needed by the shell. The next commented line shows the format of how we set up each line for a scheduled task. The next four lines are used to run the CRON jobs which are set hourly, daily, weekly and monthly, which are shown in Figure 1.
When you add a CRON job, you add a new line after the four existing lines used to run CRON jobs. The format is minute, hour, day of month (dom), month, day of week (dow), user and finally the command to execute.
The values are as follows:
- Minute – 0-59
- Hour – 0-23 (military time)
- Day of Month (dom) – 1-31
- Month – 1-12
- Day of Week (dow) – 0-7 (0 and 7 are Sunday)
- User – user name privileges with which to execute the command
- Command – the command or script to execute
Once you have entered the proper information, you can save the file.
It is possible to create a specific ‘crontab’ per user by using the command ‘crontab -e’. You will be prompted to select an editor from the list given. Once done, you can edit the ‘crontab’ and save it. The new ‘crontab’ file will be saved in ‘/var/spool/crontabs/’ with the name matching your username. Multiple files can be found, one for each user that creates one.
NOTE: The ‘cron’ daemon checks all the ‘cron’ files once every minute to check for commands which need to be ran. Also, any time missing because of daylight savings will be skipped. If a schedule is made to run on February 29th, then the job will run once every four years.
If you want to remove your personal ‘crontab’ then run the command ‘crontab -r’.
When using numbers you can use a range. Two numbers separated by a dash, no space, can specify the range. For example, you want a task to run between the hours of 10 A.M to 2 P.M. The range would be 10-14. You can also split up multiple ranges by using a comma. For example, if you want the schedule to be 9 A.M. to 11 A.M. and 2 P.M. to 4 P.M., then the range would be 9-11,14-16.
If the range includes all of the allowed range, such as 0-23 for the hours, then you can use an ‘*’ to represent all the allowed numbers.
If you want to represent every second hour of the whole range you can use the value ‘*/2’. Every third hour between 8 A.M. and 5 P.M. would be ‘8-17/3’.
By default, in most cases, all users can use CRON jobs. If you want to change this on a user-by-user basis then you can create the files ‘/etc/cron.allow’ and/or ‘/etc/cron.deny’. These files contain the usernames that are either allowed or denied from using personal CRON jobs.
Keep in mind that multiple commands can be used in the ‘command’ section of CRON. You can separate the commands with a percent sign (%) which is interpreted as a newline character. In some cases, it may be best to specify a shell script that can be edited independently of CRON.
Anacron is similar to CRON, but the times scheduled for Anacron are determined by the system being powered on.
For instance, by specifying a time of ‘45’ would cause the task to run 45 minutes after the system was powered on.
Anacron tasks are set in the file ‘/etc/anacrontab’. By default, the file looks like Figure 3.
The first four lines with information set up how anacron is configured to use the system. Note that the scripts are Shell and not Bash. The last three lines are important to understand.
The first ‘1’ designates the line is executed every day that the system is on and runs five minutes after the system is turned on. The command which is executed is ‘run-parts’. The command runs all of the executable files within the folder specified, which is ‘/etc/cron.daily’. The other parameters are used by the ‘run-parts’ command.
The next line is run every 7 days (weekly) and executes all of the executable files in the folder ‘/etc/cron.weekly’.
The last line is executed every month (@monthly). Things can get strange if you specify ‘30’ since eventually a task could be executed twice in a month, or even skip a month (February).
You can also use the keywords: @daily, @weekly, @monthly and @yearly. You can find out the day on which the specific task was executed by the command ‘sudo cat /var/spool/anacron/cron.daily’. The result may be like ‘20200827’, which is the year 2020 and month 08 and the day is the 27th. The values can be also be found in the files ‘cron.weekly’ and ‘cron.monthly’. The files can be found with the command ‘ls /var/spool/anacron’.
For irregularly scheduled jobs you can use ‘at’.
The ‘at’ command is installed in CentOS by default, but not in Ubuntu. To install ‘at’ in Ubuntu, you need to run the command ‘sudo apt install at’ from a Terminal. You can check it by the command ‘systemctl status atd’ to verify the ‘at’ daemon is running. To start the daemon you can use the command ‘sudo systemctl enable –now atd.service’. If for any reason ‘at’ is not installed CentOS, you can install it with the command ‘yum install at’.
The ‘at’ command is run as ‘at time day’. Time can be specified as a specific time, using AM or PM. With the AM and PM removed then the time is considered Military Time. The day can be a specific day of the week (mon, tue, wed, thu, fri, sat or sun). The ‘day’ can also specify a month (Jan, Feb, Mar, etc.). If a month is specified alone then the current day is used as the day for the month. You can also use the following keywords:
- noon (12 P.M.)
- midnight (12 A.M.)
- tomorrow (24 hours)
- teatime (4 P.M.)
- next week (7 days)
- date (such as 11/23)
- date (Nov 23)
- now + 5 minutes (5 minutes from now)
- now + 3 days (3 days from now)
- now + 5 weeks (5 weeks from now)
The keywords can also be combined, such as, ‘teatime tomorrow’, or even ‘noon next week’. By adding a specific time you could have ‘1:10 PM next week’.
Once you enter a valid time and date you will be given a prompt ‘>’. Here you will enter all of the commands you want to be executed in the order they are entered. When your list of tasks is completed, then you type CTRL+D to end the listing.
Any task you will run which does not need an Interface, such as using ‘echo’ commands to write to a file, can be done normally. If you want to open a GUI-based program, then you need to specify a display. The typical default display is ‘:0’. To verify the display. You run the command ‘echo $DISPLAY’ and the result is what you use. For any task needing a GUI, you start with the command ‘DISPLAY=:0.0’, or change the number to the one you received from the ‘echo’ command.
Let’s try an example task to start ‘leafpad’ if it is installed on your system. Otherwise, use a different program. Perform the following after you have ‘at’ installed:
- at now + 2 minutes
- > DISPLAY=:0.0
- > leafpad
Wait two minutes and see that the task is open and running.
Each schedule you set is given a job number. To see the list of jobs still left to be executed you can run the command ‘atq’. The ‘atq’ command will show the ‘at’ queue with a job number. To view the commands for the specific job you can run the command ‘at -c #’, where ‘#’ is the job number.
If you want to remove a job, you use the command ‘atrm #’ where ‘#’ is the job number.
These methods to automate tasks can be a very useful means to free up some time for you. Any repetitive task can easily be scripted and automated.
Make sure you truly understand the use of these commands. Once you do, I’m sure you will find ways to automate things to work better for you.