转载, 并记下我的使用心得。

Linux services can be started, stopped and reloaded with the use of scripts stocked in /etc/init.d/. However, during start up or when changing runlevel, those scripts are searched in /etc/rcX.d/ where X is the runlevel number. This tutorial will explain how one can activate, deactivate or modify a service start up. When installing a new service under debian, the default is to enable it. So for instance, if you just installed apache2 package, after you installed it, apache service will be started and so will it be upon the next reboots. If you do not use apache all the time, you might want to disable this service from starting up upon boot up and simply start it manually when you actually need it by running this command:

/etc/init.d/apache2 start

You could either disable this service on boot up by removing any symbolic links in /etc/rcX.d/SYYapache2 or by using update-rc.d. The advantage of using update-rc.d is that it will take care of removing/adding any required links to /etc/init.d automatically. Taking apache2 as an example, let’s examine how /etc/rcX.d is looking like:

# ls -l /etc/rc?.d/*apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc0.d/K91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc1.d/K91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc2.d/S91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc3.d/S91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc4.d/S91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc5.d/S91apache2 -> ../init.d/apache2

lrwxrwxrwx 1 root root 17 2007-07-05 22:51 /etc/rc6.d/K91apache2 -> ../init.d/apache2

As you can see, for runlevels 0, 1 and 6 there is a K at the beginning of the link, for runlevels 2, 3, 4 and 5, there is a S. Those two letters stands for Kill and Start. On Debian and Ubuntu, runlevels 2, 3, 4 and 5 are multi-users runlevels. Runlevel 0 is Halt. Runlevel 1 is single user mode Runlevel 6 is reboot

1. Removing A Service

If you want to totally disable apache2 service by hand, you would need to delete every single link in /etc/rcX.d/. Using update-rc.d it is as simple as:

update-rc.d -f apache2 remove

The use of -f is to force the removal of the symlinks even if there is still /etc/init.d/apache2.

Note: This command will only disable the service until next time the service is upgraded. If you want to make sure the service won't be re-enabled upon upgrade, you should also type the following:

# update-rc.d apache2 stop 80 0 1 2 3 4 5 6 .

2. Adding A Service

2.1. Default Priorities

Now, if you want to re-add this service to be started on boot up, you can simply use:

# update-rc.d apache2 defaults

Adding system startup for /etc/init.d/apache2 ...

/etc/rc0.d/K20apache2 -> ../init.d/apache2

/etc/rc1.d/K20apache2 -> ../init.d/apache2

/etc/rc6.d/K20apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

2.2. Custom Priorities

But as you can see, the default value is 20 which is pretty different than 91 … a S20 link is started before a S91 and and K91 is kill before K20. To force apache2 to be started with priorities 91 for both Start and Kill, we need to use the following command:

# update-rc.d apache2 defaults 91

Adding system startup for /etc/init.d/apache2 ...

/etc/rc0.d/K91apache2 -> ../init.d/apache2

/etc/rc1.d/K91apache2 -> ../init.d/apache2

/etc/rc6.d/K91apache2 -> ../init.d/apache2

/etc/rc2.d/S91apache2 -> ../init.d/apache2

/etc/rc3.d/S91apache2 -> ../init.d/apache2

/etc/rc4.d/S91apache2 -> ../init.d/apache2

/etc/rc5.d/S91apache2 -> ../init.d/apache2

2.3. Different Priorities For Start And Kill

Alternatively, if you want to set different priorities for Start than for Kill, let say Start with 20 and Kill with 80, you will need to run:

# update-rc.d apache2 defaults 20 80

Adding system startup for /etc/init.d/apache2 ...

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

3. Specifying Custom Runlevels

Finally, if you only want to Start and Kill on specific runlevels, like for instance starting apache with priority 20 on runlevels 2, 3, 4 and 5 and Kill with priority 80 on runlevels 0, 1 and 6:

# update-rc.d apache2 start 20 2 3 4 5 . stop 80 0 1 6 .

Adding system startup for /etc/init.d/apache2 ...

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S20apache2 -> ../init.d/apache2

Or, to start with priority 20 for runlevel 2, 3 and 4 and priority 30 for runlevel 5 and kill with priority 80 for runlevel 0, 1 and 6:

# update-rc.d apache2 start 20 2 3 4 . start 30 5 . stop 80 0 1 6 .

Adding system startup for /etc/init.d/apache2 ...

/etc/rc0.d/K80apache2 -> ../init.d/apache2

/etc/rc1.d/K80apache2 -> ../init.d/apache2

/etc/rc6.d/K80apache2 -> ../init.d/apache2

/etc/rc2.d/S20apache2 -> ../init.d/apache2

/etc/rc3.d/S20apache2 -> ../init.d/apache2

/etc/rc4.d/S20apache2 -> ../init.d/apache2

/etc/rc5.d/S30apache2 -> ../init.d/apache2

我的总结

通过 update-rc.d 来管理Linux下开机自动运行,的确很方便,但是我在实际部署实践中,还是遇到了一些问题,导致开机后没有正常自动启动,但是手工通过service xxx start可以启动。我经过排查发现,原因是在Linux系统启动中,在执行/etc/init.d/中的脚本时,此时系统有可能没有加载好系统中的PATH变量,所以需要在init.d脚本中手工指定,对于使用Ruby脚本写的程序,需要GEM_HOME\GEM_PATH等环境变量,我这里是用RVM来管理Ruby的,这是我使用的:

PATH="/usr/local/rvm/gems/ruby-1.9.2-p290/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:/usr/local/rvm/bin:/opt/node/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"

RUBY_VERSION="ruby-1.9.2-p290"

GEM_HOME="/usr/local/rvm/gems/ruby-1.9.2-p290"

GEM_PATH="/usr/local/rvm/gems/ruby-1.9.2-p290:/usr/local/rvm/gems/ruby-1.9.2-p290@global"

export PATH=$PATH

export RUBY_VERSION=$RUBY_VERSION

export GEM_HOME=$GEM_HOME

export GEM_PATH=$GEM_PATH

另外还有一些其它的变量(如果你的启动程序需要),也需要手工指定,如在Ruby 1.9中,就有可能需要指定,比如说unicorn启动脚本:

export LANG='en_US.UTF-8'

最后附上一个常用的init.d启动脚本的样本:

/etc/init.d/ntgps

#!/bin/sh

### BEGIN INIT INFO

# Provides: gps

# Required-Start: $syslog $remote_fs $network

# Required-Stop: $syslog $remote_fs $network

# Should-Start: fam

# Should-Stop: fam

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: Start the gps.

### END INIT INFO

PATH=/usr/local/rvm/gems/ruby-1.9.2-p290/bin:/usr/local/rvm/gems/ruby-1.9.2-p290@global/bin:/usr/local/rvm/rubies/ruby-1.9.2-p290/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

DAEMON=/www/georgia/current/lib/gps/gps.rb

NAME=ntgps

DESC="ntgps daemon"

PIDFILE=/var/run/$NAME.pid

SCRIPTNAME=/etc/init.d/$NAME

DAEMON_OPTS=""

set -e

. /lib/lsb/init-functions

export LANG='en_US.UTF-8'

export PATH=$PATH

export GEM_HOME=/usr/local/rvm/gems/ruby-1.9.2-p290

export GEM_PATH=/usr/local/rvm/gems/ruby-1.9.2-p290:/usr/local/rvm/gems/ruby-1.9.2-p290@global

case "$1" in

start)

log_daemon_msg "Starting $DESC" $NAME

if ! start-stop-daemon --start -m --background --oknodo --quiet --pidfile $PIDFILE --exec /usr/local/rvm/rubies/ruby-1.9.2-p290/bin/ruby $DAEMON -- $DAEMON_OPTS

then

log_end_msg 1

else

log_end_msg 0

fi

;;

stop)

log_daemon_msg "Stopping $DESC" $NAME

if kill -9 `cat $PIDFILE`

then

rm -f $PIDFILE

log_end_msg 0

else

log_end_msg 1

fi

;;

restart)

$0 stop

$0 start

;;

status)

status_of_proc -p "$PIDFILE" "$DAEMON" $NAME && exit 0 || exit $?

;;

*)

echo "Usage: $SCRIPTNAME {start|stop|restart|status}" >&2

exit 1

;;

esac

exit 0

乌班图Linux程序自动启动,通过update-rc.d来管理Ubuntu系统的自动启动程序相关推荐

  1. 乌班图LINUX如何用SHELL链接,Xshell连接本地虚拟机Ubuntu 18.04的方法

    本地安装了虚拟机,网络选择的是桥接网络,想通过Xshell连接上去,结果无法连接,寻找资料后发现Ubuntu 18.04没有安装ssh,其实Ubuntu系统多数默认情况下并没有安装ssh服务,如果通过 ...

  2. 头歌实践教学平台-Linux网络实战(一)-DNS配置(Ubuntu系统)——保姆级教程

    头歌实践教学平台-Linux网络实战(一)-DNS配置(Ubuntu系统) ***写在前面*** 知识补充 相关知识 实验环境准备 安装DNS服务器并开启服务 一.标题配置域名和IP的正解与反解zon ...

  3. docker安装gamit_科学网—Ubuntu系统GAMIT/GLOBK程序安装 - 陈超的博文

    最近开始学习GAMIT,网上资料还是蛮多的,但是感觉都是东拼西凑的,一点都不系统,一点不适合初学者.安装教程也是乱七八糟的,下面把我的安装过程分享一下:(我也是参考的网上一个教程,很久以前下载的,地址 ...

  4. linux 每日学一点《用tar来备份ubuntu系统》

    用tar来备份ubuntu系统 可能你已经习惯了使用GHOST来备份WINDOWS的操作系统了.GHOST备份WINDOWS的系统是比较麻烦的,你要先重启计算到DOS模式,然后运行GHOST程序,再选 ...

  5. linux 辅助ip地址到文本,技术|如何在 Ubuntu 系统中添加一个辅助 IP 地址

    Linux 管理员应该意识到这一点,因为这是一项例行任务.很多人想知道为什么我们需要在服务器中添加多个 IP 地址,以及为什么我们需要将它添加到单块网卡中?我说的对吗? 你可能也会有类似的问题:在 L ...

  6. Linux火狐解压完运行不了,在Ubuntu系统下firefox账号无法登录的解决

    在Ubuntu 16.04系统下默认自带有firefox浏览器,但是使用这个firefox浏览器会发现账号无法登录,原来是在windows系统下的数据没有办法同步,书签也同步不了.经过查询资料后得知, ...

  7. Ubuntu系统下添加程序启动器

    Ubuntu系统上安装的软件,有的会自动创建快捷方式,在程序中可以搜索到,而有的安装后不会在应用程序中出现,如Eclipse.Spring Tool Suite或是绿色软件等,那么怎么手动创建快捷方式 ...

  8. 数字化棋牌室 | 会员管理预约系统 | 棋牌室小程序

    棋牌室在城市与农村都是部分老年人与年轻人的经常去的娱乐场所,以前这些场所里总是挤满了人,但现在越来越多的棋牌室即使环境装修的漂亮.设备高端完善等依然面对流量难题及管理难题,同时由于棋牌室具有社区属性, ...

  9. linux版本装机大师,如何将电脑上的ubuntu系统重装为win7系统

    有的用户会因为好奇,想要尝试Linux系统到底是怎么样的,所以就在电脑上安装了ubuntu系统.但是安装完之后发现不太合适,就想着重装回win7系统.那究竟怎么将电脑上的ubuntu系统重装为win7 ...

  10. Amlogic Linux系列(一)S912盒子刷Khadas ubuntu系统

    打算学习一下Amlogic Linux系统,目前已知的amlogic Linux系统包括 1.官方的buildroot(没有技术资料,只支持A311D/S905D3) 2.khadas ubuntu ...

最新文章

  1. 上传数据时 ajax请求成功 上传完成,ajaxSubmit请求返回数据成功,但是不执行success回调函数...
  2. XSS之xssprotect
  3. 前端学习(3135):react-hello-react之函数的柯里化
  4. Ajax-基础篇(持续更新01)
  5. VC启动窗口画面制作方法研究
  6. 2017.10.15 旅行comf 失败总结
  7. JQuery Mobile中特有事件和方法
  8. MongoDB,无模式文档型数据库简介
  9. [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据
  10. 基于Java的体育场地预约系统
  11. 【倒计时】用JS写出京东倒计时效果
  12. 聊聊什么是对象存储?
  13. 舌尖上的阳朔,除米粉之外的桂菜诱惑
  14. 【音频分析】短时傅立叶变换结果为啥是对称?每个结果对应的频率是多少?
  15. 小丸子学MongoDB系列之——安装MongoDB
  16. Android Sdk热修复实践之旅
  17. 从输入 url 到页面展示到底发生了什么
  18. CIO访谈实录:英国电讯媒体集团首席信息官亨利·科恩
  19. ubuntu16.04系统下谷歌浏览器不能拖入下载好的扩展程序离线包
  20. Visual Studio 2017 激活密钥

热门文章

  1. 性能测试--33Jvisualvm远程监控Linux服务器方案
  2. GPU数据库PG-strom安装及使用
  3. Web前端开发技术第三版课后练习答案
  4. 实践 | 图片文本爬虫与数据分析
  5. Arduino--DS3231时钟模块
  6. UltraISO 制作U盘启动盘(Ubuntu、CentOS)
  7. 第11章 拾遗3:虚拟局域网(VLAN)
  8. ReactJS快速入门
  9. Windows系统如何修改Hosts文件
  10. 第一章、天天生鲜项目框架搭建