How to Create a Linux service and program self-start

Source: Internet
Author: User
Article Title: How to Create a Linux service and program self-start. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

1. Create a self-starting program in Linux

You can use either of the following methods to perform self-testing.

1.1 method 1 of self-starting program:

1.2 method 2 of self-starting program:

To create a Linux service, follow these steps:
1) write the Service Startup Script File (see the following example)
2) modify the chmod 777 autoruntest script file.
3) copy the script file to/etc/rc. d/init. d/
4) chkconfig -- add autoruntest to add a service
5) Restart (completed)
After step 3, you can start and stop using the service. To start the service, you must use chkconfig to add the service to the system.

2. Services in Linux

To customize system services, you must use the root user. Through the ntsysv command, you can see all the services of the system and whether they are started together with the system. Here, you can customize system services. The operation interface is shown in, here we can see that the test program autoruntest has been added as a system service and runs along with the system. Here you can customize the started service. Press F1 to see the introduction of each service. The system service description is attached.

3 appendix

 3.1 Service script file example

#! /Bin/bash
#
# Autoruntest the shell script takes care of autoruntest auto start and stop
#
# Chkconfig: 2345 20 80
# Description: autoruntest
# Processname:/etc/rc. d/init. d/autoruntest
EXEC_PATH =/home/robin/code
EXEC = autoruntest
PID_FILE =/var/run/autoruntest. pid
DAEMON =/home/robin/code/autoruntest
# 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
Usleep 100
Echo "Shutting down $ EXEC: [OK]"
}
Start ()
{
Echo "Starting $ EXEC ..."
$ DAEMON>/dev/null &
Usleep 100
Echo "Starting $ EXEC: [OK]"
}
Restart ()
{
Stop
Start
}
Case "$1" 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 $?

[1] [2] Next page

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.