Make Linux executable a service

Source: Internet
Author: User
Tags generator ldap syslog system log time and date openssh server rsync pkill

When the Linux system starts, we can see that many service programs are started one after another (that is, those with a blue [OK] in the back), these programs run in the background for us to use the computer played a very important role in this way to run the program has the following characteristics:
Boot , shutdown, background run
Control or view the running state of the corresponding program through the service command
Well, good, then how to make our own procedures for the production of such a service?

Less nonsense, the main points are the following 3 steps:

1. Put the executable in a place where a Linux system can be found.
Enter the env at the Linux command prompt, you can see the environment variables in the Enter, there is a path, yes, the directory specified in this is the system can find the place, put our executable program into one of the directories can be, we generally put in/usr/sbin/ In the catalogue (where there is anything to put, it is best not to put it in disorder).

2. Under directory/etc/init.d/, create a new file with the name of the service.
If we open the directory/etc/init.d/, see the files are actually service program files, each file content is similar, we will see, here the file structure is almost the same. Almost every file has a start, stop, restart and status flag, yes, the new file we created also has the same structure, just inside these flags to replace the corresponding executable program with our own executable, you know how to do it? That is: copy--Modify--save.

3. Under directory/etc/rc3.d/, create a new symbolic connection file that points to the service file established in step 2nd.
Open the directory/etc/rc3.d/, and we'll see here are the symbolic connection files (the symbolic connection in the Linux system is equivalent to a shortcut in Windows). Use the following command format:
LN-SF destination file name connection file name
The connection file name is in the format of "sxx destination filename", where xx is generally a 1 to 100 integer, which indicates the start priority, the higher the number, the lower the priority, for example: Service A's run depends on service B, that A's XX number should be greater than B. Followed by the "target file name" is to see at a glance, know which file is the symbolic connection.
Directory/etc/rc3.d/is the directory that is automatically searched when the system starts up, and the target file of the symbolic connection file in the directory will be run, which is the reason why the symbolic connection is established in this directory--run it in order to boot.

Well, by completing the 3 steps above, you've made one of your executable programs a service of the system, which has a few specific ones that we started with. We can control this program with the service command:
Start: Service Hdz_service start
STOP: Service Hdz_service stop
Restart: Service Hdz_service restart
The above hdz_service is the service name, which is the file name established in step 2nd.

However, it seems to feel more trouble, can you automate these steps? It's easier to deploy on a new machine, and it's more like-_^! Of course there is, remember that our record code file dependencies between the makefile file? Oh, we're going to use it here.
Add a flag to the makefile file and add a code similar to the following under the flag:

Install
CP./wl_pro/usr/sbin/
CP./wl_service/etc/init.d/
cd/etc/init.d/
chmod +x Wl_service
cd/etc/rc3.d/
LN-SF. /init.d/wl_service./s99wl_service

In the logo install without a word, the front must be left blank, which is not only conducive to reading, but also a requirement, makefile file requires that every executable statement must have white space (space or tab).
The Wl_pro and Wl_service in the code above are the executable file name and the service name, which you can do with your own, with the following command:
Make install
Run it all OK, easy?




-----------------------------------------------------

The operating level of the general system is specified in the/etc/inittab. There is a line like
Id:5:initdefault:
Or
Id:3:initdefault:
, the number here is the default operating level of the system. The assumption is 5.

Starting the service is dependent on the script under/etc/init.d/. These are common shell scripts, when the system is invoked, if the service is started, the start is passed in as a parameter; stop is stopped; restart indicates a restart. So you can write a script, such as simple:

#!/bin/sh

Arg=$1

Case $ARG in
Start):
Nohup/path/to/your/program &
;;
Stop):
Pkill Program
;;
Restart):
Pkill Program
Nohup/path/to/your/program &
;;
Esac

Put this script in/etc/init.d, for example/etc/init.d/your_prog
Then add a link to the appropriate runlevel: start at level 5
Ln-s/etc/init.d/your_prog/etc/rc5.d/s100your_prog
Ln-s/etc/init.d/your_prog/etc/rc0.d/k100your_prog
Here two numbers can be used for the largest number in the corresponding directory, because no other service depends on your service.
This means that when the system enters Init level 5, start your program and go to init level 0 to stop your program.

----------------------------------------------------------

Introduction to System Services
1. Alsasound:alsa sound card driver daemon.
2. Acpid:acpid (Advanced Configuration and Power Interface) is a new power management standard that has been introduced to replace traditional APM power management standards. Usually laptops need to be powered up for management.
3. Atalk:appletalk the network daemon.
4. AMD: Automatically installs the NFS daemon.
5. Anacron: An automated running task daemon.
6. APMD:APMD (Advanced Power Management) is a premium management.
7. ARPTABLES_JF: Control the filtering daemon for users of the Arptables network.
8. Arpwatch: Log and build an Ethernet address and IP address pair database that you see on the LAN interface.
9. AutoFS: Automatically installs the management process AutoMount, which is related to NFS and relies on Server for NIS.
BOOTPARAMD: A boot parameter server that provides the information needed to boot a diskless workstation on a LAN.
Bluetooch: Bluetooth server daemon.
Crond:cron is a traditional program under UNIX that periodically runs user-scheduled tasks. Compared to the traditional UNIX version, the Linux version adds a lot of attributes and is more secure and simpler to configure. Similar to Scheduled tasks.
Chargen: The Chargen server,chargen (Character Generator Protocol) using the TCP protocol is a network service, and the main function is to provide similar remote typing capabilities.
CHARGEN-UDP: Chargen server using the UDP protocol.
Cpuspeed: Monitors system idle percentages, reduces or accelerates CPU clock speed and voltage to minimize energy consumption when the system is idle, and maximizes system execution speed when the system is busy.
DHCPD: The service daemon for Dynamic Host Control Protocol (PROTOCOL).
Cups:cups (Common Unix Printing System) is a generic UNIX print daemon.
Cups-config-daemons:cups Print System Switch daemon.
The Cups-lpd:cups line Print daemon.
Daytime: The daytime daemon using the TCP protocol, which provides the client with the ability to obtain the date and time from a remote server. Default port: 13.
DAYTIME-UDP: The daytime daemon using the UDP protocol.
Dc_server: Proxy Server daemon using SSL Secure sockets.
Dc_client: The client daemon that uses SSL Secure sockets.
Diskdump: Server disk Backup daemon.
Echo: The server echoes the Customer data Service daemon.
ECHO-UDP: The server that uses the UDP protocol echoes the client data Service daemon.
Eklogin: A daemon that accepts rlogin session forensics and a service that is encrypted with KERBEROS5.
Gated: Gateway routing daemon. It supports a variety of routing protocols, including RIP versions 1 and 2, the DCN Hello Protocol, OSPF version 2, and EGP versions 2 through 4.
GPM:GPM (General Purpose Mouse Daemon) daemon provides mouse support for Linux programs in text mode such as MC (Midnight Commander).
GSSFTP: FTP daemon with Kerberos 5 authentication
Httpd:web Server Apache daemon.
inetd: Internet Operation daemon.
Innd:usenet News server Daemon.
Iiim: Chinese Input Method Server daemon.
Iptables:iptables Firewall daemon.
The. IrDA: Infrared Port daemon.
ISDN:ISDN starts and aborts the service daemon.
Krb5-telnet: The telnet daemon that uses Kerberos 5 authentication.
Klogin: remote login daemon.
Keytable: The function of this process is to reprint the keyboard mapping table defined in/etc/sysconfig/keyboards, which can be selected through the Kbdconfig tool. You should make the program active.
Irqbalance: A daemon that load balances system interrupt requests in multiple system processor environments. If you have only one CPU installed, you do not need to load this daemon.
Kshell:kshell daemon process.
Kudzu: Hardware auto-detection program.
The Directory Access Protocol server daemon for LDAP:LDAP (Lightweight Directory Access Protocol).
Lm_seroems: Detects the motherboard work daemon.
LPD:LPD is an old-fashioned print daemon responsible for submitting programs such as LPR to print jobs.
Mdmonitor:raid. Daemon for related devices.
Messagebus:d-bus is a library that provides one-to-two communication for two or more two applications.
Microcode_ctl: Can encode and send new micro-codes to the kernel to update the Intel IA32 series processors.
Mysqld: A fast and efficient and reliable lightweight SQL database engine daemon.
The Named:dns (BIND) server daemon.
NETPLUGD:NETPLUGD (Network cable hotplug Management daemon) daemon, which monitors the state of one or more network interfaces and runs an external script program when certain events are triggered.
Netdump: Remote network backup server daemon.
Netfs:network Filesystem Mounter, the process installs and uninstalls NFS, samba, and NCP network file systems.
NFS: Network File System daemon.
Nfslock:nfs is a popular protocol for sharing files over a TCP/IP network, and this daemon provides the NFS file locking feature.
Ntpd:network Time Protocol Daemon (Network Temporal Correction Protocol). NTPD is the protocol daemon used to keep the system and an accurate time source in sync with time.
NET: Activates/shuts down the various network interface daemons at startup.
PSACCT: The daemon includes several tools for monitoring process activity, including Ac,lastcomm, Accton, and SA.
PCMCIA: Mainly used to support the laptop interface daemon.
Portmap: This daemon is used to support RPC connections, and RPC is used for services such as NFS and NIS.
Postgresql:postgresql relational database engine.
PROFTPD:PROFTPD is a daemon that configures a flexible FTP server under UNIX.
PPPOE:ADSL Connection daemon.
Random: A high-quality random number generator that saves and restores the system, which is provided by some random behavior of the system.
Rawdevices: The daemon used to load raw devices when using a clustered file system.
ReadAhead, Readahead_early:readahead, and Readahead_early are the latest two daemons running in the background in Fedora Core 2. The function is to start the system, the file to be used to start the system to read into memory, and then execute in memory to speed up the system.
rhnsd:red Hat Network Service daemon. Notify the official security information and patch the system.
Routed: The daemon supports automatic IP routing table maintenance for RIP protocols.
Rsync:remote Sync Remote Data Backup daemon.
RSH: Starts a shell on the remote host and executes the user command.
Rwhod: Allows a remote user to obtain a list of all logged-in users on the machine running the rwho daemon.
RSTATD: A waiting process for collecting and providing system information for other machines on the LAN.
Ruserd: Remote User location service, an RPC-based service that provides user information about the current record to a machine log in the LAN
RWALLD: Activates the Rpc.rwall service process, an RPC-based service that allows users to write messages to each other terminal registered on the LAN machine.
Rwhod: Activates the Rwhod service process, which supports LAN rwho and Ruptime services.
SASLAUTHD: Use the SASL authentication daemon.
SendMail: Mail server sendmail daemon.
Smb:samba File share/Print Service daemon.
SNMPD: Local Simple Network management daemon.
Bayi Squid: Agent server squid daemon.
The SSHD:OPENSSH server daemon. Secure Shell protocol enables remote management of hosts safely.
Smartd:self Monitor Analysis and Reporting technology System monitors your hard drive for failure.
Syslog: A script that lets the system boot up the syslog and klogd the system log waiting process.
Time: The daemon obtains the times and dates from the remote host, using the TCP protocol.
TIME-UDP: The daemon obtains the time and date from the remote host, using the UDP protocol.
Tux: The daemon that runs the Apache server in the Linux kernel.
The daemon of the VSFTPD:VSFTPD server.
VNCSERVER:VNC (Virtual network Computing, VM Compute).
xfs:x Window Font Server daemon that provides font sets for local and remote X servers.
XINETD: A core daemon that supports multiple network services.
Ypbind: Activates the Ypbind service process for NIS (Network Information System) clients.
The Yppasswdd:nis Password Server daemon.
94. Ypserv:nis Master Server daemon.
YUM:RPM operating system auto-upgrade and package management daemon.
According to their own needs to feel whether to start a department service. such as: U, iptables, etc. are necessary to run. Echo, ECHO-UDP, daytime, DAYTIME-UDP, Chargen, CHARGEN-UDP can be turned off. Rsh, RSTATD, rsync, RUSERSD, rwalld These commands are Berkley remote commands, because they all start with the letter R, so they are called the r* command. The main use is to make a user on a computer with the same account remote execution of another computer program, do not shut down, reduce the risk. Innd is the process that runs the newsgroup service and should not be closed.


----------------------------------------------------------

Nohup is to tell the system to ignore the user exiting the system
Let the process run until the end of the process
For example, I am remotely opening the SSH terminal to run the program./server
If you don't use nohup when I close the SSH terminal
The./server process will abort
Using Nohup./server &
Can be used when the user exits the terminal
Process can continue to execute

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Make Linux executable a service

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.