Linux service/daemon, do you really understand?

Source: Internet
Author: User

Services and GeneralProgramDifference

Service (also known as Daemon) indicates the program running in the background. Generally, it is automatically started with the system startup and can continue running after the user's logoff. The daemon process usually needs to be disconnected from the parent process after it is started, and the process has no control terminal (TTY ).BecauseDaemonThe program is executed in the background and usually closed without terminal interaction.Stdin,StdoutAndStder. Daemon cannot output information. You can use syslog or your own log system to process logs.

You can use the daemon function in the/etc/rc. d/init. d/functions script to start a general program as Daemon. For example, start NFS daemon:

Echo-N $ "Starting NFS daemon :"
Daemon RPC. nfsd $ rpcnfsdargs $ rpcnfsdcount
Etval = $?
Echo

[$ Retval-Ne 0] & Exit $ retval

 

2. service-related directories and files

/Etc/init. d->/etc/rc. d/init. d:/etc/rc. d/init. D contains all the service control scripts,/etc/init. D is/etc/rc. d/init. d.

/Etc/rc *. d->/etc/rc. d/rc *. d:/etc/rc. d/rc *. D contains the configuration of Automatic startup options for all services,/etc/rc *. D is/etc/rc. d/rc *. d.

/Etc/rc. d/rc *. the script in D/is for/etc/rc. d/init. the link to the script under D. It only adds the K or s prefix to the name to indicate automatic start or stop.

The directory tree structure related to all services is as follows:

/Etc
| -- Init. d-> rc. d/init. d
| -- RC-> rc. d/RC
| -- Rc. d
| -- Init. d
| -- NetworkManager
| -- Network
| -- NFS
| -- Xinetd
| --......
| -- RC
| -- RC. Local
| -- RC. sysinit
| -- Rc3.d
| -- K02networkmanager-> ../init. d/NetworkManager
| -- K20nfs-> ../init. d/nfs
| -- S10network-> ../init. d/Network
| -- S56xinetd->.../init. d/xinetd
| --......
| -- RC. Local-> rc. d/rc. Local
| -- RC. News
| -- RC. sysinit-> rc. d/rc. sysinit
| -- Rc0.d-> rc. d/rc0.d
| -- Rc1.d-> rc. d/rc1.d
| -- Rc2.d-> rc. d/rc2.d
| -- Rc3.d-> rc. d/rc3.d
| -- Rc4.d-> rc. d/rc4.d
| -- Rc5.d-> rc. d/rc5.d
| -- Rc6.d-> rc. d/rc6.d

 

Composition and deployment of three services

A service usually contains an executable file and a service control script. Executable programs used as service programs are generally stored in/usr/bin, and scripts used as control services are generally stored in/etc/rc. d/init. d, and the script format for controlling the service is relatively fixed. At least the start, stop, and status parameters are supported. For example, the smb service control script is:

#! /Bin/sh
#
# Chkconfig:-91 35
# Description: starts and stops the Samba smbd and nmbd daemons \
# Used to provide SMB network services.
#
# Pidfile:/var/run/samba/smbd. PID
# Pidfile:/var/run/samba/nmbd. PID
# Config:/etc/samba/smb. conf

# Source function library.
If [-F/etc/init. d/functions]; then
./Etc/init. d/functions
Elif [-F/etc/rc. d/init. d/functions]; then
./Etc/rc. d/init. d/functions
Else
Exit 1
Fi

# Avoid using root's tmpdir
Unset tmpdir

# Source networking configuration.
./Etc/sysconfig/Network

If [-F/etc/sysconfig/samba]; then
./Etc/sysconfig/samba
Fi

# Check that networking is up.
[$ {Networking} = "no"] & Exit 1

# Check that smb. conf exists.
[-F/etc/samba/smb. conf] | exit 6

Retval = 0

Start (){
Kind = "SMB"
Echo-N $ "Starting $ kind services :"
Daemon smbd $ smbdoptions
Retval =$?
Echo
Kind = "nmb"
Echo-N $ "Starting $ kind services :"
Daemon nmbd $ nmbdoptions
Retval2 = $?
Echo
[$ Retval-EQ 0-A $ retval2-EQ 0] & Touch/var/lock/subsys/SMB | \
Retval = 1
Return $ retval
}

Stop (){
Kind = "SMB"
Echo-N $ "shutting down $ kind services :"
Killproc smbd
Retval =$?
Echo
Kind = "nmb"
Echo-N $ "shutting down $ kind services :"
Killproc nmbd
Retval2 = $?
[$ Retval-EQ 0-A $ retval2-EQ 0] & Rm-F/var/lock/subsys/SMB
Echo ""
Return $ retval
}

Restart (){
Stop
Start
}

Reload (){
Echo-N $ "reloading smb. conf file :"
Killproc smbd-Hup
Retval =$?
Echo
Return $ retval
}

Rhstatus (){
Status smbd
Retval =$?
Status nmbd
Retval2 = $?
If [$ retval-Ne 0]; then
Return $ retval
Fi
If [$ retval2-Ne 0]; then
Return $ retval2
Fi
}

# Allow status as non-root.
If ["$1" = Status]; then
Rhstatus
Exit $?
Fi

# Check that we can write to it... so non-root users stop here
[-W/etc/samba/smb. conf] | exit 4

Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Restart)
Restart
;;
Reload)
Reload
;;
Status)
Rhstatus
;;
Condrestart)
[-F/var/lock/subsys/SMB] & restart |:
;;
*)
Echo $ "Usage: $0 {START | stop | restart | reload | status | condrestart }"
Exit 2
Esac

Exit $?

 

4. Service Management Tools/sbin/service

You can use tools/sbin/service to view and manage all services on the system. For example, you can view the status of all services and use/sbin/service -- Status-all, or start or stop a service as/sbin/service XXX start/stop/restat.

Open the/sbin/service script and we can see that the service command implementation is very simple, that is, traversing/etc/init. D (->/etc/rc. d/init. d) run all the service control scripts in the corresponding service control script.

You can use the command CAT/sbin/service to view the implementation of/sbin/service.

 

Five Service Automatic Start control tools/sbin/chkconfig

When you implement your service and deploy it to/etc/init. d. Your script can be manually started and closed, but you still need to make simple configuration to enable your service automatically when the system starts. We know/etc/rc. d/rc *. the script in D is for/etc/rc. d/init. A connection of the Service Control script under D is used to control the automatic start option of the service. For example, in/etc/rc. d/rc3.d lrwxrwxrwx 1 Root 16 2009-05-20 s12syslog-> .. /init. d/syslog. The connection file starts with S, indicating that the syslog service will be automatically started with the system when runlevel is 3.

Manually go to/etc/rc. d/rc *. d. Create a connection file to configure whether the service is automatically started. This is a bit of a hassle. We can use the tool/sbin/chkconfig to simplify the configuration of service startup options. The/sbin/chkconfig tool requires that the first few lines of the Service Control script must contain the following similar information:

#
# Chkconfig:-91 35
# Description: starts and stops the Samba smbd and nmbd daemons \
# Used to provide SMB network services.
#

 

 

The example using chkconfig is as follows:

Chkconfig -- list is used to view the configuration of startup options for all services;

The chkconfig network is used to view the configuration of the startup options of the network service;

 

For more information, see:Six Linux chkconfig command instances-add, delete, view, and modify Automatic startup options of services

 

Xinetd

Xinetd itself is a service that listens to all ports and starts different applications on different ports according to the configuration. For some programs that need to be run in the background, you can choose to set the service to run continuously in the background, or you can choose to use xinetd to configure this program to be activated as needed.

 

For services that require frequent access, you must configure the service under/etc/rc. d/init. d;

For services that are not frequently accessed, you can use xinetd to activate it to save server resources;

In short, choose one service and xinetd.


Seven references:Write daemon in Linux

Complete!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.