Monday, March 1, 2010

Debian 16

Runlevels are like distinct profiles. Runlevels range from 0-6, which are:0) Shut Down
1) Single-User Mode (No networking)
2) Multi-User Mode (Normal)
3-5) Multi-User Mode Alternatives
6) Reboot

Single-User mode is used for debugging, disk administration and updating the kernel.

Runlevels can be invoked using:
init #

You need to be a privileged user to run init. shutdown does init 0, but similarly you need to be root. The default runlevel can be found in:

The line id:2:initdefault: tells us that runlevel 2 is the default. Before booting into any runlevel, init first runs /etc/init.d/rcS which runs the first Run Control script.

If updates are made to /etc/inittab, you can update init through:
init -Q

/etc/init.d contains services/daemons that are run on the system. The Run Control scripts are referenced via /etc/rc# where # is for runlevel. Services in init.d tend to be BASH shell scripts with instructions on how to start, stop, reload, restart etc.

/etc/rc#.d contains symbolic links to services/daemons that are to be executed in the desired run-level. This directory contains K* and S* scripts. K* scripts stops services/daemons, while S* starts them. K* scripts are run before S*. Services are stopped or started in numeric order from low to high.

The K* or S* files are actually symbolic links of the script files found in init.d, and they are started or stopped based on K* or S*.

/etc/rc.local contain scripts that are to be run after any Multi-User mode. By default the script does nothing.

Cron scheduler lets you run tasks/jobs at predetermined times. Tasks can be per-user or system-wide. Cron is located in:

Cron can be run in all runlevels, and checks every minute for new jobs/schedules to execute. Cron can be used to run 'at' jobs, which executes at a specific time once. There are 4 classes for system-wide recurring jobs:

The system-wide cron table is at:

The per-user cron table is at:

There are 7 fields in the cron table:
1) Minute (m) 0-59
2) Hour (h) 0-23
3) Day of the Month (dom) 1-31
4) Month (m) 1-12
5) Day of the Week (dow) 1-7
6) User - The user to run the job as
7) Command - The full syntax for the command

To extract the date, use:
date +%Y%m%d

To create a script that runs every 5 minutes putting the load averages into a file named after the date, create a script like this:

uptime | awk '{print $8,$9,$10,$11,$12}' >> `date +%y%m%d`.txt

After setting the execute flag, it's time to add per-user jobs. If nano is not the default editor, use:
export $EDITOR=nano

Now, execute crontab to add per-user jobs:
crontab -e

That would run the default editor (which is set to nano now). To view a user's jobs, use:
crontab -l kelvin

To make it run every 5 minutes, use */5 for the minute field. We should have this in the crontab -e:
*/5 * * * * uptime | awk '{print $8,$9,$10,$11,$12}' >> `date +%y%m%d`.txt

To specify that the job should be run on specific minutes like:

You can also specify a range, like:

You can also specify an interval which we used above, like:

Cron supports any type of executable for a job including BASH, Perl, PHP, Python, etc.

Notice that in the /etc/crontab file, there is a reference to anacron. anacron runs programs that were not run either because of system failure or because the computer was not on at the time. The Administrator can also add system-wide tasks in the /etc/crontab file.

No comments :

Post a Comment