The Linux start up sequence for many years had been a hybrid of BSD
and SVR4 stay up files. These files consisted of the /etc/inittab,
/etc/rc.local and the /etc/init.d/* and /etc/rc[0-6].d/* locations.
The sequence was sequential and in most cases required each command
to complete before moving on to the next. Debian based systems
atempted to tackle this issue and sped up the start up process by
introducing upstart which enabled milestones and dependencies, but
was cumbersome and not as effective as the Solaris 10 start up
method. Red Hat 7 took its startup sequence from the Fedora project,
with Fedora 18 and 19 making the change to systemd which by Fedora 20
was firmly in place and saw the demize of the old mechanism, but like
Red Hat 7 still allows the old style SVR4 scripts to be used.
In the rest of this document I will take you through the necesary
changes from v6 to v7 start up, covering;
- Essential commands to start and stop services
- Commands to disable or enable a service at boot
- How to change the default run level
- How to configure your own boot script
Essential service commands
In this section we will look at how you start, stop and view the
status services in Red Hat 7.
Starting a service Red Hat 6
The service command in Red Hat 6 is used to control daemon process
and associated files, such as the pid file, subdydtem lock file and
others. In the event that a service (daemon) terminates unexpectedly
these files remain in place as a foot print to let us know that the
system didn't stop cleanly. To get the service back up and running
you would need to remove these files from within the /var
subdirectories. They are normally in /var/run and /var/lock.
service serviceName start
E.g. service dhcpd start
Starting a service Red Hat 7
systemctl start serviceName
E.g. systemctl start dhcpd.service
Stopping a service Red Hat 6
service serviceName
stop
E.g. service network stop
Stopping a service Red Hat 7
systemctl stop serviceName
E.g. systemctl stop network.service
You'll notice that the syntax for
the systemctl command lead to better history editing since the name
of the service comes after the action unlike the old service
commands, alowing us to simply recall the command and remove the
service name and put in the next.
Systemctl syntax
systemctl action
serviceName
systemctl [start|stop|status]
serviceName
Listing services Red Hat 6
chkconfig --list [serviceName]
This will list whether a service is
set to run at boot time if the service name is supplied, or list all
services run ability at boot.
If you wish to se current state of
all processes then you would require the following shell script;
for serviceName in $(chkconfig --list | cut -f1 -d' ')do service $serviceName statusdone
Listing services Red Hat 7
systemctl -a
This one command tells us whether
the service is running or is set to run at boot time.
You can also see if an individual service is enabled with;
systemctl is-enabled serviceName
Another useful command for checking
a daemons status is the new journalctl command which shows
information that will have been written to the log file. This
command shows up if you start a service from the command line, and
mentions the use of option -xn.
To list all available services;
systemctl list-unit-files --type=service
Red Hat 6 enable or disable services at boot
The chkconfig command was used up to and including Red Hat 6 systems
to define if a service should be included in the run level as part of
its start up sequence. This command like the service command can
still be used in v7, but you should become familiar with the new
commands.
Enabling a service for any runlevel
was performed with;
chkconfig serviceName on
And to disable it;
chkconfig serviceName off
To specify the runlevel you would
use the --level option
E.g. chkconfig --level 23 network on
These commands simply created the correct S or K script in the
relevant rc.d directories.
To add your own boot script to a system you used;
chkconfig --add scriptName
Where scriptName
is the name of the boot script in the /etc/rc.d/init.d directory that
you created. There is also a corresponding --del to remove it from
the boot sequence, but not the init.d directory.
Red Hat 7 enable or disable services at boot
In the v7 systems we continue to use the one command, systemctl.
Enable a service or resource with;
systemctl enable serviceName
E.g. systemctl enable cupsd.service
Disable a service with;
systemctl disable serviceName
Modifying the boot sequence
In this section we will look at how to set the default runlevel, add
your own service and change the runlevel on boot.
Red Hat 6 setting default runlevel
The /etc/inittab file is used up to
and including v6. One line in the file needed the numerical value
changed. The initdefault line required the number in column 2 to be
set to your desired run level. E.g.
id1:3:initdefault:
Note that with the lines in the
inittab it was always essential to have the correct number of :
characters on the line.
The above example sets the default run level to 3.
Red Hat 7 setting default runlevel
The
inittab has now been retired and a symlink system is now in use that
points to the relevant configuration file containing the runlevel to
be started on boot. The files that define the levels are located in
the /lib/systemd/system directory and end .system, e.g.
graphical.target.
To
find out the current and default runlevel use the following command;
systemctl
get-default
To
change the resources to start on boot you use the set-default option
as follows;
systemctl
set-default multi-user.target
If
you wish to use run levels in v7 you can by specifying;
runlevel?.target
The
? should be changed for the number of the run level you want to set.
You can then use;
systemctl
set-default runlevel5.target
The
above example is the same as graphical.target.
Red Hat 6 temporarily change runlevel at boot
Through grub boot menu you would
press a key to halt the countdown. You then press e
to edit the kernel boot line you wish to start the system with and
add the runlevel number to the end of the kernel line and press the
relevant key to boot the system using your current settings.
This of course can be used to alter any kernel values.
Red Hat 7 temporarily change runlevel at boot
The
process is the same as v6, but instead of specifying the numerical
run level you will need to use the target name at the end of the
kernel line. For example to use rescue mode;
systemd.unit=rescue.target
Red Hat 6 boot scripts
These
are simple bash shell scripts which make use of the case statement,
but require 2 special comments to appear in the file. The 2 comments
required are;
#description:
#chkconfig:
Note that they must have the colon (:) symbol immediately street the
name.
Following
the colon are the directives to define what the script is for and
what the default start and stop run levels are.
Example:
#!/bin/bash#description:
Describes what your service does, used by apps that can provide more
info#chkconfig
235 99 01# the above line sets the service to start at # run levels 2, 3 and 5
as the 99th item, e.g S99xxx#
and spots add the 1st, e.g K01case $1 in 'start') #
start the service here and create pid and lock files ;; 'stop') #
stop the service using pid file, remove lock file ;; 'status') #
use pid file to check process is running
# and if lock file exists
;;esac
# and if lock file exists
;;esac
Red Hat 7 boot scripts
The
equivalent of the init.d script is to create a .sevice file in the
/lib/systemd directory. Notice that you can still use your old style
init.d script by placing it into /usr/lib/systemd/scripts.
[Unit]Description=Describe
your service[Service]Type=oneshotExecStart=/usr/lib/systemd/scripts/yourInitScript
startExecStop=/usr/lib/systemd/scripts/yourInitScript
stopRemainAfterExit=yes[Install]WantedBy=multi-user.target
You
change the relevant parts, e.g. what this script needs before it can
start, the daemon to run our the script.
Useful references
- https://fedoraproject.org/wiki/SysVinit_to_Systemd_Cheatsheet
- http://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd
No comments:
Post a Comment