Wise people learn when they can; fools learn when they must - Arthur Wellesley

Monday, 22 September 2014

Process Control CRON -- Solaris

                          
PROCESS CONTROL [cron]

Suppose u went to a place and stayed at hotel, u want that someone will wake u up at 5’O clock sharp daily and someone from laundry come at 7pm sharp, and someone will keep the dinner at your room 9pm sharp. Suddenly you remembered that after 3 days at 11am you have to meet somebody so you want this also to be recalled by someone.

Well… Now let’s explain this example…
Daily morning wake up / daily laundry / daily dinner at particular time… without fail
After 3 days want to remind … but this is only once

So what does it mean… it means that we can set particular tasks to be run daily ..once twice thrice or whatever.
We can set also onetime jobs… that will run and expire

The first one can done via utility CRON and the one timers can be done via utility AT

CRON…
5minute 10 hour 12th march Monday


Just remember this line … it will help u… believe me…

Cron is a utility that allows tasks to be run automatically in background at regular interval referring crontab file and with the help of crond daemon
** A daemon is a software process that runs in the background continuously and provides the
    Service to the client upon request
/etc/default/cron     default file to control global behavior of cron        

root@sol-test-2:>/# ls -l /etc/cron
cron    cron.d/
root@sol-test-2:>/# ls -l /etc/cron
lrwxrwxrwx   1 root     root          16 Aug 16 19:00 /etc/cron -> ../usr/sbin/cron


root@sol-test-2:>/# ls -l /etc/cron.d
total 6
prw-------   1 root     root           0 Aug 16 21:29 FIFO
-rw-r--r--   1 root     sys           40 Jun 28  2011 at.deny
-rw-r--r--   1 root     sys           40 Jun 28  2011 cron.deny
-rw-r--r--   1 root     sys           17 Jan 22  2005 queuedefs

So we can see that the directory for controlling cron is /etc/cron.d
Ok it is to control (allow / deny) cron / at users

But where can I find the crontab (cron table) … let’s find

root@sol-test-2:>/# ls -l /var/spool/cron/
total 4
drwxr-xr-x   2 root     sys          512 Aug 16 19:00 atjobs
drwxr-xr-x   2 root     sys          512 Aug 16 19:13 crontabs

Well here these directories are … crontabs & atjobs under /var/spool/cron/
These directories content the files related to the particular users

root@sol-test-2:>/# ls -l /var/spool/cron/crontabs/
total 10
-rw-------   1 root     sys          190 Jun 28  2011 adm
-r--------   1 root     root         452 Jun 28  2011 lp
-rw-------   1 root     sys          482 Aug 16 19:28 root
-rw-------   1 root     sys          308 Jun 28  2011 sys
-r--------   1 root     sys          404 Aug 16 19:09 uucp

root@sol-test-2:>/# ls -l /var/spool/cron/atjobs
total 0

root@sol-test-2:>/# ls -l /var/cron/
total 4
-rw-------   1 root     root        1460 Sep 11 19:11 log

This is the log file

Fine…Let’s start with /etc/cron.d

FIFO             FIFO is used as a lock to prevent running more than one instance of cron at a                             time.
Cron.allow     only the users listed in this file are allowed to create cron jobs
Cron.deny      all users are allowed to create cron jobs except users listed in this file
No file           only root can create cron jobs
queuedefs     Queue description file for at, batch, and cron

*cron.deny is created by default, if required then we need to create cron.allow
  If both file exists… cron.deny will be ignored

### Crontab uses default system editor, so better to export vi before editing###

Though we can use vi also to set cron …

root@sol-test-2:>/# vi /var/spool/cron/crontabs/root

1,2,3,4,5,20,21,22,23,24 * * * * ping 192.168.234.133 >>/tmp/myping.txt
[I made this entry in the file and]
root@sol-test-2:>/# svcadm restart cron

lets see the result
root@sol-test-2:>/# cat /tmp/myping.txt
192.168.234.133 is alive
192.168.234.133 is alive
192.168.234.133 is alive

It’s working… but we need to restart the service cron , I think which might be not advisable
Suppose a cron job is running and we restarted the service… then it will results in weird o/p{though I am thinking so… never tested this]

Better to follow the RULEBOOK…

Export the vi editor before editing cron jobs…

root@sol-test-2:>/# EDITOR=vi
root@sol-test-2:>/# export EDITOR
root@sol-test-2:>/# crontab –e
32,33,34,35,36,37 * * * * ping 192.168.234.133 >>/tmp/pingtest.txt
Save & quit

root@sol-test-2:>/# cat /tmp/pingtest.txt
192.168.234.133 is alive
192.168.234.133 is alive
192.168.234.133 is alive
192.168.234.133 is alive
192.168.234.133 is alive
192.168.234.133 is alive

Here it is not required to restart the cron service…

Ok… Have u remember… what I had written on very first page …

20minutes 11m 18th of july Friday

Wooohh… u scrolled and caught my cheat …

I had written something else on first page and else here…
I apologize … but it is just to remember … whatever u like …. Ur B’day … first date… or first time when u failed in class… or when u got a back in your engineering semester

No issues … just remember that in same manner
Minute – Hour – Date – Month - day  then what happened on that day…

Just it and your cron is done.

But how ?
Ok… let’s see…
root@sol-test-1:>/# cat /var/spool/cron/crontabs/root
10               3                  *                  *                  *                  /usr/sbin/logadm
Minute           Hour             Date             Month           Day
20mnts         11                18                July              Friday           Full Path of command
Min               hour             date             month           day of week
                                      Of month
OK…
Minute           0-59    (* means every minute)
What if I want to run the cmd on every 5 and 10 minutes  (5,10)
What if want to run the cmd on 0 and 30th minutes ,every half hour  (0,30)  or */2
What if I want that cmd will run after every 5 minutes …
(5,10,15,20,25,30,35,40,45,50,55,60)

Hour             0-23    (* means every hour)
18                simply run at 6pm
18,22            at 6pm and  at 10pm
0,12             at 12am and 12pm
9-17             every hour from 9am to 5pm

Date             1-31 (* means every day)
15                on 15th of <month>
1-10             on every 1st to 10th day of <month>


Month           1-12             (* means every month)
3                  in march
4,6               in april and june month

Day              0-6               (* means every day) [day starts from 0 i.e. Sunday]
3                  Thursday
0                              Sunday
0,5               on Sunday & Friday
1-5               from Monday to Friday

Command      Command or Script with full path


Some Example’s…

0,4,8,12,16,20 * * * * <cmd>
Every hour /every day/every month on each date at the given minutes

5 12 16 * 1
Each month on every Monday & 16th of every month at 12pm (noon) and 5 minutes
(B’coz month is * here) What if the entries are like…

5 12 16 3 1
It will run only when the 16th march will be Monday

00 11,16 * * *
On 0th minute of 11am and 4pm on every date of every month and full week (sun to sat)

= Each command within a crontab file must be on a single line, doesn’t matter how long it is.
= Lines starting with # are treated as comment lines and are ignored.
= All entries [min-hour-date of month-month-day of week] are space separated
= Use , to multiple values in one field
= Use – to give range in one field
= NO Blank lines between entries

To list the cron-jobs of current user
root@sol-test-2:>/# crontab –l


To list the cron-jobs of other user
root@sol-test-2:>/# crontab -l anurag
5,6,7,8,9,10,11 * * * * /usr/sbin/ping 192.168.234.133 >>/export/home/anurag/anurag.txt

TO remove crontab file of current user
root@sol-test-2:>/# crontab –r

TO remove crontab file of Particular user
root@sol-test-2:>/# crontab –r anurag

To Block particular cronjob
Just crontab –e then # that line

ACCESS CONTROL IN CRONTAB

/etc/cron.d/
Cron.allow   and    cron.deny

·         If cron.allow exists, only the users listed in this file can create, edit, display, and remove  crontab files.

·         Ifcron.allow doesn’t exists, all users may submit crontab files, except for Users listed in  cron.deny.

·         If neither cron.allow or cron.deny exists, root privileges are required to run crontab.

·         cron.deny is created by default, if required then we need to create cron.allow
 If both file exists… cron.deny will be ignored

The Logs
root@sol-test-2:>/# cat /var/cron/log
root 1819 c Fri Sep 19 18:20:00 2014
<  root 1819 c Fri Sep 19 18:20:00 2014
>  CMD: ping 192.168.234.133 >>/tmp/myping.txt
>  root 1822 c Fri Sep 19 18:21:00 2014
<  root 1822 c Fri Sep 19 18:21:00 2014
>  CMD: ping 192.168.234.133 >>/tmp/myping.txt
>  root 1830 c Fri Sep 19 18:22:00 2014
<  root 1830 c Fri Sep 19 18:22:00 2014



No comments:

Post a Comment