Make a linux executable program into a service

Source: Internet
Author: User
Tags syslog openssh server pkill

When the linux system is started, we can see that many service programs are started one by one (that is, the lines with blue [OK ), these programs run in the background play an important role in using computers. The programs run in this way have the following features:
Start and shut down (nonsense! Shut down and run ???)
Background running
Use service commands to control or view the running status of the corresponding program
Well, that's good. How can we make our own program into such a service?
 
The following three steps are involved:
 
1. Place the executable program in a place that can be found in a linux system.
Enter env in the linux Command Prompt, and press enter to see the environment variable. There is a PATH in it. Right, the directory specified in this item is the one that can be found by the system, put our executable program in one of the directories. We usually put it in the/usr/sbin/directory (where there is something to put, it is better not to put it in disorder ).
 
2. In the/etc/init. d/directory, create a file named "service.
If we open the directory/etc/init. d/. The files you see are actually service program files. The content of each file is similar. We can see that the files here are basically the same in the file structure. Almost every file has a mark such as start, stop, restart, and status, I just need to replace the corresponding executable program with our own executable program in these marks. Do you know how to do it? That is: Copy, modify, and save.
 
3. In the/etc/rc3.d/directory, create a symbolic connection file pointing to the service file created in step 1.
Open the/etc/rc3.d/directory, and we will see that all these are symbolic Connection Files (the symbolic connection in linux is equivalent to a shortcut in windows ). Use the following command format:
Ln-sf target file name connection file name
The connection file name is in the format of "SXX target file name", where XX is generally an integer from 1 to 100, which indicates the startup priority. The larger the number, the lower the priority, for example: if service A depends on service B, the XX number of service A should be greater than that of Service B. The "target file name" is followed by a symbolic connection to the desired file at a glance.
Directory/etc/rc3.d/is the directory automatically searched when the system starts. The target files of the symbolic connection files under the directory will be run, this is the reason for establishing a symbolic connection in this directory-to run on startup.
 
Now, after completing the three steps above, you can make one of your executable programs into a service of the system. It has some specific features we mentioned at the beginning. We can use the service command to control this program:
Start: service hdz_service start
Stop: service hdz_service stop
Restart: service hdz_service restart
The above hdz_service is the service name, that is, the file name created in step 1.
 
However, it seems to be troublesome. Can you automate these steps? In this way, it is easy to deploy on a new machine, and it is more like-_ ^! Of course there is a solution. Do you still remember the makefile that records the dependencies between code files? We will use it here.
Add a flag to the makefile file and add code similar to the following under the flag:
 
Install:
Cp./hdz_pro/usr/sbin/
Cp./hdz_service/etc/init. d/
Cd/etc/init. d/
Chmod + x hdz_service
Cd/etc/rc3.d/
Ln-sf ../init. d/hdz_service./S99hdz_service
 
There is no sentence under the install flag. It must be left blank before, which is not only conducive to reading, but also a requirement, makefile requires that each executable statement must be blank (space or tab ).
In the above Code, hdz_pro and hdz_service are executable file names and service names respectively. You can replace these two names with your own. Use the following command to perform the operation:
Make install
After running, everything is OK. Is that simple?
 
When reading what I wrote, people may encounter something like "hdz" from time to time. This is the abbreviation of my name, indicating that this is exactly what I want to replace.
 
 
-----------------------------------------------------
 
Generally, the system running level is specified in the/etc/inittab. One line is similar
Id: 5: initdefault:
Or
Id: 3: initdefault:
The number here is the default running level of the system. Suppose it is 5.
 
To start the service, you must start the service by running the script in/etc/init. d. These are common shell scripts. When the system calls a service, if the service is started, the start parameter is input; stop indicates that the service is stopped; restart indicates that the service is restarted. Therefore, you can write a script, such as the following:
 
#! /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 the corresponding link to the running level: When the startup level is 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
Both numbers can use 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 stop your program when it enters init level 0.
 
----------------------------------------------------------
 
System Service Introduction
1. alsasound: the Alsa audio card driver daemon.
2. acpid: acpid (Advanced Configuration and Power Interface) is a new Power management standard to replace the traditional APM Power management standard. Generally, the laptop needs to be powered on for management.
3. atalk: AppleTalk network daemon.
4. amd: automatically installs the NFS daemon.
5. anacron: An automated task daemon.
6. apmd: apmd (Advanced Power Management) is Advanced Power Management.
7. arptables_jf: Specifies the daemon for user control of the arptables network.
8. arpwatch: records logs and constructs an ethernet address and IP address pair database seen on the LAN interface.
9. autofs: automatically installs the management process automount, which is related to NFS and depends on the NIS server.
10. bootparamd: boot parameter server that provides information required for booting a diskless workstation on the LAN.
11. ch: the Bluetooth server daemon process.
12. crond: cron is a traditional Unix program that periodically runs user-scheduled tasks. Compared with the traditional Unix version, the Linux version adds many attributes, which are safer and easier to configure. Similar to scheduled tasks.
13. chargen: the chargen server using the tcp Protocol. Character Generator Protocol is a network service. Its main function is to provide a function similar to remote typing.
14. chargen-udp: Use the chargen server of UDP protocol.
15. cpuspeed: monitor the idle percentage of the system to reduce or speed up the CPU clock speed and voltage so as to minimize the energy consumption when the system is idle, while maximizing the system execution speed when the system is busy.
16. dhcpd: service daemon of the Dynamic Host Control Protocol.
17. cups: cups (Common UNIX Printing System) is a universal UNIX print daemon.
18. cups-config-daemons: the cups printing system switches the daemon process.
19. cups-lpd: the daemon for printing cups rows.
20. daytime: the Daytime daemon of TCP protocol is used to obtain the date and time from the remote server for the client. Default port: 13.
21. daytime-udp: Use the Daytime daemon of UDP protocol.
22. dc_server: the proxy server daemon that uses the SSL secure socket.
23. dc_client: the client daemon that uses the SSL secure socket.
24. diskdump: the server disk backup daemon.
25. echo: the server displays the customer data service daemon.
26. echo-udp: the server that uses the UDP protocol returns the client data service daemon.
27. eklogin: a service daemon that accepts rlogin session authentication and is encrypted with kerberos5.
28. gated: Gateway Routing daemon. It supports various routing protocols, including RIP version 1 and 2, dcn hello protocol, OSPF version 2, and EGP version 2 to 4.
29. gpm: the gpm (General Purpose Mouse Daemon) Daemon provides Mouse support for Linux programs in text mode, such as mc (Midnight Commander.
30. gssftp: ftp daemon using kerberos 5 Authentication
31. httpd: the Apache daemon of the Web server.
32. inetd: Internet operation daemon.
33. innd: Usenet news server daemon.
34. iiim: the daemon process of the Chinese Input Method server.
35. iptables: iptables firewall daemon.
36. irda: infrared port daemon.
37. isdn: start and stop the service daemon process.
38. krb5-telnet: telnet Daemon using kerberos 5 authentication.
39. klogin: log on to the daemon remotely.
40. keytable: the function of this process is to reprinted the keyboard ing table defined in/etc/sysconfig/keyboards. This table can be selected using the kbdconfig tool. You should make the program active.
41. irqbalance: the Server Load balancer daemon for system interrupt requests in multiple system processor environments. If you only install one CPU, you do not need to load this daemon.
42. kshell: kshell daemon.
43. kudzu: hardware automatic detection program.
44. ldap: ldap (Lightweight Directory Access Protocol) Directory Access Protocol server daemon process.
45. lm_seroems: detects the working process of the motherboard.
46. lpd: lpd is an old-fashioned print daemon that submits lpr and other programs to print jobs.
47. mdmonitor: the daemon of RAID-related devices.
48. messagebus: The D-BUS is a library that provides one-to-one communication for two or more applications.
49. microcode_ctl: encodes and sends new microcodes to the kernel to update Intel IA32 series Processors.
50. mysqld: a fast, efficient, and reliable lightweight SQL database engine daemon process.
51. named: DNS (BIND) server daemon.
52. netplugd: The netplugd (network cable hotplug management daemon) daemon is used to monitor the status of one or more network interfaces and run an external script program when some events are triggered.
53. netdump: Remote network backup server daemon.
54. netfs: Network Filesystem Mounter. This process installs and uninstalls NFS, SAMBA, and NCP Network file systems.
55. nfs: Network File System daemon.
56. nfslock: NFS is a popular protocol for sharing files through TCP/IP networks. This daemon provides the NFS file locking function.
57. ntpd: Network time Protocol daemon (Network time correction Protocol ). Ntpd is a protocol daemon process used to synchronize time between the system and a precise time source.
58. network: enable/disable the network interface daemon at startup.
59. psacct: the daemon includes several tools used to monitor process activity, including ac, lastcomm, accton, and sa.
60. pcmcia: it is mainly used to support the daemon process of the laptop interface.
61. portmap: This daemon is used to support RPC connections. RPC is used for NFS, NIS, and other services.
62. postgresql: PostgreSQL relational database engine.
63. proftpd: proftpd is a dedicated ftp server daemon in Unix.
64. pppoe: ADSL connection daemon.
65. random: a high-quality random number generator for saving and restoring the system. These random numbers are provided by Uniform random behaviors.
66. rawdevices: used to load the daemon of raw devices when using the cluster file system.
67. readahead, readahead_early: readahead and readahead_early are two backend daemon programs launched in Fedora core 2. The function is to read the files used to start the system to the memory during system startup, and then execute the files in the memory to speed up the system startup speed.
68. rhnsd: Red Hat Network Service daemon. Notify the official security information and patch the system.
69. routed: This daemon supports the automatic IP route table maintenance of the RIP Protocol.
70. rsync: remote sync remote Data Backup daemon.
71. rsh: Start a shell on the remote host and execute USER commands.
72. rwhod: allows remote users to obtain a list of all logged-on users on the machines running the rwho daemon.
73. rstatd: A waiting process for other machines on the LAN to collect and provide system information.
74. ruserd: remote user location service, an RPC-based service that provides user information about a machine log currently recorded on the LAN
75. rwalld: activates the rpc. rwall service process. This is an RPC-based service that allows users to write messages to each terminal registered on the LAN machine.
76. rwhod: activates the rwhod service process. It supports rwho and ruptime services of the LAN.
77. saslauthd: Use the SASL authentication daemon.
78. sendmail: the sendmail daemon of the mail server.
79. smb: Samba file sharing/printing service daemon.
80. snmpd: The local Simple Network Management daemon.
81. squid: the squid daemon of the proxy server.
82. sshd: OpenSSH server daemon. Secure Shell Protocol enables Secure remote host management.
83. smartd: Self Monitor Analysis and Reporting Technology System, which monitors whether your hard disk is faulty.
84. syslog: a script that enables syslog and klogd system logs to wait for processes during system boot.
85. time: the daemon obtains the time and date from the remote host and uses the TCP protocol.
86. time-udp: This daemon obtains the time and date from the remote host and uses UDP protocol.
87. tux: run the daemon of the apache server in the Linux kernel.
88. vsftpd: the daemon of the vsftpd server.
89. vncserver: VNC (Virtual Network Computing, Virtual Network Computing ).
90. xfs: X Window server daemon, which provides a font set for local and remote X servers.
91. xinetd: supports core daemon processes of multiple network services.
92. ypbind: activates the ypbind service process for the NIS (Network Information System) client.
93. yppasswdd: the NIS Password server daemon.
94. ypserv: the NIS master server daemon.
95. yum: automatically upgrades the RPM operating system and manages the software package daemon.
You can determine whether to start a service based on your needs. For example, u and iptables must be run. Echo, echo-udp, daytime, daytime-udp, chargen, and chargen-udp can be disabled for debugging. Commands such as rsh, rstatd, rsync, rusersd, and rwalld are all Berkley remote commands. They all start with the letter r and are called the r * command. It is mainly used to allow a user on a computer to remotely execute a program on another computer with the same account. Do not close the program to reduce potential risks. Innd is a process that runs newsgroup services. Do not close it.
 
 
----------------------------------------------------------
 
Nohup tells the system to ignore the user's exit from the system.
Run the process until the process ends.
For example, I remotely open the ssh terminal to run the program./server
If nohup is not required, when I close the ssh terminal
./The server process will be aborted
Use nohup./server &
When the user exits the terminal
Process can continue execution
 

Related Article

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.