Linux boot program rc.local

In CentOS7, there are two ways to boot main program:

1) To start the program configuration system to customize the service, which I have introduced, read: CentOS7 add a custom system services .

2) Scripting start the program in the /etc/rc.local script file, the article describes this method in detail.

1, / etc / rc.local is /etc/rc.d/rc.local soft link

Perform ls -l /etc/rc.localand see.
Here Insert Picture Description
/etc/rc.local is a soft link /etc/rc.d/rc.local file, which means that they are the same file.

Original content 2, rc.local file

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
#
touch /var/lock/subsys/local

Chinese meaning is as follows:

# 添加此文件是为了兼容。
# 强烈建议创建自己的systemd服务或udev规则,以便在引导期间运行脚本,而不是使用此文件。
# 与以前版本不同,由于在引导期间并行执行,此脚本不会在所有其他服务之后运行。
# 请注意,必须运行'chmod+x/etc/rc.d/rc.local',以确保在引导期间执行此脚本。

Understand.

Although Linux is strongly recommended custom service system boot from the start the program, but I think the start-up program is also a good way to configure in the rc.local, because rc.local configuration more straightforward, it is still widely used .

3, the configuration file rc.local

Is a shell script file rc.local on nature, you can put the command to be executed when you start to write in it, the order will be executed at startup.

Now let's test it.

1) Add the following script in the rc.local.

/usr/bin/date >> /tmp/date1.log # 把当前时间追加写入到/tmp/date1.log中。
/usr/bin/sleep 10 # 睡眠10秒。
/usr/bin/date >> /tmp/date2.log # 把当前时间追加写入到/tmp/date2.log中。

2) modify the /etc/rc.d/rc.local executable permissions.

chmod +x /etc/rc.d/rc.local

3) restart the server.

4) Check the log file /tmp/date1.log and content of /tmp/date2.log.
Here Insert Picture Description

4, application experience

1) rc.local script is executed only once when the operating system starts.

2) environmental variables.

In rc.local script execution procedure is not environment variables, if you execute the program needs an environment variable, you can set the environment variable in the script, you can also use su to switch user to perform, for example:

su - oracle -c "sqlplus scott/tiger @/tmp/test.sql"

The meaning of the above command is executed again as the oracle user login sqlplus command.

3) Do not let the rc.local hang.

rc.local is a script that is executed in order, will execute the next program After executing a program, if a program is not a daemon, you should add & let the program run in the background, otherwise rc.local hang.

The following script can be used to test the contents rc.local as follows:

/usr/bin/date >> /tmp/date1.log  # 把当前时间追加写入到/tmp/date1.log中。
/usr/bin/sleep 100 # 睡眠100秒。
/usr/bin/date >> /tmp/date2.log  # 把当前时间追加写入到/tmp/date2.log中。

If the script above, Linux system after completion of start 100, the following login screen will appear.
Here Insert Picture Description

5. Copyright

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.

Source: C Language Technology Network ( www.freecplus.net )

Author: Ethics Code farming

Published 159 original articles · won praise 458 · views 120 000 +

Guess you like

Origin blog.csdn.net/wucz122140729/article/details/105165912