linux 设置开机启动项三种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务。
在解问题之前先来看看Linux的启动流程
Linux的启动流程
主要顺序就是:
1. 加载内核
2. 启动初始化进程
3. 确定运行级别
4. 加载开机启动程序
5. 用户登录
启动流程的具体细节可以看看Linux 的启动流程
第4步加载启动程序其实是两步:
- init进程逐一加载开机启动程序,其实就是运行指定目录里的启动脚本。
- 高级(只针对于centos7以上版本), 基于 Systemd , 命令: systemctl enable 服务名.service
参考连接 : https://my.oschina.net/sijiacheng/blog/5194143 - 在运行完指定目录里面的程序后init进程还会去执行/etc/rc.local 这个脚本。
ps:“指定目录”是指在第3步中设置的运行级别对应的目录。
要完成我们的需求,我们使用第4步中的任意一种方式都可以。
下面分别就是这两种方式的具体实现:
1.chkconfig
以supervisord服务脚本为例:
#!/bin/sh
##
## /etc/rc.d/init.d/supervisord
##
#supervisor is a client/server system that
# allows its users to monitor and control a
# number of processes on UNIX-like operating
# systems.
#
# chkconfig: - 64 36
# description: Supervisor Server
# processname: supervisord# Source init functions
. /etc/rc.d/init.d/functionsprog="supervisord"
prefix="/usr/"
exec_prefix="${prefix}"
PIDFILE="/var/run/supervisord.pid"
CONFIG="/etc/supervisord.conf"
prog_bin="${exec_prefix}bin/supervisord -c $CONFIG "function log_success_msg() {echo "$@" "[ OK ]"
}function log_failure_msg() {echo "$@" "[ OK ]"
}start()
{#echo -n $"Starting $prog: "#daemon $prog_bin --pidfile $PIDFILE#[ -f $PIDFILE ] && success $"$prog startup" || failure $"$prog failed"#echoif [ ! -r $CONFIG ]; thenlog_failure_msg "config file doesn't exist (or you don't have permission to view)"exit 4fiif [ -e $PIDFILE ]; thenPID="$(pgrep -f $PIDFILE)"if test -n "$PID" && kill -0 "$PID" &>/dev/null; then# If the status is SUCCESS then don't need to start again.log_failure_msg "$NAME process is running"exit 0fifilog_success_msg "Starting the process" "$prog"daemon $prog_bin --pidfile $PIDFILElog_success_msg "$prog process was started"}
stop()
{echo -n $"Shutting down $prog: "[ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"echo
}case "$1" instart)start;;stop)stop;;status)status $prog;;restart)stopstart;;*)echo "Usage: $0 {start|stop|restart|status}";;esac
第1步:把上面的脚本放在/etc/init.d/文件夹下。
ln -s ./supervisord /etc/init.d/supervisord
第2步:将启动脚本权限改为可执行。
chmod a+x /etc/init.d/supervisord
第3步:添加启动项。
chkconfig --add supervisord
chkconfig supervisord on
第4步:检查是否设置成功。
chkconfig --list | grep supervisord
supervisord 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
成功~
2.修改/etc/rc.local脚本
/etc/rc.local 脚本内容如下
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.#touch /var/lock/subsys/local
echo "hello linux" >> /tmp/hello2.loginfluxd > /tmp/influxd.log 2>&1 &echo "hello linux" >> /tmp/hello3.log
echo “hello linux” >>/tmp/hello2.log ,就模拟了一个简单的开机启动脚本。
influxd 则就是启动 influxd 服务。
ps: influxd > /tmp/influxd.log 2>&1 &
这样写的意思是让influxd后台执行。
influxd和前面的echo "hello linux"
是不一样的,echo 执行过后就结束了,而influxd则为服务一直执行,如果不后台执行的话则influxd 启动后就不会返回,那么init进程就会一直等待influxd执行完毕,导致后面的程序一直无法执行。
这个着实坑了我一把,当时我写的是:
#!/usr/bin/python
...
influxd
telegraf
发现influxd启动成功了,telegraf就是起不来。后来把telegraf写在前面就能起来,但是influxd又起不来了于是就猜测是这个原因~~~bingo。
linux 设置开机启动项三种方式相关推荐
- linux 设置开机启动项两种方式
有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务. 在解问题之前先来看看Linux的启动流程 Linux的启动流程 主要顺序就是: 1. 加载内核 2. 启动初始化进程 3. 确定运 ...
- Linux 设置开机启动项的几种方法
Linux 设置开机启动项的几种方法 方法一:编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本. 所以我们可以直接在/etc/rc.local中添加启动脚本. ...
- C# 将程序添加开机启动的三种方式
前言 最近在研究程序随系统启动,发现在 win7 上因为权限的问题,写注册表的时候总是会出现问题,写不进去导致的不能自动启动,随后决定仔细的看一看这方面的问题. 查资料过程中主要发现有三种方式可以添加 ...
- linux设置开机自启动的三种方法
一.rc.local文件中添加自启动命令 1.执行命令: 编辑"/etc/rc.local" vi /ect/rc.local 2.然后在文件最后一行添加要执行程序的全路径. 例如 ...
- Linux设置开机启动
Linux设置开机启动有几种方式,今天就来讨论以下几种方式. 方法一:编辑rc.loacl脚本 直接在/etc/rc.local中添加启动脚本. $ vim /etc/rc.local 方法二:Lin ...
- linux 添加开机启动项的三种方法。
原文地址: https://blog.csdn.net/lylload/article/details/79488968 Shell环境变量配置文件:https://blog.csdn.net/yzs ...
- linux 添加开机启动项的三种方法
linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh # # This sc ...
- Linux 添加开机启动项的三种方法
linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh # # This sc ...
- linux设置开机自启服务,linux设置服务开机自启动的三种方式
linux设置服务开机自启动的三种方式 这里介绍一下linux开机自动启动的几种方法,共计3种,大家可以借鉴一下!经验里面以centos 5.3系统为例! 方法1:.利用ntsysv命令进行设置,利用 ...
最新文章
- 11月29日云栖精选夜读:阿里传奇工程师多隆的程序世界
- 空扫描Idle Scanning
- python 同时给多个变量赋值
- 如何让搜索引擎抓取AJAX内容
- fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
- 二叉树的基本特性和二叉树的几种基本操作的机制_深入理解二叉树01 二叉树基础
- final 在 java 中有什么作用?
- 登录验证应该是进行在客户端还是服务器端_网站登录认证方式
- 【华为云技术分享】敏捷DevOps知识卡大全(内附下载资料)
- 我的内核学习笔记2:platform设备模型
- docker php伪静态无效,解决Docker network Create加--subnet后遇到问题
- asp.net js 提示信息封装函数
- PHP7.1安装memcaehd扩展
- python linux开发_python之Linux开发环境安装
- 华为eNSP-基本配置指令
- Hello CSDN blog
- 银联支付,实现代码对接
- 阿里云思维导图系列(一)开篇
- 玩转电脑常用的140个技巧
- bat 批处理 B站缓存视频转成 .mp4格式
热门文章
- Android 驱动CH340实现和单片机串口通信
- 从放弃本专业,到直播编程,这女孩如何做到的?
- 前端可排程的vue 排程甘特图
- QueryWrapper 或用法.or()
- 阿里云环境下搭建HadoopHA集群
- C语言笔记--代码学习笔记--C语言语法--基本操作运算-basic-logorithm
- Power Automate 中的 SharePoint Trigger Conditions 配置
- 彻底退出,刘强东转让所持京东股份;​芯片巨头高通宣布冻结招聘;Rust 1.65.0 稳定版发布|极客头条
- base64图片展示(后端给base64数据,前端展示图片)
- 带你一起瞧瞧自定义属性以及自定义View的使用