Ubuntu 16.04设置rc.local开机启动命令/脚本的方法

     
Ubuntu 16.04设置rc.local开机启动命令/脚本的方法(通过update-rc.d管理Ubuntu开机启动程序/服务)

注意:rc.local脚本里面启动的用户默认为root权限。

一、rc.local脚本

rc.local脚本是一个Ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令。该脚本位于/etc/路径下,需要root权限才能修改。

该脚本具体格式如下:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing. exit 0

注意: 一定要将命令添加在exit 0之前。里面可以直接写命令或者执行Shell脚本文件sh。

二、关于放在rc.local里面时不启动的问题:

1、可以先增加日志输出功能,来查看最终为什么这个脚本不启动的原因,这个是Memcached启动时的样例文件:

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing. #log
exec 2> /tmp/rc.local.log # send stderr from rc.local to a log file
exec 1>&2 # send stdout to the same log file
set -x #
tell sh to display commands before execution #Memcached
/usr/local/memcache/bin/memcached -p 11211 -m 64m -d -u root
exit 0

2、rc.local文件头部/bin/sh修改为/bin/bash

3、如果是执行sh文件,那么要赋予执行权限sudo chmod +x xxx.sh,然后启动时加上sudo sh xxx.sh

三、 update-rc.d增加开机启动服务

给Ubuntu添加一个开机启动脚本,操作如下:

1、新建个脚本文件new_service.sh

#!/bin/bash
# command content exit 0

2、设置权限

sudo chmod 755 new_service.sh
#或者
sudo chmod +x new_service.sh

3、把脚本放置到启动目录下

sudo mv new_service.sh /etc/init.d/

4、将脚本添加到启动脚本

执行如下指令,在这里90表明一个优先级,越高表示执行的越晚

cd /etc/init.d/
sudo update-rc.d new_service.sh defaults 90

5、移除Ubuntu开机脚本

sudo update-rc.d -f new_service.sh remove

6、通过sysv-rc-conf来管理上面启动服务的启动级别等,还是开机不启动

sudo sysv-rc-conf 

7、update-rc.d的详细参数

使用update-rc.d命令需要指定脚本名称和一些参数,它的格式看起来是这样的(需要在 root 权限下):

update-rc.d [-n] [-f] <basename> remove
update-rc.d [-n] <basename> defaults
update-rc.d [-n] <basename> disable|enable [S|2|3|4|5]
update-rc.d <basename> start|stop <NN> <runlevels>
-n: not really
-f: force

其中:

  • disable|enable:代表脚本还在/etc/init.d中,并设置当前状态是手动启动还是自动启动。
  • start|stop:代表脚本还在/etc/init.d中,开机,并设置当前状态是开始运行还是停止运行。(启用后可配置开始运行与否)
  • NN:是一个决定启动顺序的两位数字值。(例如90大于80,因此80对应的脚本先启动或先停止)
  • runlevels:则指定了运行级别。

实例:

(1)、添加一个新的启动脚本sample_init_script,并且指定为默认启动顺序、默认运行级别(还记得前面说的吗,首先要有实际的文件存在于/etc/init.d,即若文件/etc/init.d/sample_init_script不存在,则该命令不会执行):

update-rc.d sample_init_script defaults

上一条命令等效于(中间是一个英文句点符号):

update-rc.d sample_init_script start 20 2 3 4 5 . stop 20 0 1 6

(2)、安装一个启动脚本sample_init_script,指定默认运行级别,但启动顺序为50:

update-rc.d sample_init_script defaults 50

(3)、安装两个启动脚本A、B,让A先于B启动,后于B停止:

update-rc.d A 10 40
update-rc.d B 20 30

(4)、删除一个启动脚本sample_init_script,如果脚本不存在则直接跳过:

update-rc.d -f sample_init_script remove

这一条命令实际上做的就是一一删除所有位于/etc/rcX.d目录下指向/etc/init.d中sample_init_script的链接(可能存在多个链接文件),update-rc.d只不过简化了这一步骤。

(5)禁止Apache/MySQL相关组件开机自启:

update-rc.d -f apache2 remove
update-rc.d -f mysql remove

8、服务的启动停止状态

#通过service,比如
sudo service xxx status
sudo service xxx start
sudo service xxx stop
sudo service xxx restart

9、查看全部服务列表

sudo service --status-all

Ubuntu 16.04设置rc.local开机启动命令/脚本的方法的更多相关文章

  1. Ubuntu 16.04设置rc.local开机启动命令/脚本的方法(通过update-rc.d管理Ubuntu开机启动程序/服务)

    注意:rc.local脚本里面启动的用户默认为root权限. 一.rc.local脚本 rc.local脚本是一个Ubuntu开机后会自动执行的脚本,我们可以在该脚本内添加命令行指令.该脚本位于/et ...

  2. centos7设置rc.local开机执行命令

    在Centos7下,rc.local文件,开机默认是不执行的 它是个软链接 [root@data-1-1 ~]# ll /etc/rc.local lrwxrwxrwx. 1 root root 13 ...

  3. Ubuntu 16.04设置Redis为开机自动启动服务

    继上一篇文章http://www.cnblogs.com/EasonJim/p/7599941.html安装好Redis后,假设文件已经安装到/usr/local/redis目录下.假设我安装的版本为 ...

  4. ubuntu 16.04 小键盘数字键盘开机自动启动

    ubuntu 16.04 小键盘数字键盘开机自动启动 最近安了ubuntu 16.04,用windows用久了,换一个也挺好玩的! 但ubuntu 16.04因为算是最新的吧,还是存在些令我们不适应的 ...

  5. ubuntu 18.04 配置 rc.local

    ubuntu 18.04 配置 rc.local:https://blog.csdn.net/a912952381/article/details/81205095 Ubuntu /etc/rc.lo ...

  6. ubuntu 16.04 安装 Matlab R2016b后启动出现的问题

    (1)报以下错误: License checkout failed.License Manager Error -95MATLAB is unable to connect to the licens ...

  7. Ubuntu 16.04 设置MySQL远程访问权限

    本文记录一下在Ubuntu 16.04版本下设置MySQL数据库的远程访问. 第一步:修改配置文件的端口绑定 打开的目录可能会根据MySQL的版本稍有不同,可以先尝试打开/etc/mysql/my.c ...

  8. Debian 9.x "stretch" 解决 /etc/rc.local 开机启动问题

    由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐添加命令到 /etc/rc.local 文件,但是 Debian 9 默认不带 /etc/rc.local 文件,而 rc. ...

  9. Ubuntu 18.04 启用 rc.local 设置开机启动

    ubuntu18.04 不再使用initd管理系统,改用systemd. 然而systemd很难用,改变太大,跟之前的完全不同. 使用systemd设置开机启动为了像以前一样,在/etc/rc.loc ...

随机推荐

  1. 【洛谷P2114】起床困难综合征 位运算+贪心

    题目大意:给定 N 个操作,每个操作为按位与.或.异或一个固定的数字,现在要求从 0 到 M 中任选一个数字,使得依次经过 N 个操作后的值最大. 题解:位运算有一个重要的性质是:位运算时,无进位产生 ...

  2. c++并发编程之互斥锁(mutex)的使用方法

    1. 多个线程访问同一资源时,为了保证数据的一致性,最简单的方式就是使用 mutex(互斥锁). 引用 cppreference 的介绍: The mutex class is a synchroni ...

  3. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. ;i<=q;i++) { scanf("%ld%ld% ...

  4. [转]Robotium环境搭建中的Errors running builder 'Android Resource Manag

    转自:http://blog.sina.com.cn/s/blog_68f262210102v75t.html 最近学习了Robotium测试框架,当然学习任何一个框架或是语言之前,第一步就是搭建环境 ...

  5. pthread_detach pthread_create实例

    //pool.h 1 #ifndef POOL_H #define POOL_H #include <pthread.h> class pool { public: pool(); ~po ...

  6. nginx配置自动跳转

    阅读更多 希望实现的效果是,用户只要访问域名,自动跳转到index.html页面 原本配置为: location / { root   /users/apple/git_local/YAE/YAE/f ...

  7. bzoj千题计划236:bzoj2300: [HAOI2011]防线修建

    http://www.lydsy.com/JudgeOnline/problem.php?id=2300 维护动态凸包,人懒用的set 用叉积判断,不要用斜率 #include<set> ...

  8. LaTeX字体设置

    % 导言区 % 帮助文档 texdoc lshort-zh % 设置normalsize大小 \documentclass[10pt]{ctexart} %article,ctexbook封面, ct ...

  9. Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang

    Liberty Mutual Property Inspection, Winner's Interview: Qingchen Wang The hugely popular Liberty Mut ...

  10. Eclipse的个性化设置

    Eclipse的个性化设置 1. 在Eclipse中查看JDK源码的配置 a. 点 “window”-> "Preferences" -> "Java&quo ...