这里提供两种方式(第一种更直接快捷):
一、通过编辑添加命令到/etc/rc.d/rc.local 即可
[root@ald8 ]# vim /etc/rc.d/rc.local
#!/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
# 要开机执行的命令或脚本
mkdir /tmp/111 /tmp/222 -p
/tmp/test.sh
[root@ald8 ]# chmod +x /etc/rc.d/rc.local (这里记得执行这条命令加上执行权限)
二、通过在/etc/init.d目录下创建个shell脚本
[root@ald8 ]# vim /etc/init.d/test_run (脚本文件名自定义)
#!/bin/bash
###############
# SysV Init Information
# chkconfig: - 58 74
### BEGIN INIT INFO
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
#-1--------------要运行的命令或脚本
dir='/tmp'
mkdir $dir/ontest -p
touch $dir/ontest/111
cat /etc/passwd > $dir/mypasswd
/tmp/test.sh
[root@ald8 ]# chkconfig --add /etc/init.d/test_run (添加到开机自启服务)