CentOS startup custom script details and implementation

  • 2020-05-13 04:24:46
  • OfStack

CentOS boot starts the custom script

Sometimes we need to set up a script in the server, so that it will start itself when it is turned on. The method is as follows:

cd /etc/ init.d vi youshell.sh # change youshell.sh to your own script name

Save and exit after writing your own script.

When writing a script, please add in a ***

A brief description of the script #add for chkconfig #chkconfig: #description: # first process name, which is used later when setting up from startup

Description:

2345 refers to the running level of the script, that is, it can run in all four modes of 2345, 234 is a text interface, and 5 is a graphical interface X

70 refers to the future startup sequence number of the script. If the startup sequence number of other programs is less than 70 (such as 44, 45), the script needs to wait until all of these programs are started.

30 is the stop sequence number of the script when the system is down.

Add executable permissions to the script:

chmod +x youshell.sh

Set the script to start from scratch using the chkconfig command

chkconfig --add servicename

This will allow your script to run automatically after booting up.
In addition, this method can also be used in redhat to enable startup.

How to use chkconfig

chkconfig(check config)

Function description: check and set up various services of the system.
Grammar: chkconfig
[-- add] [-- del] [-- list] [service] or chkconfig [- level < Level code > ][system services][on/off/reset]
Note: this is a program developed by RedHat following the rules of GPL. It can query which system services will be executed by the operating system in each execution level, including various resident services.

Parameters:
Add the specified system service to enable the chkconfig directive to manage it, and add the relevant data to the system startup narrative file at the same time.
The service is no longer managed by the chkconfig directive, and the data is deleted from the description file for system startup.
� level < Level code > At which execution level does the reader service need to be turned on or off

The 1: chkconfig command can also be used to activate and deactivate services. The chkconfig hang list command displays the list of system services and whether they have been started (on) or stopped (off) at runlevels 0 to 6.
chkconfig � list
chkconfig � list httpd
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

2: chkconfig can also be used to set whether a particular 1 service is started or stopped at a particular 1 runtime level. For example, to disable the nscd service at runlevels 3, 4, and 5, use the following command:
chkconfig, level 345, nscd off

3: services managed by xinetd are immediately affected by chkconfig. For example, if xinetd is running and finger is disabled, then after the chkconfig finger on command is executed, finger does not have to manually restart xinetd to be enabled immediately. Changes to other services will not take effect immediately after chkconfig is used. You must use the service servicename start/stop/restart command to restart the service

The second way

Edit/etc/rc. d/rc. local file
The format is the program name and the program path
For example a. sh/home/a. sh

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: