Description of Use
Service commands are used to manage system services such as Start (start), Stop (stop), restart (restart), view state (status), and so on. The related commands also include chkconfig, NTSYSV, etc., chkconfig to view, set the runlevel of the service, NTSYSV for intuitive and convenient setting of whether each service starts automatically. The service command itself is a shell script that looks up the specified service script in the/etc/init.d/directory and then invokes the services script to complete the task.
Take a look at the following hand the album may be clearer about the inside of the service: when the service runs the specified services (known as the System V initial script), most of the environment variables are removed, only the Lang and term environment variables are preserved, and the current path is set 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, first with the Stop command, and then with the 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.
See ALSO
Chkconfig (8), NTSYSV (8)
Jan 2006 Service (8)
Common ways
Format: Service <service>
Print the command line use 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>, that is, stop (stop) before starting (start).
Format: Chkconfig--list
View the list of system services and the runlevel of each service.
Format: Chkconfig <service> on
Set the specified service <service> start automatically when powered on.
Format: Chkconfig <service> off
Setting the specified service <service> does not start automatically when the machine is started.
Format: NTSYSV
Sets whether the service starts automatically when it is powered on in a full-screen text interface.
Using the sample example a network restart
When you modify the hostname, IP address and other information, you often need to restart the network to make it effective.
[Email protected] root]#Service Network
Usage:/etc/init.d/network {start|stop|restart|reload|status}
[Email protected] root]#Service Network Status
To configure the device:
Lo eth0
Current Active Device:
Lo eth0
[Email protected] root]#Service Network Restart
Shutting down interface eth0: [OK]
Close Loopback interface: [OK]
Set network parameters: [OK]
Eject loopback interface: [OK]
Popup interface eth0: [OK]
[Email protected] root]#
Example two restart MySQL
[Email protected] root]#service MySQL
Mysql:unrecognized Service
[Email protected] root]#Service Mysqld
Usage:/etc/init.d/mysqld {Start|stop|status|condrestart|restart}
[Email protected] root]#Service mysqld Status
MYSQLD (PID 1638) is running ...
[Email protected] root]#Service mysqld Restart
Stop MySQL: [OK]
Start MySQL: [OK]
[Email protected] root]#
Example three service script source code display
[Email protected] ~]#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]; Do
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 *; Do
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
[Email protected] ~]#
Example four Crond service source code
[Email protected] 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 times. 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"
# See 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
[Email protected] init.d]#
Problem thinking related information
"1" Test the unusual commands in the life of Linux--service
http://www.51testing.com/?uid-66775-action-viewspace-itemid-78574
"2" Linux Shed "service"-"Linux command five-minute series" of the second
http://roclinux.cn/?p=47
"3" yqh860921 Linux service Management
Http://blogold.chinaunix.net/u3/95470/showart_1934759.html
"4" Cool Linux system service comments
Http://www.kuqin.com/linux/20090824/67321.html
"5" Momodog custom Linux Service
http://momodog.iteye.com/blog/286047
Linux-service Service