The Linux Schedulers – cron – cronology – sequence
| June 8, 2012 | Posted by kaswan under linux |
CRON-ANACRON
System Administration
============
The Linux Schedulers – cron – cronology – sequence
cronological order – date-wise
=================
A. System crontab
=================
crond* —–> Binary or App server daemon
/etc/rc.d/init.d/crond —–> Initscript to start crond server
/etc/crontab —–> System crontab file
————
/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly
/etc/crontab
============
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
mins hrs DOM MOY DOW
00-59 00-23 1-31 1-12 0-7 0=7=Sun 1=Mon,..
Each of the time-related fields may contain :
* A ‘*’, which matches everything, or matches any value
* A single integer, which matches exactly
* Two integers seperated by a dash, matching a range of values ie
8-10 in the hr field would match 8am,9am and 10am.
|||’ly, 8-10,13 would match 8am,9am,10am and 1pm
* A comma-seperated series of ints or ranges, matching any listed value
* */2 in the hr field refers to midnote, 2am, 4am and so forth
ie the cmd is executed every 2 hrs
* 0-10/2 in the hr field refers to midnite, 2am, 4am, 6am, 8am and 10am
Note :
- A crontab entry is considered to match the current time when the min
and hr fields match the curr time and the mth field matches the curr
month
- An entry is considered to match the current date when the day of mth
field [3rd] matches the current day of the mth OR the day of week [5th]
field matches the current day of the week :
IT IS NOT NECESSARY THAT BOTH THE DAY OF THE MTH AND DAY OF THE
WEEK MATCH!
- If both the time and date match the current time and date the cmd
is executed !
Note :
Never put a ‘*’ in the first field unless u want the cmd to run every
minute
Note : You MAY hand-edit this file but it is never necessary since run-parts
does everything. Simply put a shell script in the appropriate
/etc/cron.*/ dirs
Also the crond* daemon need not be restart. It will do just that
every minute anyway
Example : Users often forget to shutdown their machines and go home
Hence, machine should auto shutdown at 11 pm
/etc/crontab
============
00 23 * * * root /sbin/shutdown -h now
============================
B. Customized System crontab
============================
/etc/cron.d/ SU’s customized crons and 3rd party crons
Example :
# Create an exec shell script : to_foo.sh
* * * * * mail -s “Hi This is the password file” foo < /etc/passwd
Now login as foo and access the mail. EVERY MINUTE !
And that too the system passwd file!
Exercise: Log in as ‘foo’ and define a cron job that runs the ‘w’ cmd
every midnite. Check your mail in the morning !
===============
C. User crontab
===============
* /var/spool/cron/$USER
* crontab -e, -l, -r
Note : Never hand-edit this crontab file. Always use crontab -e
As root : crontab -u foo -e
Exercise: Log in as ‘foo’
# crontab -e
* * * * * echo I love Linux
# crontab -l
Now check you mail every min by using mail* and what do u see ?
====================
D. cron access perms
====================
4. /etc/cron.allow and /etc/cron.deny
If a user is only in /etc/cron.allow, then all others are denied
If a user is only in /etc/cron.deny then all others are allowed/not affected
If cron.deny is touched, then no users is allowed to create a crontab
If cron.allow is touched, then no users is allowed to create a crontab
=======
Part II
=======
=======
anacron
=======
* procinfo* can tell one when the system was booted up
1. anacron* is not as smart as crond
2. Must be started by hand and IS – by /etc/rc.d/init.d/anacron
and does not run continuously as a daemon
3. Cannot schedule jobs at intervals that are less than a day
4. Cannot guarantee that a job will run
anacron*
/etc/rc.d/init.d/anacron
/etc/anacrontab
/var/spool/anacron
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
1 65 cron.daily run-parts /etc/cron.daily
7 70 cron.weekly run-parts /etc/cron.weekly
30 75 cron.monthly run-parts /etc/cron.monthly
1. period : which specifies the execution interval in days
2. delay : which specifies a delay in mins before the job is started
3. job id : string which uniquely identifies the anacron job
4. ccmd : cmd to be executed
========
Part III
========
=================
The watchdog logs
=================
=========================
A. last* – /var/log/wtmp
=========================
- Login history of all users – multiple user entries
last [-i/-x] username
# last foo –> Display login history of foo
# last -x –> Display the system shutdown entries/run level changes too
last -x | grep system
# last -i –> Displays remote login history too
==============================
B. lastlog* – /var/log/lastlog – Last login of user – single entries per user
==============================
- Last login of user – single entries per user
# lastlog -u foo –> Display the last login of foo
[Don't you see this when u login ?]
==============================
C. lastb* – /var/log/btmp – Bad Login history log
==============================
Note: /var/log/btmp does not exist by def. Use touch to create first!
Now do some bad logins by using a VLU but wrong password and check what
lastb* has to say after that
————————————————
HOMEWORK
————————————————
Example : Checking whether mail server is up or down and getting a report
on the screen, if down
1. Create the following cron job script
/etc/cron.d/mailcheck*
=====================
* * * * * root /usr/local/sbin/ismailup
2. Create the following file
/usr/local/sbin/ismailup*
========================
#!/bin/sh
a=ps -el | grep sendmail |head -n 1
if [ -z "$a" ]
then
write root < /usr/local/sbin/mailnok
else
write root < /usr/local/sbin/mailok
fi
3. Now create the following files :
/usr/local/sbin/mailnok
=======================
Mail server is down !!
/usr/local/sbin/mailok
======================
Mail server is up
All okay
Examples :
1. echo The time is now date > /dev/console
2. write foo % Hello foo % Remember to call home
3. cd /etc; /bin/mail -s “Password file” foo < passwd
Example :
I want to be reminded to phone home everyday at 6pm
The message should come on the screen and not by mail
# crontab -e
00 18 * * * echo Hello > /dev/console
Observations :
Examine the following canned RH file : /etc/cron.daily/tmpwatch
It is a daily cron job
/usr/sbin/tmpwatch 240 /tmp <– Dele all file in /tmp not accessed
in the last 240 hrs or 10 days
/usr/sbin/tmpwatch 720 /var/tmp <– So……
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
if [ -d "$d" ]; then
/usr/sbin/tmpwatch -f 720 $d
fi
done
Isn’t the following shell script loop instructive !
================
CRON-ANACRON
================
1. The system crontab file is :
A: /etc/crontab
2. Which daemon should be started to read this file ?
A: crond
3. When ?
A: At sysstartup. Use ntsysv
4. Where is the cron startup script ?
A: /etc/rc.d/init.d/crond
5. What does ‘cron’ mean ?
A: cronology
6. Which dirs does the system crontab drive the crond daemon to read ?
* /etc/cron.hourly
* /etc/cron.daily
* /etc/cron.weekly
* /etc/cron.monthly
7. How many time-related fields are there in the system crontab file?
A: 5
8. What are they ?
The time and date fields are:
field allowed values
—– ————–
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (0 or 7 is Sun, or use names)
9. Where would you put a customized script to run as a system cron job?
A: /etc/cron.d/
10 Which script accepts a dir as an arg and executes all scripts in that dir ?
A: run-parts
11 Explain the following :
5 0 * * * some job
A: Run the cmd 5 mins after midnite
12 Explain the following :
30 4 1,15 * 5 some command…..
A: Cmd will be run at 4:30 am on the 1st and 15th of each month, plus
every Friday.
13 Explain the following :
15 14 1 * * $HOME/bin/monthly.payslip.printing
A: Run at 2:15pm on the first of every month
14 Explain the following :
45 16 * * 5 echo hello
A: Run the cmd at 16:45 every Friday
15 Explain the following :
45 16 * */3 5 echo hello
A: Run the cmd at 16:45 on every Friday, but every 3rd month
i.e every Fri in Jan, Apr,…
16 Explain the following :
23 0-23/2 * * * some command
A: Run the cmd 23 minutes after midn, 2am, 4am …, everyday
17 Explain the following :
0 22 * * 1-5 some-command
A: Run at 10pm on weekdays.
18 Explain the following :
5 4 * * sun
A: Run at 5 after 4 am every sunday
19 Explain the following :
0,30 * 13 * 5
Ambiguity : Watchout for ‘weekday’ and ‘day’ fields. Everyday is both a day of
the week and also a day of the month. If both ‘weekday’ and ‘day’
are specifed, as above, a day need satisfy only one of the two
conditions in order to be selected.
Remember : ‘day of the mth’ and ‘weekday’ fields both will match. Beware !
A: Run every half-hr on Fri, AND every half-hr on the 13th of the month
NOT
“Every half-hr on Fri the 13th”
20 Explain the following :
20 1 * * * find /tmp -atime +3 -exec rm -i {} ‘;’
A: Run at 1:20 am each morning and removes all files in the /tmp dir
that have not been accessed in 3 days.
21 Explain the following :
55 23 * * 0-3,6 find /tmp -atime +3 -exec rm -i {} ‘;’
A: Does the same as above, but runs at 23:55 every day except Thu/Fri
22 Explain the following :
1 0 * * *
find / -xdev -atime +3 ‘(‘ -name ‘#’ -o -name ‘.#’ -o -name ‘*.CKP’
-o -name ‘*~’ -o -name ‘.nfs’ ‘)’ -exec rm -fr {} ‘;’
A: At one past midnite, every day, all files beginning with #, .#, or .nfs
or end with ~ or .CKP AND that have not been accessed in 3 days will
be deleted. Also stick to the / filesystem and do not cross over to
other foreign file systems viz. [NFS, SMBFS etc]
23 Explain the following :
1 0 * * *
cd /tmp; find . ! -name lost+found -type -d -mtime +3 -exec rm -f {} ‘;’
A: At one past midnite, every day, remove all subdirs of /tmp which have
not been accessed in the last 72 hrs. But do not remove the dir
lost+found in /tmp [which will exist iff /tmp is a seperate FS and
it most always IS.]
Files are normally cleared from /tmp by the system startup scripts,
but subdirs created here stubbornly remain.
24 What command would foo use to create her own crontab?
A: crontab -e
25 Where would you find foo’s crontab file ?
A: /var/spool/cron/foo
26 What command would foo use to modify her crontab?
A: crontab -e
27 What command would foo use to list her crontab?
A: crontab -l
28 What command would foo use to delete her crontab?
A: crontab -r
29 How would you prevent user foo from creating her own personal crontab file ?
A: Use /etc/cron.deny
30 Briefly explain the purpose and necessity of anacrontab?
A: Missed jobs are executed by anacron
31 The Any time crontab file is :
A: /etc/anacrontab
32 Which daemon should be started to read this file ?
A: anacron
33 When ?
A: At sysstartup. Use ntsysv
34 Where is the anacron startup script ?
A: /etc/rc.d/init.d/anacron
35 I would like to know the detailed login / logout history of user ‘foo’ ?
A: last* foo
36 Which db stores all this info which the ‘last’ cmd decodes and reads ?
A: /var/log/wtmp
37 Which command would show a log of all reboots and shutdowns ?
A: last* -x
38 Which db stores the info of all bad logins ?
A: /var/log/btmp
39 And how would you read this encoded db ?
A: lastb*
40 The lastb cmd does not seem to work ?
A: Create it with ‘touch /var/log/btmp’, do some bad logins on purpose,
and then run the ‘lastb’ command to check
41 Which command is used to see the last time ‘foo’ logged in ?
A: lastlog
42 But this shows the last time every user logged in. I wish to only see
foo’s last login?
A: lastlog -u foo
43 Which db stores this info ?
A: /var/log/lastlog
44 I want to know all users who logged in the last 4 days ?
A: lastlog -t 4
45 So what is the difference between ‘last’ and ‘lastlog’?
A: lastlog* shows only the last previous login.
[A username will never be repeated here]
last* shows the entire login history of the user.
***




Recent Comments