Shell ScriptingLearn Managing Services in Linux Shell Scripting

Learn Managing Services in Linux Shell Scripting

In this article, we are going to talk about new topic: Managing Services.
 
What is Service?
A service is a software program whose functionality is to serve, i.e. respond to requests from other programs, or network clients. Services run as background processes, and are usually started at operating system boot.
 
For example, a DNS service running on one machine, provides the name resolution facility to other machines that need to resolve hostnames to IP addresses (or vice versa), and to itself as well. Similarly, when you start an SSH connection to a UNIX, Linux machine, router or firewall, you act as an SSH client, while your destination becomes an SSH server, that runs the sshd (d for daemon) service. When you connect to a shared directory (on Linux, or even Windows), the machine that shares the directory runs a software called SMB (Server Message Block) that provides the file sharing facility. All these examples take us to the client/server model, where we have a client that requests a service, and a server that provides the service.
 
How does Linux Shell Manage Services?
Services in Linux are managed using two commands: service and chkconfig.
 
The service Command
The service command controls system services, and displays their state. It can start, stop, restart, reload a service, or simply print its current status.
 
Syntax

service SERVICENAME COMMAND
service --status-all	##Lists all services in alphabetical order ##with their status.

Where:            
SERVICENAME is the name of the service to be started, stopped, restarted, or have its status displayed.
COMMAND is either start, stop, restart, reload, and status.
 
Example
To check the status of the ntpd (Network Time Protocol) service which is responsible for maintaining correct date and time on the machine by synchronizing with an NTP server, we need to use the following command:
1
To start the stopped service, use the same command, but with start instead of status:
2
Now, re-check the status:
3
Example
To reload (refresh) the httpd (Apache) service after making some configuration changes, without having to stop and start the service, we could use the service command, with the reload subcommand.
4
Enrich Your Linux Knowledge
The list of supported subcommand may differ from service to another. To get the list of supported subcommands for a specific list, use the service command without specifying subcommand:

service SERVICENAME

Example
What are the subcommands supported for httpd and sshd services?
5
Example
After changing the IP address of your Linux box, you need to restart the network service to reflect changes. This can be done using the restart subcommand.
6
How are Services Started?
Services are started either automatically when the system boots, or manually when the administrator runs service start command.
 
To define how services will start, the chkconfig command comes into the stage.
 
Learn the Basics of C Programming Language

The chkconfig Command
chkconfig checks and controls whether a service should start on system boot, or not. For a given service, the command can list and modify the startup info for the service per runlevel. The runlevel defines the mode (status) of the machine after boot. Most Linux distributions have seven runlevels, ranging from 0 (shutdown) to 6 (reboot). The seven runlevels are listed in the following table:

Run Level Description
0 System Halt (Shutdown)
1 Single User Mode
2 Multi-user, without NFS service (same as runlevel 3, if you do not have networking)
3 Full multi-user mode
4 Reserved
5 X11 (GUI)
6 System Reboot

 
Syntax

chkconfig [--list]	##lists  all  of  the  services which ##chkconfig knows about, and whether they ##are stopped or started in each runlevel.

chkconfig SERVICENAME <on|off>	##Turns a specific service on ##or off

Example
To list all the defined services on your Linux box, with their startup state in all runlevels:
7
Notice that using chkconfig without any arguments is exactly equivalent to using it with –list option.
 
Example
Based on the above output, the httpd and ntpd services don’t start automatically when the system boots. We need to enable both services to start automatically.
8
To check they are enabled:
9
 
* * * * * *
 
In this article, we have talked about Services, and how to manage them. Short and easy article, but important!! See you in the next article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -