centos7 系统启动自动执行shell脚本

一、创建启动脚本

$cd {your path}

内容参考:

# cd /root/clouddevice
# touch start.sh
# vi start.sh

 start.sh内容参考:

#!/bin/bash
echo '准备启动 frp...'
cd /root/clouddevice/frp/ && nohup /root/clouddevice/frp/frps -c /root/clouddevice/frp/frps.ini > frps.log & &&
echo 'frp启动完毕!'
echo '准备启动 rethinkdb及stf...'
cd /root/clouddevice/stf 
rethinkdb &
nohup stf local --public-ip 192.168.137.100 --bind-dev-pull tcp://0.0.0.0:7114 --bind-dev-pub tcp://0.0.0.0:7116 -R > stf.log & echo 'rethinkdb及stf 启动完毕!'

 注意点:
1、.sh的脚本开头有#!/bin/bash
2、很多启动脚本都涉及日志输出,建议配置的命令最好进入对应目录, 然后再执行脚本
二、在开机自启动文件中加入启动执行目录

# sudo vim /etc/rc.d/rc.local 

 /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
su - developer -c 'cd /root/clouddevice/ && sh start.sh'

 在centos7中,/etc/rc.d/rc.local文件的权限被降低了,没有执行权限,需要给它添加可执行权限,赋予自启动文件执行权限:

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

注意点:
1、启动的用户如果都以root启动, 其他用户不方便操作root留下的进程以及生成的文件, 建议切换到对应权限的用户执行启动文件。
2、切换用户命令:

# su - username -c  //username:系统登录用户名



原文地址:https://www.cnblogs.com/Tanwheey/p/14816645.html