Linux Configuration Service Command Service__linux

Source: Internet
Author: User
Purpose Description

Service commands are used to manage system services, such as Startup (start), Stop (stop), restart (restart), view status (status), and so on. Related commands also include chkconfig, NTSYSV, etc., chkconfig for viewing, setting the running level of the service, NTSYSV for visually and conveniently setting whether each service automatically starts. The service command itself is a shell script that looks for the specified service script in the/etc/init.d/directory and then invokes the service script to complete the task.

Take a look at the following hand albums may have a clearer understanding of the service's insider: The service runs the specified services (called the System V Initial script), removes most of the environment variables, retains only the Lang and term two environment variables, and puts the current path to/, This means running the service script in a very clean environment that can be predicted. This script is saved in the/ETC/INIT.D directory, and it supports at least the start and stop commands.

Man service wrote service (8) service (8)

NAME
Service-run a System V init script

Synopsis
Service SCRIPT COMMAND [OPTIONS]

Service--status-all

Service--help | -H | --version

DESCRIPTION
Service runs a System V init script in as predictable environment as possible, removing most environment vari-
Ables and with the current working directory set to/.

The script parameter specifies a System V init SCRIPT, located in/etc/init.d/script. The supported values of
command depend on the invoked script, service passes COMMAND and OPTIONS it to the Init script unmodified. All
Scripts should support at least the start and stop commands. As a special case, if COMMAND is--full-restart,
The script is run twice, the "then" and "Start" command.

Service--status-all runs all init scripts, in alphabetical order, with the status command.

FILES
/etc/init.d
The directory containing System V init scripts.

Environment
LANG, TERM
The only environment variables passed to the INIT scripts.

ALSO
Chkconfig (8), NTSYSV (8)

2006 Service (8)
Common Ways

Format: Service <service>

Print command line usage Help for the specified service <service>.

Format: Service <service> start

Start the specified system service <service>

Format: Service <service> stop

Stop the specified system service <service>

Format: Service <service> restart

Restarts the specified system service <service>, stopping (stop) before starting (start).

Format: Chkconfig--list

View the list of system services and the level of operation for each service.

Format: Chkconfig <service> on

Sets the specified service <service> starts automatically when it is powered on.

Format: Chkconfig <service> off

Sets the specified service <service> does not start automatically when the machine is open.

Format: NTSYSV

Set the full screen text interface to start automatically when the service is powered on.

using the sample example a network reboot

When you modify the host name, IP address and other information, often need to restart the network to take effect.

[Root@node34 root]# Service Network
Usage:/etc/init.d/network {start|stop|restart|reload|status}
[Root@node34 root]# Service Network status
To configure a device:
Lo eth0
Current active Devices:
Lo eth0
[Root@node34 root]# Service Network restart
Shutting down interface eth0: [OK]
Close Loopback interface: [OK]
Set network parameters: [OK]
Pop-up loopback interface: [OK]
Pop-up interface eth0: [OK]
[Root@node34 root]#

  Example Two restarts MySQL

[root@node34 root]# service mysql 
mysql:unrecognized service
[Root@node34 Root]# service MYSQLD&NBSP
Usage:/etc/init.d/mysqld {start|stop|status|condrestart|restart}
[Root@node34 root]#  Service mysqld status 
Mysqld (PID 1638) is running ...
[Root@node34 root]# service mysqld restart 
Stop mysql:                                                  [  OK  ]
start mysql:                                                 [   OK  ]
[root@node34 root]#

Example three service script source display

[Root@web ~]# Cat/sbin/service
#!/bin/sh

. /etc/init.d/functions

Version= "' basename $ ' ver 0.91"
Usage= "USAGE: ' basename $ ' < option > | --status-all | \
[service_name [Command |--full-restart]] "
Service=
servicedir= "/ETC/INIT.D"
options=

If [$#-eq 0]; Then
echo "${usage}" >&2
Exit 1
Fi

CD/
While [$#-GT 0]; Todo
Case "${1}" in
--help | -H | --h*)
echo "${usage}" >&2
Exit 0
;;
--version | -V)
echo "${version}" >&2
Exit 0
;;
*)
If [-Z "${service}"-A $#-eq 1-a "${1}" = "--status-all"]; Then
CD ${servicedir}
For SERVICE in *; Todo
Case "${service}" in
Functions | Halt | Killall | single| Linuxconf| Kudzu)
;;
*)
if! Is_ignored_file "${service}" \
&& [-X "${servicedir}/${service}"]; Then
Env-i lang= "$LANG" path= "$PATH" term= "$TERM" "${servicedir}/${service}" status
Fi
;;
Esac
Done
Exit 0
elif [$#-eq 2-a "${2}" = "--full-restart"]; Then
Service= "${1}"
If [-X "${servicedir}/${service}"]; Then
Env-i lang= "$LANG" path= "$PATH" term= "$TERM" "${servicedir}/${service}" stop
Env-i lang= "$LANG" path= "$PATH" term= "$TERM" "${servicedir}/${service}" start
Exit $?
Fi
elif [-Z "${service}"]; Then
Service= "${1}"
Else
Options= "${options} ${1}"
Fi
Shift
;;
Esac
Done

If [-X "${servicedir}/${service}"]; Then
env-i lang= "$LANG" path= "$PATH" term= "$TERM" "${servicedir}/${service}" ${options}
Else
echo $ "${service}: Unrecognized SERVICE" >&2
Exit 1
Fi
[Root@web ~]#

example four Crond the source of the service

[Root@web init.d]# Cat/etc/init.d/crond
#! /bin/bash
#
# Crond Start/stop the cron clock daemon.
#
# chkconfig:2345
# Description:cron is a standard UNIX program that runs user-specified \
# programs at periodic scheduled. Vixie Cron adds a \
# Number of features to the basic UNIX cron, including better \
# Security and more powerful configuration options.
# Processname:crond
# config:/etc/crontab
# Pidfile:/var/run/crond.pid

# Source function library.
. /etc/init.d/functions
. /etc/sysconfig/crond
T=${cron_validate_mailrcpts:-unset}
["$t"!= "UNSET"] && export cron_validate_mailrcpts= "$t"

# How we were called.

Prog= "Crond"

Start () {
Echo-n $ "Starting $prog:"
if [-e/var/lock/subsys/crond]; Then
If [-e/var/run/crond.pid] && [-e/proc/' cat/var/run/crond.pid ']; Then
Echo-n $ "Cannot start Crond:crond is already running.";
Failure $ "Cannot start Crond:crond already running.";
Echo
Return 1
Fi
Fi
Daemon Crond $CRONDARGS
Retval=$?
Echo
[$RETVAL-eq 0] && Touch/var/lock/subsys/crond;
Return $RETVAL
}

Stop () {
Echo-n $ "Stopping $prog:"
if [!-e/var/lock/subsys/crond]; Then
Echo-n $ "Cannot stop Crond:crond is not running."
Failure $ "Cannot stop Crond:crond is not running."
Echo
return 1;
Fi
Killproc Crond
Retval=$?
Echo
[$RETVAL-eq 0] && rm-f/var/lock/subsys/crond;
Return $RETVAL
}

Rhstatus () {
Status Crond
}

Restart () {
Stop
Start
}

Reload () {
Echo-n $ "Reloading cron daemon configuration:"
Killproc Crond-hup
Retval=$?
Echo
Return $RETVAL
}

Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart)
Restart
;;
Reload
Reload
;;
Status
Rhstatus
;;
Condrestart)
[-f/var/lock/subsys/crond] && Restart | | :
;;
*)
echo $ "Usage: $ {Start|stop|status|reload|restart|condrestart}"
Exit 1
Esac
[Root@web init.d]#

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.