"Linux" method for creating Linux system services

Source: Internet
Author: User
Tags usleep

Http://www.cnblogs.com/silverwings/archive/2010/08/02/1790801.html

Using this to do the system service, but led to the Apache image Service can not start???

This method was successfully tested on RET Hat Enterprise Linux Server 5.5. The first thing you need to do is write the system service startup script, as follows:

#!/bin/bash

# test.sh

# chkconfig:2345 20 81

# Description:just for Test

Exec_path=/root/server

Exec=gnserver

Pid_file=/var/run/test.sh.pid

Daemon=/root/server/gnserver

# Source function library.

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

if! [-X $EXEC _path/$EXEC]; Then

echo "ERROR: $EXEC _path/$EXEC not Found"

Exit 1

Fi

Stop ()

{

echo "stoping $EXEC ..."

Killall $DAEMON >/dev/null

Rm-f $PID _file

Usleep 100

echo "Shutting down $EXEC: [OK]"

}

Start ()

{

echo "Starting $EXEC ..."

$DAEMON >/dev/null &

Pidof $EXEC > $PID _file

Usleep 100

echo "Starting $EXEC: [OK]"

}

Restart ()

{

Stop

Start

}

Case "$" in

Start

Start

;;

Stop

Stop

;;

Restart

Restart

;;

Status

Status-p $PID _file $DAEMON

;;

*)

echo "Usage:service $EXEC {start|stop|restart|status}"

Exit 1

Esac

Exit $?

The above script needs to be noted in several places.

#chkconfig后面三个参数分别表示服务在哪几个运行级别启动 (This example is in 2,3,4,5), the priority of the service script execution at startup and shutdown.

#description是对该服务的描述. Add these two lines before the service can be added with the Chkconfig command.

In addition/etc/rc.d/init.d/functions This script contains the following status function, which is used to print the state of the current service process.

/root/server/gnserver is the program we need as a system service.

After creating the script, put it in the/ETC/RC.D/INIT.D directory , where I name the script test.sh.

Then modify the properties of the script so that it can be executed: chmod test.sh.

Finally, use the chkconfig command to add it to the system service: Chkconfig--add test.sh.

This will enable our program to run automatically in the background when the system starts. You can also use the command service test.sh start to launch the service program immediately and view the status of the service test.sh status.

You can also use the following method to automatically run the program at system startup: If we want to start the program/root/server/gnserver, then edit the/etc/rc.d/rc.local file, followed by the following line:/root/server/ Gnserver >/dev/null &. The program will run automatically after the system starts. (No effect)

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.