How to add a service in Linux

Source: Internet
Author: User
Tags exit chmod config ssh openssh server

Software installation into the service process detailed:

Service httpd Restart

Composed of three parts service (services), httpd (program name), restart (related commands) One less

Service: The system is already defined. No changes are required. /etc/init.d/* is found by default

HTTPD: The name of the program is already determined.

Restart: Related commands, such as start this is the program direction.

Then very clearly a service needs the process:

1, the procedure is the executable procedure

2, the location should be placed in the/etc/init.d/directory

3, the relevant orders, this by the program itself to decide (there is no designation).

3.1 If there is already a relevant command, then the file is copied directly to the/ETC/INIT.D directory, and the spear can execute permissions.

3.2 If there is no definition, then we need to write a script. and define the relevant commands.

First, look at a sshd example to help us understand the whole process.

# vim/etc/init.d/sshd

----------sshd Start------------------

#!/bin/bash

#

# Init file for OpenSSH server daemon

#

# chkconfig:2345 55 25

# DESCRIPTION:OPENSSH Server Daemon

#

# processname:sshd

# config:/etc/ssh/ssh_host_key

# config:/etc/ssh/ssh_host_key.pub

# config:/etc/ssh/ssh_random_seed

# config:/etc/ssh/sshd_config

# Pidfile:/var/run/sshd.pid

# source Function Library

. /etc/rc.d/init.d/functions

# Pull in Sysconfig settings

[-f/etc/sysconfig/sshd] &&. /etc/sysconfig/sshd

Retval=0

Prog= "Sshd"

# Some functions to make the below more readable

Keygen=/usr/bin/ssh-keygen

Sshd=/usr/sbin/sshd

Rsa1_key=/etc/ssh/ssh_host_key

Rsa_key=/etc/ssh/ssh_host_rsa_key

Dsa_key=/etc/ssh/ssh_host_dsa_key

Pid_file=/var/run/sshd.pid

runlevel=$ (Set--$ (runlevel); eval "Echo \$$#")

Do_rsa1_keygen () {

If [!-s $RSA 1_key]; Then

Echo-n $ "Generating SSH1 RSA host key:"

If $KEYGEN-Q-t rsa1-f $RSA 1_key-c ' n ' >&/dev/null; Then

chmod $RSA 1_key

chmod 644 $RSA 1_key.pub

if [-x/sbin/restorecon]; Then

/sbin/restorecon $RSA 1_key.pub

Fi

Success $ "RSA1 Key generation"

Echo

Else

Failure $ "RSA1 key generation"

Echo

Exit 1

Fi

Fi

}

Do_rsa_keygen () {

If [!-s $RSA _key]; Then

Echo-n $ "Generating SSH2 RSA host key:"

If $KEYGEN-Q-t rsa-f $RSA _key-c ' n ' >&/dev/null; Then

chmod $RSA _key

chmod 644 $RSA _key.pub

if [-x/sbin/restorecon]; Then

/sbin/restorecon $RSA _key.pub

Fi

Success $ "RSA Key generation"

Echo

Else

Failure $ "RSA key generation"

Echo

Exit 1

Fi

Fi

}

Do_dsa_keygen () {

If [!-s $DSA _key]; Then

Echo-n $ "Generating SSH2 DSA host key:"

If $KEYGEN-Q-t dsa-f $DSA _key-c ' n ' >&/dev/null; Then

chmod $DSA _key

chmod 644 $DSA _key.pub

if [-x/sbin/restorecon]; Then

/sbin/restorecon $DSA _key.pub

Fi

Success $ "DSA Key generation"

Echo

Else

Failure $ "DSA key generation"

Echo

Exit 1

Fi

Fi

}

Do_restart_sanity_check ()

{

$SSHD-T

Retval=$?

if [! "$RETVAL" = 0]; Then

Failure $ "Configuration file or keys are invalid"

Echo

Fi

}

Start ()

{

# Create Keys if necessary

Do_rsa1_keygen

Do_rsa_keygen

Do_dsa_keygen

Cp-af/etc/localtime/var/empty/sshd/etc

Echo-n $ "Starting $prog:"

$SSHD $OPTIONS && Success | | Failure

Retval=$?

["$RETVAL" = 0] && touch/var/lock/subsys/sshd

Echo

}

Stop ()

Url:http://www.bianceng.cn/os/linux/201410/45691.htm

{

Echo-n $ "Stopping $prog:"

If [-N ' ' Pidfileofproc $SSHD ']; Then

Killproc $SSHD

Else

Failure $ "Stopping $prog"

Fi

Retval=$?

# If we are in halt or reboot RunLevel kill all running sessions

# so the TCP connections are closed cleanly

If ["X$runlevel" = X0-o "x$runlevel" = x6]; Then

Killall $prog 2>/dev/null

Fi

["$RETVAL" = 0] && rm-f/var/lock/subsys/sshd

Echo

}

Reload ()

{

Echo-n $ "Reloading $prog:"

If [-N ' ' Pidfileofproc $SSHD ']; Then

Killproc $SSHD-hup

Else

Failure $ "Reloading $prog"

Fi

Retval=$?

Echo

}

Case "$" in

Start

Start

;;

Stop

Stop

;;

Restart)

Stop

Start

;;

Reload

Reload

;;

Condrestart)

if [-f/var/lock/subsys/sshd]; Then

Do_restart_sanity_check

If ["$RETVAL" = 0]; Then

Stop

# Avoid race

Sleep 3

Start

Fi

Fi

;;

Status

Status-p $PID _file Openssh-daemon

Retval=$?

;;

*)

echo $ "Usage: $ {Start|stop|restart|reload|condrestart|status}"

Retval=1

Esac

Exit $RETVAL

--------------sshd Stop---------------------------

Boiled Wine tea: you can see that some judgment statements and some execution paths. To get the relevant command at the bottom and give the relevant execution command. Look, the addition of a service is so simple. Test it.

The process is as follows:

[root@localhost test]# Service Test restart

Test:unrecognized Service

[Root@localhost test]# CP test/etc/init.d/

[root@localhost test]# ll/etc/init.d/|grep test

-rwxr-xr-x 1 root Apr 11:37 test

[root@localhost test]# Service Test restart

Hello World

#vim/etc/init.d/test

-------------------Test Start---------------

Case "$" in

Start

Echo-n "Starting test:"

~/test/test

Echo-n "Start is OK"

;;

Stop

Echo-n "Shutting stop test:"

Killall Test

Echo-n "Stop is OK"

;;

Esac

Exit

-------------Stop-------------------

[root@localhost test]# Service Test start

Starting Test:hello World

Start is ok[root@localhost test]# service test stop

Shutting stop Test:test:no process killed

Boiled Wine tea: it is easy to understand, first put in the service can find the place, and then give it to judge the command if it is start to write the file is starting in test, boot up after the start OK. It would be nice to end the process by killing him directly. Other free play such as: Reload, restart and so on.

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.