If you use Linux you most likely have heard of the init.d directory. But what exactly does this directory do? It ultimately does one thing but it does that one thing for your entire system, so init.d is very important. The init.d directory contains a number of start/stop scripts for various services on your system. Everything from acpid to x11-common is controlled from this directory. Of course it's not exactly that simple.

If you look at the /etc directory you will find directories that are in the form rc#.d (Where # is a number reflects a specific initialization level - from 0 to 6). Within each of these directories is a number of other scripts that control processes. These scripts will either begin with a "K" or an "S". All "K" scripts are run before "S" scripts. And depending upon where the scripts are located will determine when the scripts initiate. Between the directories the system services work together like a well-oiled machine. But there are times when you need to start or stop a process cleanly and without using the kill or killall commands. That is where the /etc/init.d directory comes in handy.

Now if you are using a distribution like Fedora you might find this directory in /etc/rc.d/init.d. Regardless of location, it serves the same purpose.

In order to control any of the scripts in init.d manually you have to have root (or sudo) access. Each script will be run as a command and the structure of the command will look like:

/etc/init.d/command OPTION

Where command is the actual command to run and OPTION can be one of the following:

  • start
  • stop
  • reload
  • restart
  • force-reload

Most often you will use either start, stop, or restart. So if you want to stop your network you can issue the command:

/etc/init.d/networking stop

Or if you make a change to your network and need to restart it, you could do so with the following command:

/etc/init.d/networking restart

Some of the more common init scripts in this directory are:

  • networking
  • samba
  • apache2
  • ftpd
  • sshd
  • dovecot
  • mysql

Of course there may be more often-used scripts in your directory - it depends upon what you have installed. The above list was taken from a Ubuntu Server 8.10 installation so a standard desktop installation would have a few less networking-type scripts.

But what about /etc/rc.local

There is a third option that I used to use quite a bit. This option is the /etc/rc.local script. This file runs after all other init level scripts have run, so it's safe to put various commands that you want to have issued upon startup. Many times I will place mounting instructions for things like nfs in this script. This is also a good place to place "troubleshooting" scripts in. For instance, once I had a machine that, for some reason, samba seemed to not want to start. Even afer checking to make sure the Samba daemon was setup to initialize at boot up. So instead of spending all of my time up front with this I simply placed the line:

/etc/init.d/samba start

in the /etc/rc.local script and Samba worked like a charm. Eventually I would come back and trouble shoot this issue.

Final Thoughts

Linux is flexible. Linux is so flexible there is almost, inevitably, numerous ways to solve a single problem. Starting a system service is one such issue. With the help of the /etc/init.d system (as well as /etc/rc.local) you can pretty much rest assured your service will start.

Advertisement

 
 

ABOUT JACK WALLEN

Jack has been a technical writer, covering Linux and open source, for nearly ten years. He began as an editor in chief of Linux content with Techrepublic and is now a freelance writer for numerous sites. Jack is also a writer of novels and is currently working on his first zombie fiction!
 
 
 
 
 

/etc/init.d

  如果你使用过linux系统,那么你一定听过init.d目录。但是这个目录究竟是做什么用的?他基本上只做一件事,但是这件事是为你的整个系统服务的,所以init.d目录非常重要。这个目录里面包含了一系列系统里面服务的开启和停止的脚本。从acpid到x11-common的各项事务都受它控制。所以init.d没有想象的那么简单。
  当你查看/etc目录时,你会发现许多rc#.d形式的目录。(#代表数字0-6,指相对的初始化水平)这些目录中包含许多进程的控制脚本。这些脚本以’K’或者’S’开头。所有以’K’开头的脚本先于以’S’开头的脚本运行。这些脚本所在位置将决定这些脚本启动的时间。这些系统系统服务像一个高效率的机器一样在这些目录间共同工作。有时候你想在不使用kill或者killall进程的情况下,干净的启动或者停止一个脚本的时候,、inin.d这个目录就派上用场了。
  如果你是用的是Fedore系统,你将可以找到/etc/rc.d/init.d目录。无论init.d在什么位置,它都将发挥同样的作用。
  为了能够使用init.d目录下的脚本,你需要有root或者sudo权限。所有的这些命令将被当做一个命令运行,这些命令的结构大致如下:
  /etc/init.d/command OPTION
  command指的是你实际运行的命令。OPTION可以是下面几种:

  • start
  • stop
  • reload
  • resart
  • force-reload

大多数情况下你使用的是start,stop,restart。例如:你想关掉你的网路连接,你可以使用如下的命令:
/etc/init.d/networking restart
或者当你修改了你的网络配置,需要重启网络的时候。你可以使用下面的命令:
/etc/init.d/networking restart

这个目录下常用的脚本有:
- samba
- apache2
- ftpd
- sshd
- dovecot
- mysql
- networking
当然你的目录下可能含有其他的一些常用脚本。这个取决于你安装了什么服务。

/etc/rc.local

  /etc/rc.local是我经常是用的另一个脚本。这个脚本在系统初始化脚本之后运行,所以你可以放心的将你想在系统启动后执行的脚本放在里面。通常我会将nfs的挂载脚本放在里面。同时也是一个放调试脚本的好地方。比如,有一次我的系统中得samba服务无法正常启动,尽管检查确认本该随着系统一起启动的。通常我也会话大量的时间去寻找原因,仅仅是在rc.local文件里下写下这么一行
  /etc/init.d/samba start
  samba无法启动的问题就解决了


总结

  Linux系统是灵活的,正因为这种灵活性,同一个问题总可以找到多种解决方案。启动系统服务就是个很好的例子。 在/etc/init.d和/etc/rc.local的帮助下,你可以确保你的系统服务可以启动和运行。

https://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/

Get To Know Linux: The /etc/init.d Directory的更多相关文章

  1. Linux系统/etc/init.d目录

    理解Linux系统/etc/init.d目录和/etc/rc.local脚本 http://blog.csdn.net/acs713/article/details/7322082 Linux文件目录 ...

  2. 理解Linux系统/etc/init.d目录和/etc/rc.local脚本

       一.关于/etc/init.d 如果你使用过Linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重 ...

  3. linux中/etc/init.d [转]

    一.关于/etc/init.d 如果你使用过linux系统,那么你一定听说过init.d目录.这个目录到底是干嘛的呢?它归根结底只做了一件事情,但这件事情非同小可,是为整个系统做的,因此它非常重要.i ...

  4. linux系统/sbin/init执行过程

    对于Linux的启动过程,之前一直都是研究到内核运行/sbin/init,启动第一个用户进程为止,因为这部分一直都是在内核态工作,所以对于学习内核还是有帮助的,当时/sbin/init之后的过程也需要 ...

  5. Linux reboot与init 6区别

    Reboot与init 6的区别 - flyingcloud_2008的专栏 - CSDN博客https://blog.csdn.net/flyingcloud_2008/article/detail ...

  6. linux 下的init 0,1,2,3,4,5,6知识介绍

    一. init是Linux系统操作中不可缺少的程序之一. 所谓的init进程,它是一个由内核启动的用户级进程. 内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后, ...

  7. Linux中/etc/init.d

    原文链接:http://blog.163.com/laorenyuhai126@126/blog/static/193507792010525110516/   在这个目录下的档案都是连结档,均指向到 ...

  8. 为什么linux系统中init被systemd替换了

    Linux init 进程是系统初始化进程,比较古老了,所以出现了一些新的替代方案: Upstart – A init replacement daemon implemented in Ubuntu ...

  9. 基础(二):Linux系统/etc/init.d目录和/etc/rc.local脚本

    原文来自http://www.ghacks.net/2009/04/04/get-to-know-linux-the-etcinitd-directory/ 译文来自http://blog.csdn. ...

随机推荐

  1. war包的解压与打包

    转: war包的解压与打包 2018年03月22日 14:59:56 Jitwxs 阅读数:21421   版权声明:本文版权归Jitwxs所有,欢迎转载,但未经作者同意必须保留原文链接. https ...

  2. jq 实时监听input输入框的变化

    项目需求中有时候需要实时监测 input 的值变化,虽然 input 自身有 focus 和 blur 事件,但是有时候跟需求不符合. 所以实时监听 input 值变化的代码为: $("#i ...

  3. (转载)IDEA中对Git的常规操作(合并,提交,新建分支,更新)

    工作中多人使用版本控制软件协作开发,常见的应用场景归纳如下: 假设小组中有两个人,组长小张,组员小袁 场景一:小张创建项目并提交到远程Git仓库 场景二:小袁从远程Git仓库上获取项目源码 场景三:小 ...

  4. Redis 分库

    Redis 可以分库,相当于 MySQL 中的 database.控制数据库总数在 redis配置文件中设置,默认是 16 个.数据库名称是整数索引标识,而不是由一个数据库名称字符串. 选择数据库用 ...

  5. configmap使用-查看configmap个数

    [root@kube-node1 gitlab]# kubectl get cmNo resources found.

  6. 2019年icpc区域赛银川站总结

    目录 一.前言 二.10月19日热身赛 三.10月20日正式赛 四.结果 一.前言 比赛前我们队有ccpc厦门和icpc银川的名额,然而这两个地区的时间正好撞了,考虑到银川更容易拿奖,加上我们ACM协 ...

  7. 【VUE】vue在vue-cli3环境下基于axios解决跨域问题

    网上的绝大部分教程解决vue+axios跨域问题都不能直接适用vue-cli3.这是因为vue-cli3不一样的配置方式导致的. 如果是使用vue-cli3构建的项目,那么默认是没有config.js ...

  8. java按某属性分组并计算相关属性的和。

    工作中在处理集合的时候会经常遇到需要分组然后计算某属性的和,在java8中,通过stream来操作集合,还是非常方便的,像过滤(filter).分组(group).获取单个属性的值,总而言之,简单方便 ...

  9. VS2008新增文件没有模板

    可能是我安装的过程中发神经没有选中选项什么的,打开来想建个项目发现一个模板都没有,那就很尴尬了,作为对开发工具极度依赖的人,这真的难受... 在网上找到别人的办法 开始 –> 程序 –> ...

  10. 小记----------lombok插件idea的安装&常见注解解释及小案例

    Lombok安装插件 软件:idea 2018.3.6版本 1.打开settings