Lecture 03/03/2004
Startup and Run Levels.
Scheduled jobs.
I. Bootup and Login Sequence
II. System Run Levels
III. Startup Scripts
IV. Scheduled jobs (at, cron)
V. Introduction to shell scripting (tutorials)
------------------------------------------------
I. Bootup and Login Sequence
1. BIOS boot-ups; checks peripherals; finds bootable device.
2. First sector loaded into RAM and executed-- (LILO, GRUB, NT loader): prompt appears.
3. Kernel loaded from sector list, /boot/vmlinuz-2.4.x-xx;
places the appropriate initial RAM disk image, initrd, into RAM.
4. Kernel executed; unpacks.
5. Kernel initializes hardware.
6. Kernel mounts root, /, file system, say /dev/hda2 or /dev/hda8.
7. Kernel executes /sbin/init as PID 1.
8. init executes /etc/rc.d/rc.sysinit script, which sets environment path,
starts swap, checks the file systems, and so on;
then init executes all scripts for default run level,
defined in /etc/inittab; then it runs /etc/rc.d/rc.local.
9. init spawns getty programs on each terminal.
10. getty prompts for login.
11. getty executes /bin/login to authentic user.
12. login requests a password and validates it against /etc/passwd or NIS maps.
13. login prints the message from /etc/motd.
14. login starts a shell.
15. The shell executes the appropriate startup files
(.profile for sh, ksh; .bash_profile for bash;
.cshrc and .login for csh and tcsh).
16. The shell prints a prompt and waits for input.
When the user logs out, control returns to init, which wakes up and spawns
a new getty on the terminal port.
[alexei@capone alexei]$ pstree
init-+-atd
|-crond
|-dhcpd
|-getty
|-identd---identd---3*[identd]
|-inetd---in.rlogind---login---bash---pstree
|-keventd
|-kflushd
|-klogd
|-kswapd
|-kupdate
|-mdrecoveryd
|-nfsd---lockd---rpciod
|-7*[nfsd]
|-portmap
|-rpc.mountd
|-rpc.rquotad
|-rpc.statd
|-sshd2
|-syslogd
`-xfs
Command /usr/bin/pstree comes with package
psmisc
------------------------------------------------------------
II. System Run Levels
init executes all scripts for default run level.
0 Halt.
1 Single-user mode.
2 Multiuser, without network file system (NFS).
3 Full multiuser mode.
4 Unused.
5 X Window System Workstation (usually identical to run level 3).
6 Reboot.
7 Undefined.
8 Undefined.
9 Undefined.
/etc/inittab
Entries in /etc/inittab take the following format:
id:run-level:action:process
id: a unique identifier
run-level: 0 -- 6 (for RH linux)
action:
sysinit - run process at bootup
once - run process once upon entering specific runlevel
respawn - restart process whenever it is terminated
wait - like "once", but awaits completion of process before continuing
Content of the file:
# Default runlevel. The runlevels used by RHS are:
# 0 - halt (Do NOT set initdefault to this)
# 1 - Single user mode
# 2 - Multiuser, without NFS (The same as 3, if you do not have networking)
# 3 - Full multiuser mode
# 4 - unused
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
# System initialization.
si::sysinit:/etc/rc.d/rc.sysinit
l0:0:wait:/etc/rc.d/rc 0
l1:1:wait:/etc/rc.d/rc 1
l2:2:wait:/etc/rc.d/rc 2
l3:3:wait:/etc/rc.d/rc 3
l4:4:wait:/etc/rc.d/rc 4
l5:5:wait:/etc/rc.d/rc 5
l6:6:wait:/etc/rc.d/rc 6
# Things to run in every runlevel.
ud::once:/sbin/update
# Trap CTRL-ALT-DELETE
ca::ctrlaltdel:/sbin/shutdown -t3 -r now
# When our UPS tells us power has failed, assume we have a few minutes
# of power left. Schedule a shutdown for 2 minutes from now.
# This does, of course, assume you have powerd installed and your
# UPS connected and working correctly.
pf::powerfail:/sbin/shutdown -f -h +2 "Power Failure; System Shutting Down"
# If power was restored before the shutdown kicked in, cancel it.
pr:12345:powerokwait:/sbin/shutdown -c "Power Restored; Shutdown Cancelled"
# Run gettys in standard runlevels
1:2345:respawn:/sbin/mingetty tty1
2:2345:respawn:/sbin/mingetty tty2
3:2345:respawn:/sbin/mingetty tty3
4:2345:respawn:/sbin/mingetty tty4
5:2345:respawn:/sbin/mingetty tty5
6:2345:respawn:/sbin/mingetty tty6
# Run xdm in runlevel 5
# xdm is now a separate service
x:5:respawn:/etc/X11/prefdm -nodaemon
Changing the Run Levels:
/sbin/telinit N
or
/sbin/init N
where N is a new run level.
/sbin/init 0 is the same as
shutdown -h now
/sbin/init 6 is the same as
shutdown -r now
Booting into different Run Levels, N:
At LILO prompt:
linux N
linux single is the same as linux 1
At GRUB prompt, type a (stands for append), then add N after the prompt:
grub append> ro root=LABEL=/ N
grub append> ro root=LABEL=/ single is the same as
grub append> ro root=LABEL=/ 1
To figure out in what Run Level you are, run
/sbin/runlevel
It shows the previous and the current Run Levels.
--------------------------------------------------------------
III. Startup Scripts
The startup scripts for different run levels are located in
/etc/rc.d directory:
[alexei@unisys14 rc.d]$ ls
init.d rc0.d rc2.d rc4.d rc6.d rc.sysinit
rc rc1.d rc3.d rc5.d rc.local
[alexei@unisys14 rc3.d]$ ls
K01kdcrotate K46radvd S08iptables S26apmd S66yppasswdd S90tWnn
K03rhnsd K50snmpd S10network S26ypserv S80sendmail S90xfs
K20nfs K65identd S12syslog S27ypbind S85gpm S95anacron
K20rstatd K74nscd S13portmap S28autofs S90canna S95atd
K20rusersd K74ntpd S14nfslock S55sshd S90crond S99local
K20rwalld K74ypxfrd S17keytable S56rawdevices S90cWnn
K20rwhod S05kudzu S20random S56xinetd S90FreeWnn
K35vncserver S08ipchains S25netfs S60lpd S90kWnn
S - means "start"
K - means "kill"
The number - means kill/start sequence number.
In the example above, when system enters Run Level #3, it runs scripts
in directory /etc/rc.d/rc3.d from K01... to K74... sequentially with keyword "stop",
then it runs S05... through S99... with keyword "start", accordingly.
Manual start/stop:
/etc/rc.d/rc3.d/S55sshd start
/etc/rc.d/rc3.d/S55sshd stop
However, the files in /etc/rc.d/rcN.d/ are
symboloc links to the files in /etc/rc.d/init.d.
For example,
S12syslog -> ../init.d/syslog
S13portmap -> ../init.d/portmap
K20nfs -> ../init.d/nfs
To add/remove startup scripts at a particular run level, create/remove
the symbolic links, for example,
ln -s /etc/rc.d/init.d/portmap /etc/rc.d/rc3.d/S13portmap
ln -s /etc/rc.d/init.d/portmap /etc/rc.d/rc0.d/K87portmap
The symbolic links can be created through /sbin/chkconfig and ntsysv. For example,
/sbin/chkconfig --add portmap
/sbin/chkconfig --level 3 portmap on
/sbin/chkconfig --level 3 portmap off
To check on what run level portmap starts,
/sbin/chkconfig --list portmap
To see the list of all startup scripts at various run levels:
/sbin/chkconfig --list
[alexei@unisys14 alexei]$ /sbin/chkconfig --list
canna 0:off 1:off 2:on 3:on 4:on 5:on 6:off
atd 0:off 1:off 2:off 3:on 4:on 5:on 6:off
keytable 0:off 1:on 2:on 3:on 4:on 5:on 6:off
syslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
gpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
kudzu 0:off 1:off 2:off 3:on 4:on 5:on 6:off
kdcrotate 0:off 1:off 2:off 3:off 4:off 5:off 6:off
sendmail 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netfs 0:off 1:off 2:off 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
............................
Configuration files for the startup scripts are located in /etc/sysconfig directory.
Function library is located in file /etc/init.d/functions
Structure of the startup scripts in /etc/rc.d/init.d:
#!/bin/sh
#
# To add/remove the script with /sbin/chkconfig, there is a line below:
# chkconfig: 2345 99 02
# where 2345 - define Run levels where the script starts/stops
# 55 is the priority for Start and 25 is the priority for Stop
# description: just a born shell script;
# let's call it sample.sh
#
# To use source library in the script, uncomment the line below:
#. /etc/init.d/functions
# To use the parameters defined in /etc/sysconfig/, uncomment the line below:
#. /etc/sysconfig/
#
# For example, define the current Run Level:
runl=`/sbin/runlevel | awk '{ print $2 }'`
nowd=`date`
start() {
# start function
echo "$nowd -- start sample script at Run Level $runl" | tee -a /var/log/sample.log
# ....
}
stop() {
# stop function
echo "$nowd -- stop sample script at Run Level $runl" | tee -a /var/log/sample.log
# ....
}
status() {
# status function
echo "status sample script at Run Level $runl"
# .....
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
------------------------------------------------------------------
IV. Scheduled jobs (at, cron)
To schedule a job to run once at a particular time, use command
at time_to_run, for example, to run at 8:40 am:
at 0840
Then type-in commands and finish with ^D
Time format examples:
at 1825 Mar 4
at now + 1 minute
at now + 1 hour
at now + 1 day
To run an executable file or a script:
at -f filename time_to_run
For example,
at -f /home/jerry/script.sh 2030
Command at puts a scheduled job in a queue in /var/spool/at/;
daemon /usr/sbin/atd checks for queued jobs in that directory and runs them.
To check scheduled job ststus, run
atq
To delete scheduled job, run
atrm
Permssions on using at:
If /etc/at.allow does not exist, /etc/at.deny is checked, every user
name not mentioned in it is then allowed to use at.
If neither exists, only the superuser is allowed use of at.
An empty /etc/at.deny means that every user is allowed use these commands,
this is the default configuration.
To schedule jobs to execute repetitively at regular intervals, use cron.
Daemon crond runs every minute and searches /var/spool/cron for crontab files
and also searches for /etc/crontab.
/etc/crontab:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
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
Format: (time) (user) (executable)
time: minute (0-59), hour(0-23), day of the month(1-31), month(1-12), week day (0-6) starting with Sun.
Example:
25 10 * * * root /root/scheduled.sh # runs /root/scheduled.sh every day at 10:25
25 10 * * 6 root /root/scheduled.sh # runs every Sat. at 10:25
*/5 * * * 6 root /root/scheduled.sh # runs every 5 min on Sat.
If a new entry is added to /etc/crontab, crond should be restarted:
/etc/rc.d/init.d/crond stop
/etc/rc.d/init.d/crond start
------------------------------------------------------------------
Assignment due by 03/10/2004
1. Modify /etc/inittab to boot into Run Level 5;
reboot the system using /sbin/init 6 command.
When the system boots up, verify the current run level
with /sbin/runlevel command.
Change sequentially to Run Levels 3, 2, and 1 using /sbin/init N command,
where N is the next Run Level. Run /sbin/runlevel command in every Run Level.
2. Enable portmap in Run Level #3 and cups-lpd in Run Level #4.
Disable portmap in Run Level #4 and cups-lpd in Run Level #3.
/sbin/chkconfig --level 3 portmap on
/sbin/chkconfig --level 4 cups-lpd on
/sbin/chkconfig --level 4 portmap off
/sbin/chkconfig --level 3 cups-lpd off
Check the settings:
/sbin/chkconfig --list portmap
/sbin/chkconfig --list cups-lpd
Running /sbin/init (runlevel), enter
Run Level #1, #3 and then #4. In each level, check if the processes
have been stoped or started:
ps -ef | grep portmap
ps -ef | grep lpd
3. Copy the script above, sample.sh, in /etc/rc.d/init.d directory;
make it executable; try to run it as
/etc/rc.d/init.d/sample.sh
/etc/rc.d/init.d/sample.sh start
/etc/rc.d/init.d/sample.sh stop
Create symbolic links of the script to various /etc/rc.d/rcN.d directories:
ln -s /etc/rc.d/init.d/sample.sh /etc/rc.d/rc1.d/S99sample
ln -s /etc/rc.d/init.d/sample.sh /etc/rc.d/rc2.d/S99sample
ln -s /etc/rc.d/init.d/sample.sh /etc/rc.d/rc3.d/S99sample
Init to Run Levels 1, 2, and 3 and notice if the init
starts S99sample script: check the updated content
of file /var/log/sample.log
Remove the links above. Add the script
/sbin/chkconfig --add sample.sh
/sbin/chkconfig --list sample.sh
Set the script to "stop" at run level 1, 3, and 5:
/sbin/chkconfig --level 135 sample.sh off
Set it to "start" at Run Levels 2 and 4:
/sbin/chkconfig --level 24 sample.sh on
Check the settings:
/sbin/chkconfig --list sample.sh
Check what kind of links with sample.sh have been created in /etc/rc.d/rc1.d
/etc/rc.d/rc2.d, /etc/rc.d/rc3.d, /etc/rc.d/rc4.d and /etc/rc.d/rc5.d
Remove the log file, rm /var/log/sample.log and
enter Run Levels 1, 2, 3, 4, and 5. Check the content of file
/var/log/sample.log.
4. Create a script, mem.sh, to check memory status:
#!/bin/sh
# Checks memory status through cron
of=/home/$USER/mem.out
dt=`date`
echo "Memory status (in MB) on $dt:" >> $of
free -m >> $of
echo "------------------" >> $of
Make it executable and run through at:
at -f mem.sh now + 1 minute
Run atq to make sure the job is scheduled.
In a minute, check content of a new file, mem.out; run atq again.
Remove file mem.out. Modify the script specifying the full path to
your home directory in "of", i.e. "of=/home/your_username/mem.out".
Add the following entry in /etc/crontab before the line "# run-parts":
*/2 * * * * root /home/your_username/mem.sh
Restart crond:
/etc/rc.d/init.d/crond stop
/etc/rc.d/init.d/crond start
Check if the content of mem.out is updated every 2 minutes.
5. Shell scripting tutorial
6. AWK, GREP, SED tools tutorial
7. Read Chapters: 31.2.1 -- 31.2.4, 32.1 -- 32.5, 37