2014年9月14日 星期日

Linux Cron

linux-cron

Linux Scheduled Jobs is scheduler system know as Cron that allows system administrators to automate rescurring administrative tasks or users to automate their routine works.



There are 2 types of Scheduled Jobs:

1. User Scheduled Jobs - Which is set by a non-root user and runs under a non-root user's privilege.
2. System Scheduled Jobs - Which is set by root and runs under Super User's Privilege.

The scheduler system includes the following components.
1. The Cron Daemon - crond(8)
2. The Configuration file - /etc/crontab (5) and /var/spool/cron/*
3. The Command - crontab(1)

The Cron Daemon - crond(8)

The Cron Daemon is the primary server service for all types of scheduled jobs. For every minute, it reads all configuration files including system scheduled jobs from 'etc/crontab' and all users scheduled jobs from 'var/spool/cron/*. If it finds jobs that needs to be run at that time, it will run them.

Since the above operations is performed every minute, therefore the unit of time in the configuration is in minute. In other words, you can schedule a job to run every minute but not every second.

The Configuration files

The configuration file 'etc/contab' is used for storing system scheduled jobs and can be edited directly by SuperUser only.

The configuration file under the directory '/var/spool/cron' are used for storing users' scheduled jobs.
For example :
The scheduled jobs for the user peter are stored in the file /var/spool/cron/peter.
The scheduled jobs for the user ada are stored in the file /var/spool/cron/ada.

However, users can only edit them through the command 'crontab' and they only edit their own file.

The Crontab files
Min Hour Day Month Weekday RunAs (/etc/crontab only)  Command(Action)
* * * * * root command i.e. every minute
0 * * * * root command i.e. every hour
0 12,13 * * * root command i.e. 12:00 13:00
15 2 1 * * root command i.e. 1st  day of every month at 2:15
15 4 */3 * * root command i.e. every 3 days at 4:15

Scheduling User CronJobs

For users to maintenance their scheduled jobs, they need to use the 'crontab' command. There a few options available in the 'crontab' command:

crontab -e to edit user's own cronjob  // crontab -u peter -e  <-- Root
crontab -l to list the usr's own cronjob // crontab -u peter -l <-- Root
crontab -r to remove the usr's own cronjob // crontab -u peter -r  <-- Root

e.g. peter edit his own job

crontab -e
* * * * * date >> /home/peter/datafile
ESC :wq

The cron job file for Peter is created but Peter cannot view it directory.
Normal users has no right in accessing the directory '/var/spool/cron' and can only access it through 'crontab' command what has SetUID bit configured.

To view his own cronjob:  crontab -l

ps -fC cron(d)
chkconfig --list cron(d)
chkconfig corn(d) status
chkconfig --level 2345 cron(d) on
service cron(d) restart

Scheduling System Cronjobs

vi /etc/contab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * * root run-parts /etc/cron.weekly
41 1 1 * * root run-parts /etc/cron.monthly

Actual 'run-parts' is a script that reads and run the script file in the specified directory (i.e. '/etc/cron.daily').

Note that all entries use different values in the minute column. If not, jobs from diffrent directories will be started at the same time. These will seriously affect the performance of the Linux machine durring that period.


In order to configure system cron jobs, you can

Create an entry directly in the /etc/crontab file e.g.

* * * * * root date>> /root/datefile
 or

Prepare a script file and then put it into the appropriate directory. e.g. for a script file that needs to be run monthly, put it into the directory '/etc/cron.monthly'.


































沒有留言:

張貼留言