Centos 设置开机自动启动脚本

Centos 设置开机自动启动脚本

实现开机自启动的脚本非常简单,简单的写一个shell脚本和配置一下rc.local 文件就行
例如我的start.sh脚本文件 是这样的,利用uwsgi开启一个web端口

1.编写开机脚本

vi /start.sh
nohup uwsgi --http :8001 --wsgi-file test.py &

这里加上nohup ,nohup的作用是在centos后台运行,后面一定要加&符号,否则有可能会造成centos重启卡在进度条的那个界面,后面会讲如何解决这个问题

2.配置rc.local文件

vi /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
/start.sh &

我是在最后一行加上**/start.sh &**这行命令,最后也不能少了&符号

3.配置文件权限

如果你在启动脚本,保存脚本或者配置文件有权限的话,那么需要赋予一下文件权限,在rc.local文件中的注释也详细提到过

Centos 设置开机自动启动脚本_第1张图片

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

脚本文件也需要赋予一下权限

chmod +x /start.sh

这样就完成了开机后台自启脚本

4.一些错误排除

在开放web端口的时候可能会访问不通,那么可以选择开放端口或者永久关闭防火墙

查看防火墙状态
systemctl status firewalld.service
关闭防火墙
systemctl stop firewalld.service
永久关闭防火墙
systemctl disable firewalld.service

如果遇到了centos重启后卡在进度条的情况下,可以进入单用户模式,暂时把rc.local后台自启动的命令注释或者删除掉
在这里插入图片描述

开机时,大概启动到这里按"E"键,也可以提前就按着。
Centos 设置开机自动启动脚本_第2张图片
然后再这个位置加入 init=/bin/bash,
Centos 设置开机自动启动脚本_第3张图片
然后按 ctrl+x 进入单用户模式

然后编辑rc.local的时候可能会遇到权限不够,输入如下命令即可编辑
**加粗样式

mount -o remount,rw /

重新编辑你的rc.local文件后重启后就好了

你可能感兴趣的:(数据库运维,服务器,centos)