Scheduling processes Part 2
This chapter discusses scheduling in Linux.
anacron
We have seen how handy "cron" is at scheduling jobs. However there is a slight shortcoming to "cron" . Suppose we need to run something on a weekly basis and we schedule it for Monday morning 9am. At around 8am on Monday morning a storm comes and brings down the power. The power is restored at 10 am and cron does not run the job because the time has passed. This is where "anacron" comes in. It will run the job if the job was missed in the past. It is not a daemon but a program and works together with cron. The entries are kept in the file "/etc/anacrontab". However we do not have access to modify this file. We can take a look at the contents on the hills server.# /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthlyThe entry consists of 4 fields. The first is the period in days. The "anacron" program does not have finer granularity. The second is minutes to wait for before running the job. The third field is the job name and the fourth is the command itself.
If the system is up then cron runs the job and informs anacron with the command
"anacron -u jobname" . If however the system was down then when it comes up "anacron"
will check the folder "/var/spool/anacron" for the timestamps of the jobs. On our
hills server the folder looks like:
If it finds that the job has not been run then it will wait for the delay specified in
the third fields , run the job and update the timestamp of the job.
nice
We can change the priority of a process with the "nice" command. This gives a hint to the CPU as to how much time should be spent on this process. We can see the priority of our currnet processes as below:[amittal@hills crontab]$ ps -el | more F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 1 0 0 80 0 - 62552 - ? 00:28:29 systemd 1 S 0 2 0 0 80 0 - 0 - ? 00:00:08 kthreadd 1 I 0 3 2 0 60 -20 - 0 - ? 00:00:00 rcu_gp 1 I 0 4 2 0 60 -20 - 0 - ? 00:00:00 rcu_par_ gp 1 I 0 6 2 0 60 -20 - 0 - ? 00:00:00 kworker/ 0:0H-events_highpriThe value appears under the "NI" column. Unfortunately we don't have permission to set the nice values for our processes on the hills server. The higher positive values means a higher priority. The command "renice" can change the priority of an existing process.
nice -10 gnome-terminal Negative value nice --10 gnome-terminal change priority of an existing process sudo renice -n 15 -p 77982
run-parts
We noticed the phrase "run-parts" in the file "/etc/anacrontab" in the section on anacrontab. The "run-parts" is a command that can be used to run scripts in a folder, one by one. Create a folder ( mine is runparts1 ) and place the following 2 scripts in it.[amittal@hills runparts1]$ cat 1.sh echo "1.sh" >> "/users/amittal/cs260a/crontab/runparts1/1.txt" [amittal@hills runparts1]$ cat 2.sh echo "2.sh" >> "/users/amittal/cs260a/crontab/runparts1/1.txt" Change the full path of "1.txt" to be in the path of your new folder. Change folder to 1 level above runparts1 and run the command "run-parts" . [amittal@hills crontab]$ run-parts runparts1/ [amittal@hills crontab]$ cat runparts1/1.txt 1.sh 2.sh [amittal@hills crontab]$ We print the contents of "1.txt" to show that all the scripts in that folder have been run.The "/etc/" has 3 folders: "/etc/cron.daily", "/etc/cron.weekly" , "/etc/cron.monthly" . As their name implies they run the scripts daily, weekly and monthly in their respective folders. This is a common scheme used in Linux systems. Now if we have a script that needs to runj daily we don't create an entry in the "anacrontab" file but instead drop the script in the "cron.daily" folder.
at
The utility "cron" and "anacron" allow us to execute a job at regular intervals. What if we wanted to execute a job only once. The utility "at" can be used for such a purpose. Example:Make sure at daemon is running ( ayd ) . [amittal@hills crontab]$ ps -aef | grep "atd" root 1642 1 0 2022 ? 00:00:00 /usr/sbin/atd -f amittal 2909494 2233539 0 05:49 pts/1 00:00:00 grep --color=auto atd [amittal@hills crontab]$ at -f sample.sh 07:00 warning: commands will be executed using /bin/sh job 10 at Wed Jan 4 07:00:00 2023 [amittal@hills crontab]$ ls -ltr total 20 -rw-r--r-- 1 amittal csdept 52 Dec 31 17:37 notes.txt -rwx------ 1 amittal csdept 44 Dec 31 19:53 cp_sample.sh -rwxrwxrwx 1 amittal csdept 225 Jan 2 07:27 sample.sh -rwxrwxrwx 1 amittal csdept 30 Jan 2 18:45 sleep.sh drwx------ 2 amittal csdept 70 Jan 3 22:48 runparts1 -rw------- 1 amittal csdept 145 Jan 4 07:00 1.txt [amittal@hills crontab]$ cat 1.txt Wed Jan 4 07:00:00 PST 2023 /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/users/amittal/.local/bin:/users/amittal/bin /users/amittal [amittal@hills crontab]$The command "at -f sample.sh 07:00" tells "at" to run the script "sample.sh" at 7:00 am. The "at" command can also control the jobs. We can list the jobs using the "jobq" or "job -l" command. After listing the jobs ( that are in the future... will not list past jobs ) we can choose to delete a job also with the "job -r" command.
[amittal@hills crontab]$ at -f sample.sh 19:40 warning: commands will be executed using /bin/sh job 12 at Wed Jan 4 19:40:00 2023 [amittal@hills crontab]$ atq 11 Thu Jan 5 07:00:00 2023 a amittal 12 Wed Jan 4 19:40:00 2023 a amittal [amittal@hills crontab]$ at -l 11 Thu Jan 5 07:00:00 2023 a amittal 12 Wed Jan 4 19:40:00 2023 a amittal [amittal@hills crontab]$ at -r 12 [amittal@hills crontab]$ atq 11 Thu Jan 5 07:00:00 2023 a amittalThe "at" command can take the time in different formats:
YYMMDDhhmm[.ss]. Specify an abbreviated year, month, day, hour, minute, and optionally seconds. CCYYMMDDhhmm[.ss]. Specify a full year, month, day, hour, minute, and optionally seconds. now. Indicates the current day and time and immediate execution. midnight. Indicates 00:00 AM. noon. Indicating 12:00 PM. teatime. Interpreted as 4 PM. AM. Indicates time before 12:00 PM. PM. Indicates time after 12:00 PM. today. The current day. tomorrow. The day after the current day.Some examples executed on the hills server:
Time already past . Assumes next day. [amittal@hills crontab]$ at -f sample.sh 07:00 warning: commands will be executed using /bin/sh job 14 at Thu Jan 5 07:00:00 2023 [amittal@hills crontab]$ at -f sample.sh -t 2304011950 warning: commands will be executed using /bin/sh job 16 at Sat Apr 1 19:50:00 2023 [amittal@hills crontab]$ at -f sample.sh now warning: commands will be executed using /bin/sh job 17 at Wed Jan 4 19:53:00 2023
TO DO 1) Schedule a script to run at 8:30 pm today. If it's past 8:30 pm then the script should run next dat. Use both "PM" and "20" hour notation to schedule 2 jobs. 2) Schedule a script to run at 1:00 am on April 15th 2024 . 3) List the jobs and then delete the jobs.
at access
Access is done the same way as with cron. There are 2 files "at.allow" and "at.deny".
On the hills server the file "at.allow" does not exist and the "at.deny" is empty.
That means all users can use "at" .