原文链接:http://blog.csdn.net/karchar/article/details/52489572

有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务。

在解问题之前先来看看Linux的启动流程

Linux的启动流程

主要顺序就是: 
1. 加载内核 
2. 启动初始化进程 
3. 确定运行级别 
4. 加载开机启动程序 
5. 用户登录

启动流程的具体细节可以看看Linux 的启动流程

第4步加载启动程序其实是两步:

  1. init进程逐一加载开机启动程序,其实就是运行指定目录里的启动脚本。
  2. 在运行完指定目录里面的程序后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/functions prog="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"
#echo
if [ ! -r $CONFIG ]; then
log_failure_msg "config file doesn't exist (or you don't have permission to view)"
exit 4
fi if [ -e $PIDFILE ]; then
PID="$(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 0
fi
fi log_success_msg "Starting the process" "$prog"
daemon $prog_bin --pidfile $PIDFILE
log_success_msg "$prog process was started" }
stop()
{
echo -n $"Shutting down $prog: "
[ -f $PIDFILE ] && killproc $prog || success $"$prog shutdown"
echo
} case "$1" in start)
start
;; stop)
stop
;; status)
status $prog
;; restart)
stop
start
;; *)
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.log influxd > /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 设置开机启动项两种方式的更多相关文章

  1. Linux设置开机启动项

    第一种方式:ln -s 建立启动软连接 在Linux中有7种运行级别(可在/etc/inittab文件设置),每种运行级别分别对应着/etc/rc.d/rc[0~6].d这7个目录 Tips:/etc ...

  2. Linux 设置开机启动项的几种方法

    方法一:编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚本. 所以我们可以直接在/etc/rc.local中添加启动脚本. $ vim /etc/rc.loca ...

  3. Linux 添加开机启动项的三种方法

    linux 添加开机启动项的三种方法. (1)编辑文件 /etc/rc.local 输入命令:vim /etc/rc.local 将出现类似如下的文本片段: #!/bin/sh## This scri ...

  4. 原创:四种Linux系统开机启动项优命令超给力超详细详解

    老葵花哥哥又开课了 接下来是你们的齐天大圣孙悟空给你们带来的详细版Linux系统开机启动优化四种命令 第一种方法是很正常的 第二种有点难理解 第三种来自我的一个奇思妙想 本文档秉承 不要钱也不要臀部的 ...

  5. 源码编译安装nginx及设置开机启动项

    1.上传nginx文档:解压到/data目录下,并安装依赖包tar xf nginx-1.20.1.tar.gz -C /data/cd /data/nginx-1.20.1/ && ...

  6. HTML中设置背景图的两种方式

    HTML中设置背景图的两种方式 1.background    background:url(images/search.png) no-repeat top; 2.background-image ...

  7. linux添加开机启动项、登陆启动项、定时启动项、关机执行项等的方法

    使用chkconfig命令可以查看在不同启动级别下课自动启动的服务(或是程序),命令格式如下: chkconfig --list 可能输出如下: network         0:off   1:o ...

  8. Android系统移植与调试之------->如何修改开机动画的两种方式剖析

    首先,我们先来分析一下源码: frameworks/base/cmds/bootanimation/BootAnimation.cpp 首先看一下定义的常量: BootAnimation::ready ...

  9. Linux->ZooKeeper开机启动的俩种方式

    两种方式可以实现开机自启动 第一种:直接修改/etc/rc.d/rc.local文件 在/etc/rc.d/rc.local文件中需要输入两行, 其中export JAVA_HOME=/usr/jav ...

随机推荐

  1. BZOJ3747:[POI2015]Kinoman——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3747 https://www.luogu.org/problemnew/show/P3582 共有 ...

  2. switch语法的盲点

    switch语法在项目使用的频率很低,今天看到一个相关的例子引发一些思考,,同时自己也写了一些简单的例子如下: 实例1: int dayOfWeek = 5; switch (dayOfWeek){ ...

  3. [mysql]数据库引擎查看

    1.查看数据库引擎 全局下,show engines; 2.察看数据库引擎 show variables like '%engine%'; 或者show create table xxx\G 会显示默 ...

  4. phantomjs 无法打开https网站

    最近一直在用phantomjs 自动登陆并爬取一些数据,突然发现爬取https类型的网站的时候无法正常操作了 困扰了两天的问题在经过google和stackoverflow的一番搜索后发现原来Phan ...

  5. arm-linux-gcc等交叉编译工具的安装

    1.软件安装 步骤1:打开虚拟机,在/usr/local/下创建/usr/local/arm文件夹(一般用户自定义程序放到这里) 步骤2:先将安装包从Windows中弄到linux中去.可以用共享文件 ...

  6. idea 创建多模块时模块类无法引入

    我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...

  7. sun.security.x509.CertAndKeyGen;找不到

    导入已有项目编译时出错,报: import sun.security.x509.CertAndKeyGen;找不到 而这个包属于sun公司的jar包.不是项目本身的问题,而是开发环境的问题. 最后原因 ...

  8. 应用Xml.Linq读xml文件

    c#提供了System.Xml.Linq操作xml文件,非常方便,本文主要介绍如何应用System.Xml.Linq读取xml文件. xml文本 <?xml version="1.0& ...

  9. 东方14ACM小组 Challenge 11

    总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  262144kB 描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求 ...

  10. 51Nod 1007 正整数分组 | DP (01背包)

    Input示例 5 1 2 3 4 5 Output示例 1 分析:2组的差最小,那么每一组都要接近sum/2,这样就转化成了普通的0 - 1背包了 #include <bits/stdc++. ...