There are two mechanisms in Linux: one is the old SystemVinitialization, which comes from Unix and is still used by various Linuxdistros; the other is the Upstart mechanism proposed in recent years. Refer to: http: wwwcnblogscomcassvinarchive2011122. There are two mechanisms in Linux: one is the old-style System V initialization, which is derived from Unix and is still used by various Linux distros; the other is the Upstart mechanism proposed in recent years.
Reference: http://www.cnblogs.com/cassvin/archive/2011/12/25/ubuntu_init_analysis.html
This can also be said to be a pain point in Linux. There are also two sets of startup methods, and some software uses the System V initialization mechanism, and some use the Upstart mechanism. Different releases adopt different mechanisms and even different configuration tools .. For example, apache uses the former, mysql, and mongodb uses the latter.
System V initialization mechanism
Reference: http://zh.wikipedia.org/wiki/Init
Default Operating modes of different operating systems
| Operating system |
Default running mode |
| AIX |
2 |
| Arch Linux |
3 |
| CentOS |
3 |
| Debian GNU/Linux |
2[5] |
| Gentoo Linux |
3 |
| Mandriva Linux |
5 |
| Mac OS X |
3 |
| Red Hat Linux/Fedora Core |
3 or 5 |
| Slackware Linux |
3 |
| Solaris |
3[6] |
| SUSE Linux |
5[7] |
| Ubuntu (Server and Desktop) |
2[8] |
The System V initialization mechanism has a folder for each running mode to store the scripts used for startup. In the/etc directory, you can see rc0.d, rc1.d... rc5.d, RC. d. d .. Therefore, if you want to add a startup item, you must first add your own script under/etc/init. d, and then create soft links in directories such as rc0.d, which is very troublesome.
Therefore, Linux releases usually have tools to automatically generate these soft link configurations.
The System V initialization mechanism has another problem. it uses the serial number to indicate the start and stop sequence. For example, in/etc/rc0.d, we can see all scripts starting with S or K, S is start, and K is kill. The numbers after S/K indicate the order. The value ranges from small to large at startup and from large to small at stop. For example, K09apache2 indicates that apache2 will be stopped in the order of 09.
Configure startup items in CentOS
CentOS uses the System V initialization mechanism.
The tool used in CentOS is chkconfig.
Ubuntu is compatible with System V initialization and Upstart.
For the System V initialization mechanism under Ubuntu, the tool used is the update-rc.d,
You need to manually modify the configuration of the Upstart mechanism. However, an initctl tool can be used to start, stop, and view related processes.
For CentOS, configuring a startup service is still relatively simple. for example, configuring zookeeper to automatically start the service:
Create a script under/etc/init. d:
#/bin/sh #chkconfig: 2345 20 80 # description: zookeeper1case $1 in start) /home/zookeeper/zookeeper345_1/bin/zkServer.sh start ;; stop) /home/zookeeper/zookeeper345_1/bin/zkServer.sh stop;; status) /home/zookeeper/zookeeper345_1/bin/zkServer.sh status;; restart) /home/zookeeper/zookeeper345_1/bin/zkServer.sh restart;; *) echo "require start|stop|status|restart" ;;esac
This script is very simple, that is, to determine the content of the first parameter and then execute the corresponding program.
Note that the two rows of chkconfig must have the following configurations:
#chkconfig: 2345 20 80 #description: zookeeper1
#2345 after chkconfig indicates the system startup mode (runlevel), 20 indicates the start sequence, and 80 indicates the stop sequence.
Add the executable permission:
chmod +x /etc/init.d/zookeeper1chkconfig --add zookeeper1
Cancel startup item:
chkconfig zookeeper off
Delete a startup item:
chkconfig --del zookeeper1
Configure the startup item System V initialization mechanism in Ubuntu
For Ubuntu, if the System V initialization mechanism is used, you can refer to the chkconfig script above and set it with the update-rc.d command:
update-rc.d zookeeper1 defaults
Cancel startup item:
update-rc.d zookeeper1 disable
Delete a startup item:
update-rc.d -f zookeeper1 remove
Upstart mechanism
The Upstart mechanism is quite complex. For more information, see http://upstart.ubuntu.com/cookbook.
If the Upstart mechanism is used, you can directly create a script ending with ". conf" in the/etc/init Directory, for example, zookeeper1.conf:
# Set runlevelstart on runlevel [2345] stop on runlevel [! 2345] # set automatic restart respawnrespawn limit 2 5 umask 007 kill timeout 30 # set the start user setuid zookeepersetgid zookeeper # note that the start-foreground parameter is used, this will run the zookeeper process on the foreground, so that upstart can control the process scriptexec/home/hengyunabc/soft/zookeeper-3.4.5/bin/zkServer. sh start-foregroundend script
Note that the log of zookeeper is written to/var/log/upstart/zookeeper1.log.
If you want to use the zkServer. sh start parameter to start the program, it is troublesome because upstart cannot be associated with java processes and can only be associated with bash processes. Upstart does not seem to be suitable even though it has some parameters of the same CT daemon and minus Ct fork.
Can refer to this post: http://stackoverflow.com/questions/12200217/can-upstart-expect-respawn-be-used-on-processes-that-fork-more-than-twice
It mentions a method to detect the survival of processes at a scheduled time. if the process does not survive, it is started. You can use this method to handle script calls.
Alternatively, start the java process directly in the upstart script. you can refer to the upstart document or here:
Http://zookeeper-user.578899.n2.nabble.com/Zookeeper-run-as-non-root-td7577797.html
Summary
Compared with the System V initialization mechanism, the Upstart mechanism is powerful. for example, you can customize the trigger processing of various events, but it is not easy to configure in case of complexity.
The status of Upstart can only determine whether the process is alive (and not necessarily accurate), while System V initialization can flexibly call its own scripts to obtain more detailed state information of the process.
Other things:
Supervisor, python-written process management tools: http://supervisord.org/
Reference
Http://www.debian.org/doc/manuals/debian-reference/ch03.en.html
Https://github.com/LucidWorks/solr-fabric/blob/master/templates/zookeeper-upstart.conf