在Ubuntu系统中添加自定义服务需要遵从设计启动脚本的模式,下面就是如何编写启动脚本的示例程序。
1、在/etc/init.d/ 下以管理员权限新建文件,在本例中为location_server.
2、使用以下模板修改启动脚本的内容
#!/bin/bash
### BEGIN INIT INFO
#
# Provides: location_server
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: initscript
# Description: This file should be used to construct scripts to be placed in /etc/init.d.
#
### END INIT INFO
## Fill in name of program here.
PROG="location_server"
PROG_PATH="/opt/location_server" ## Not need, but sometimes helpful (if $PROG resides in /opt for example).
PROG_ARGS=""
PID_PATH="/var/run/"
start() {
if [ -e "$PID_PATH/$PROG.pid" ]; then
## Program is running, exit with error.
echo "Error! $PROG is currently running!" 1>&2
exit 1
else
## Change from /dev/null to something like /var/log/$PROG if you want to save output.
$PROG_PATH/$PROG $PROG_ARGS 2>&1 >/var/log/$PROG &
$pid=`ps ax | grep -i 'location_server' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
echo "$PROG started"
echo $pid > "$PID_PATH/$PROG.pid"
fi
}
stop() {
echo "begin stop"
if [ -e "$PID_PATH/$PROG.pid" ]; then
## Program is running, so stop it
pid=`ps ax | grep -i 'location_server' | sed 's/^\([0-9]\{1,\}\).*/\1/g' | head -n 1`
kill $pid
rm -f "$PID_PATH/$PROG.pid"
echo "$PROG stopped"
else
## Program is not running, exit with error.
echo "Error! $PROG not started!" 1>&2
exit 1
fi
}
## Check to see if we are running as root first.
## Found at http://www.cyberciti.biz/tips/shell-root-user-check-script.html
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
echo "Usage: $0 {start|stop|reload}" 1>&2
exit 1
;;
esac
其中,PROG变量为所要运行的可执行程序的名称, PROG_PATH为可执行文件所在的目录,PROG_ARGS为执行程序的各个参数。
需要注意的是,在stop()函数中利用kill命令结束进程,有两种方法可以处理,一种是利用进程名称,如“location_server”查找相应的进程号,然后调用kill <进程号>结束进程,另一种方法是直接使用killall <进程名称>,但是在这种方法下,本启动脚本的名称不能和可执行文件的名称相同,不然的话,stop后会出现”Terminated“说明脚本也被kill掉。也可以在start()中将进程号存储在.pid文件中,然后在stop()中从文件中取得要结束的进程号,但是这样的话,还想获得的进程号会比实际进程号小2,现在还不知道是什么原因。
3、添加删除服务
添加: sudo update-rc.d 服务名 defaults
删除:sudo update-rc.d -f 服务名 remove
1
2
4、启动、关闭、重启服务
/etc/init.d/服务名 start
/etc/init.d/服务名 stop
/etc/init.d/服务名 start
原文链接:https://blog.csdn.net/xkjcf/article/details/78698232
ubuntu开机自启动思路
ubuntu操作系统有很多运行级别,默认情况下runlevel为2,进入runlevel2时,会按照优先级执行/etc/rc2.d/目录下的所有可执行文件。操作系统的运行级别可以用过命令runlevel查看。如下:
li@li:~$ runlevel
N 2
开机自启动的具体解决方法为:
1. 创建固定格式的的启动脚本并修改权限;
your_bash.sh格式如下,其中,setsid ./your_bin &中的setsid 和& 符号是打开一个窗口让程序后台运行。
#!/bin/sh
### BEGIN INIT INFO
# Provides: HMI.sh
# Required-start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the HMI.sh daemon
# Description: starts HMI.sh using start-stop-daemon
### END INIT INFO
#this is the command that you write to start your app
cd /home/your_file/
setsid ./your_bin &
exit 0
修改your_bash.sh的权限:
chmod a+x your_bash.sh
2. 将脚本移动到/etc/init.d/目录下;
sudo cp your_bash /etc/init.d/
3. 运行runlevel查看系统运行级别,默认情况下为2;
$ runlevel
N 2
4. 进入对应的 /etc/rcx.d/文件夹
$ cd /etc/rc2.d/
5. 创建软连接,使进入这一runlevel时,自动运行脚本。
sudo ln -vsf /etc/init.d/your_bash S90your_binStart
6. 查看是否创建成功.
若有返回则正常,可以sudo reboot,查看启动系统时程序是否自动运行。
ls -alh | grep S90your_binStart
软连接的命令有特定的要求,开头必须时’S’,后接2位数字,其值为5-99,表示程序的启动优先级,99为最后启动;最后是命令的字符串,可任意。
8. 重启后,使用以下命令查看可执行文件是否正在运行。若有打印出进程信息,则OK 。
ps -aux | grep your_bin | grep -v grep
原文链接:https://blog.csdn.net/u013894391/article/details/89405738
1. 开机启动时自动运行程序
Linux加载后, 它将初始化硬件和设备驱动, 然后运行第一个进程init。init根据配置
文件继续引导过程,启动其它进程。通常情况下,修改放置在
/etc/rc或
/etc/rc.d 或
/etc/rc?.d
目录下的脚本文件,可以使init自动启动其它程序。例如:编辑
/etc/rc.d/rc.local 文件(该文件通常是系统最后启动的脚本),
在文件最末加上一行“xinit”或“startx”,可以在开机启动后直接进入X-Window。
(1) 用户登录时,bash先自动执行系统管理员建立的全局登录script :
/ect/profile
然后bash在用户起始目录下按顺序查找三个特殊文件中的一个:
/.bash_profile、
/.bash_login、
/.profile,
但只执行最先找到的一个。因此,只需根据实际需要在上述文件中加入命令就可以实
现用户登录时自动运行某些程序(类似于DOS下的Autoexec.bat)。
(2)14.04 and later
参考:How do I start applications automatically on login?
在Ubuntu14.04版本之后提供了更方便的autostart文件夹,可以在这个文件夹下设置启动项,设置方法有三种,如下:
方法一:GUI方法
Open the Dash and search for "Startup Applications"
Now click on Add and give in the command to run the application. This can be found in Main Menu if installed (see below) or as shown in this question.
方法二:使用其他程序:Using Main Menu (alacarte )
Firstly open the program 'Main Menu' (type Menu
in the Dash)
Now select the program which you want to add to startup and click on properties .
Now note the command for that program .
方法三:Non GUI approach
Advanced users may want to put a .desktop file in ~/.config/autostart
to run applications after a user login. This may have following content:
[Desktop Entry]
Type=Application
Name=
Exec=
Icon=
Comment=
X-GNOME-Autostart-enabled=true
3. 退出登录时自动运行程序
退出登录时,bash自动执行个人的退出登录脚本
/.bash_logout。
例如,在/.bash_logout中加入命令“tar -cvzf c.source.tgz *.c”,则在每次退出
登录时自动执行 “tar” 命令备份 *.c 文件。
4. 定期自动运行程序
Linux有一个称为crond的守护程序,主要功能是周期性地检查 /var/spool/cron目录
下的一组命令文件的内容,并在设定的时间执行这些文件中的命令。用户可以通过
crontab 命令来建立、修改、删除这些命令文件。
例如,建立文件crondFile,内容为“00 9 23 Jan * HappyBirthday”,运行“crontab
cronFile”命令后,每当元月23日上午9:00系统自动执行“HappyBirthday”的程序(“*
”表示不管当天是星期几)。
5. 定时自动运行程序一次
定时执行命令at 与crond 类似(但它只执行一次):命令在给定的时间执行,但不自
动重复。at命令的一般格式为:at [ -f file ] time ,在指定的时间执行file文件
中所给出的所有命令。也可直接从键盘输入命令:
$ at 12:00
at>mailto Roger -s ″Have a lunch″ < plan.txt
at>Ctr-D
Job 1 at 2000-11-09 12:00
2000-11-09 12:00时候自动发一标题为“Have a lunch”,内容为plan.txt文件内容
的邮件给Roger.
ubuntu 自添加开机启动程序
ubuntu (我的是 9.10)的开机启动会和 redhat suse 这些发行版会稍有差别,比如默认情况下没有 /etc/inittab 的配置文件,redhat 发行版在启动级别 3 上是文本模式登录,而 ubuntu 的启动级别2~5 都是一样的启动。现在,添加一个自定义的可执行文件或脚本,使其在开机启动时执行。
以一个脚本为例,脚本的内容很简单 :
引用
#! /bin/sh
echo "hello start up script!" > /home/beyes/mystart.txt
exit 0
这个脚本的作用只是在我的家目录里建立一个文本文件,里面的内容就是 echo 后的内容hello start up script!。
编辑好这个脚本后,给其赋予相应的可执行文件,为了方便,就 chmod 777 /etc/init.d/mystart
接着在 /etc/rc5.d 这个目录下做一个软链接: ln -s /etc/init.d/mystart /etc/rc5.d/S99mystart
那么,这个脚本开机启动生效了么?经过重启后,并没有发现在 /home/beyes 目录下生成 mystart.txt 文件。
使用 sysv-rc-conf 配置一下启动服务:
上图,在第 2 运行级别也配置了让 mystart 启动。这个 sysv-rc-conf 会读取 /etc/init.d 里的文件以及 rcx.d (x为运行级别)下的软连接等信息。关于更多管理启动项的更多信息见:
http://www.groad.net/bbs/read.php?tid-1392.html
配置完后,重启。再到 /home/beyes 里查看,生成了 mystart.txt 文件,里面也有相应的内容。从这里,也看到了 ubuntu 默认的启动运行级别为 2 。另外,在 /etc/rc2.d 目录下,也发现了由 sysv-rc-conf 生成的软连接: S99mystart
ubuntu开机自动运行程序
1.编写shell脚本
gedit /etc/init.d/aa
#!/bin/bash
mplayer /home/aa.avi -fs -vo fbdev -vf scale=800:600 (fs全屏,vo进入桌面前使用, scale设置画面大小)
2. chmod 755 /etc/init.d/aa
3. ubuntu默认启动级别为2加载的脚本在/etc/rc2.d/
ln -s /etc/init.d/aa /etc/rc2.d/s99aa (s为开始执行99为执行顺序aa为文件名)
本贴来自天极网群乐社区--http://q.yesky.com/group/review-17826808.html
linux启动过程综述
http://www.ibm.com/developerworks/cn/linux/kernel/startup/index.html
Upstart与ubuntu启动过程,简单原理
http://www.linuxdiyf.com/viewarticle.php?id=102927
Upstart: Ubuntu 的基于事件的启动进程
http://www2.oklinux.cn/html/Basic/azpz/20080504/52808.htm
ref link: https://www.cnblogs.com/mo-wang/p/5153042.html
在Ubuntu中,/etc/init.d已被/ usr / lib / systemd所取代。脚本仍然可以通过“服务”启动和停用。但主要命令现在是'systemctl'。chkconfig命令被留下,现在你用systemctl来做这件事。
所以,而不是:
chkconfig enable apache2
您应该查找服务名称,然后启用它
systemctl status apache2
systemctl enable apache2.service
如果你有一个systemd脚本或一个/etc/init.d脚本,并且做正确的事,Systemd已经变得更加友好。
参考地址2: Ubuntu 16.04 增加bash脚本为service,开机自启服务脚本配置
常用命令
重新加载service文件: systemctl daemon-reload
启动一个服务: systemctl start nginx-1.13.0.service
关闭一个服务: systemctl stop nginx-1.13.0.service
重启一个服务: systemctl restart nginx-1.13.0.service
显示一个服务的状态: systemctl status nginx-1.13.0.service
在开机时启用一个服务: systemctl enable nginx-1.13.0.service
在开机时禁用一个服务: systemctl disable nginx-1.13.0.service
查看服务是否开机启动: systemctl is-enabled nginx-1.13.0.service
查看已启动的服务列表: systemctl list-unit-files|grep enabled
查看启动失败的服务列表:systemctl --failed
apache
启动 systemctl start httpd
停止 systemctl stop httpd
重启 systemctl restart httpd
mysql
启动 systemctl start mysqld
停止 systemctl stop mysqld
重启 systemctl restart mysqld
php-fpm
启动 systemctl start php-fpm
停止 systemctl stop php-fpm
重启 systemctl restart php-fpm
nginx
启动 systemctl start nginx
停止 systemctl stop nginx
重启 systemctl restart nginx
reflink: https://blog.csdn.net/dafei4/article/details/79741384
系统启动时需要加载的配置文件
/etc/profile、/root/.bash_profile
/etc/bashrc、/root/.bashrc
/etc/profile.d/*.sh、/etc/profile.d/lang.sh
/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)
一、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)
# 1.编辑rc.local文件 [root@localhost ~]# vi /etc/rc.local # 2.修改rc.local文件,在 exit 0 前面加入以下命令。保存并退出。 /etc/init.d/mysqld start # mysql开机启动 /etc/init.d/nginx start # nginx开机启动 supervisord -c /etc/supervisor/supervisord.conf # supervisord开机启动 /bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null # 3.最后修改rc.local文件的执行权限 [root@localhost ~]# chmod +x /etc/rc.local [root@localhost ~]# chmod 755 /etc/rc.local
二、自己写一个shell脚本
将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。
三、通过chkconfig命令设置
# 1.将(脚本)启动文件移动到 /etc/init.d/或者/etc/rc.d/init.d/目录下。(前者是后者的软连接) mv /www/wwwroot/test.sh /etc/rc.d/init.d # 2.启动文件前面务必添加如下三行代码,否侧会提示chkconfig不支持。 #!/bin/sh 告诉系统使用的shell,所以的shell脚本都是这样 #chkconfig: 35 20 80 分别代表运行级别,启动优先权,关闭优先权,此行代码必须 #description: http server 自己随便发挥!!!,此行代码必须 /bin/echo $(/bin/date +%F_%T) >> /tmp/test.log # 3.增加脚本的可执行权限 chmod +x /etc/rc.d/init.d/test.sh # 4.添加脚本到开机自动启动项目中。添加到chkconfig,开机自启动。 [root@localhost ~]# cd /etc/rc.d/init.d [root@localhost ~]# chkconfig --add test.sh [root@localhost ~]# chkconfig test.sh on # 5.关闭开机启动 [root@localhost ~]# chkconfig test.sh off # 6.从chkconfig管理中删除test.sh [root@localhost ~]# chkconfig --del test.sh # 7.查看chkconfig管理 [root@localhost ~]# chkconfig --list test.sh
四、自定义服务文件,添加到系统服务,通过Systemctl管理
1.写服务文件:如nginx.service、redis.service、supervisord.service
[Unit]:服务的说明 Description:描述服务 After:描述服务类别 [Service]服务运行参数的设置 Type=forking 是后台运行的形式 ExecStart 为服务的具体运行命令 ExecReload 为服务的重启命令 ExecStop 为服务的停止命令 PrivateTmp=True 表示给服务分配独立的临时空间 注意:启动、重启、停止命令全部要求使用绝对路径 [Install] 服务安装的相关设置,可设置为多用户 WantedBy=multi-user.target
2.文件保存在目录下:以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service
3.设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload
设置开机自启动 [root@localhost ~]# systemctl enable nginx.service [root@localhost ~]# systemctl enable supervisord 停止开机自启动 [root@localhost ~]# systemctl disable nginx.service [root@localhost ~]# systemctl disable supervisord 验证一下是否为开机启动 [root@localhost ~]# systemctl is-enabled nginx [root@localhost ~]# systemctl is-enabled supervisord
4.其他命令
启动nginx服务 [root@localhost ~]# systemctl start nginx.service 停止nginx服务 [root@localhost ~]# systemctl start nginx.service 重启nginx服务 [root@localhost ~]# systemctl restart nginx.service 查看nginx服务当前状态 [root@localhost ~]# systemctl status nginx.service 查看所有已启动的服务 [root@localhost ~]# systemctl list-units --type=service
5.服务文件示例:
# supervisord.service进程管理服务文件 [Unit] Description=Process Monitoring and Control Daemon # 内容自己定义:Description=Supervisor daemon After=rc-local.service nss-user-lookup.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ExecStop= /usr/bin/supervisorctl shutdown ExecReload=/usr/bin/supervisorctl reload Restart=on-failure RestartSec=42s KillMode=process [Install] WantedBy=multi-user.target
# nginx.service服务文件 [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target
# redis.service服务文件 [Unit] Description=Redis After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/bin/redis-server /etc/redis.conf ExecStop=kill -INT `cat /tmp/redis.pid` User=www Group=www [Install] WantedBy=multi-user.target
https://www.cnblogs.com/zwcry/p/9602756.html
https://www.jianshu.com/p/e1442913eb0e
reflink: https://www.cnblogs.com/liuhaidon/archive/2019/09/19/11549997.html
运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登陆
运行级别2:多用户状态(没有NFS)
运行级别3:完全的多用户状态(有NFS),登陆后进入控制台命令行模式
运行级别4:系统未使用,保留
运行级别5:X11控制台,登陆后进入图形GUI模式
运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动
运行级别的原理:
1。在目录/etc/rc.d/init.d下有许多服务器脚本程序,一般称为服务(service)
2。在/etc/rc.d下有7个名为rcN.d的目录,对应系统的7个运行级别
3。rcN.d目录下都是一些符号链接文件,这些链接文件都指向init.d目录下的service脚本文件,命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字。
4。系统会根据指定的运行级别进入对应的rcN.d目录,并按照文件名顺序检索目录下的链接文件
对于以K开头的文件,系统将终止对应的服务
对于以S开头的文件,系统将启动对应的服务
5。查看运行级别用:runlevel
6。进入其它运行级别用:init N
7。另外init0为关机,init 6为重启系统
由于现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit 来起动图形界面。建议在系统安装完成后把系统的默认运行等级设置在第3级,在字符终端登录后,再手工输入startx 命令起动图形界面。可以用如下的方法修改:
用文本编辑器修改 /etc/inittab文件,把
代码:
id:5:initdefault:这一行,修改成
代码:
id:3:initdefault:保存后就reboot重起,系统就默认起动到字符界面。不同运行级别之间的 差别的在于系统默认起动的服务的不同,如运行级别3默认不启动X图形界面服务,而运行级别5 却默认起动。本质上是没有区别的,更无所谓不同级别间功能强弱的问题。用户完全可自给定义不同级别的默认服务。在任何运行级别,用户都可用init 命令来切换到其他运行级别。
由于ubuntu系统没有inittab文件。其配置参考:http://blog.chinaunix.net/u2/82530/showart_2033617.html
具体我也没试过。
文章部分来源:http://space.itpub.net/370491/viewspace-216954
关于Ubuntu运行级别、开机启动脚本的说明
目录简介
1.1介绍Ubuntu下面的自启动脚本目录
1.2 Linux操作系统运行级别的概念
1.3关于操作系统自启脚本的启动顺序
1.4 Linux操作系统运行级别的概念
1.5 自启和关闭自启服务
1.6 对于init [number]命令
Content:
1.1 介绍Ubuntu操作系统关于自启动脚本的几个目录
其中rc0.d~rc.6.d文件夹下分别对应的是操作系统0-6级运行的状态下需要执行的脚本。此外还有rcS.d文件夹和rc.local文件,而在这些文件夹下的文件,都是软链文件,指向指定位置的脚本,有图为证:
经查看,发现这些软链都是指向同一个文件夹../init.d/的脚本文件,
我们查看其它的文件夹,发现情况都是这样子。因此所有开机自启动的脚本文件都是放在/etc/init.d/目录下面的:
那么这些rcN.d分别代表什么意义?或者是说是不是按照0~6的顺序依次运行?肯定不是。因为我们查看rc0.d folder
发现,在该文件夹下,居然有halt(关机)脚本,因此开启操作系统的时候,肯定不可能执行这个。因此证明rcN.d这几个folders之间是相互独立的,没有什么本质的联系。
那么为什么会有这几个文件夹的区别呢?这就引入了linux操作系统的运行级别(run-level)概念。
1.2 Linux操作系统运行级别的概念
根据前面学习的内容,我们知道有rc0~6在加 rcS.d & rc.local,后面这两个我们先不看,至少存在0-6共七种运行级别。这里的运行级别的概念被称为System V的运行机制。这七种运行级别的含义如下(仅仅针对Ubuntu,其他系统定义可能不完全一样):
我们可以通过runlevel查看当前系统的运行状态
也就是说我们系统的运行级别目前是2,该参数的配置文件/etc/init/rc-sysinit.conf
因此我们开机自启动的脚本在rc2.d文件夹下,所有的软链指向/etc/init.d/中的脚本文件:
这些就是我们默认开机状态下,要启动的服务,如果我们将自己的脚本放在/etc/init.d/目录下,同时在rcN.d中创建软链,指向该文件(软链的名称应该是S
1.3 关于操作系统自启脚本的启动顺序
假设我们的操作系统运行级别是2,那么在/etc/rc2.d/文件夹下的脚本执行顺序是怎样的?因为有些服务需要先启动,有些服务需要后启动,他们之间存在先后关系,即有向图的关系。
我们查看该文件夹下的软链名称(见上图),软链的名称有一定的规则:
S[number][service name] --> ../init.d/servicename
S 表示 Start,开启服务
[number]表示的是该脚本的运行优先级,number越小,脚本的运行优先级就越高
[service name]表示的是服务的名称。
因此我们如果需要定义一个服务启动的优先级(顺序),则需指定脚本的number即可。
根据此规则,我们查看rc6.c文件夹(重启运行级别):
按照number的顺序,显示Kill服务,之后再Start一些服务,最后重新启动S90reboot.
1.4 关于rcS.d rc.local文件夹的意义
该脚本是在rc0~6.d文件夹下的脚本执行之前执行rcS.d文件夹下的脚本命令。
1.5 自启和关闭自启服务
在理解了自启服务的原理之后,我们可以手动添加脚本链接文件到指定的rcN.d目录下,但是这种纯手工的方式比较复杂,因此我们可以使用简单的命令来实现这种功能,或者是指定的工具来实现。
命令版本# update-rc.d来自动实现System V 的RunLevel的启动脚本服务,前提是这些服务的启动脚本位于/etc/init.d/目录下。
update-rc.d
eg:
sudo update-rc.d rinetd start 20 2
sudo update-rc.d rinetd stop 20 0
update-rc.d
eg:
sudo update-rc.d rinetd disable 2 在runlevel2中暂时禁止该服务
update-rc.d
eg:
sudo update-rc.d rinetd default 80 80 # default 表示在2 3 4 5 中添加80(the first 80)顺序的Start,在0 6 中添加80(the second 80)顺序的Kill服务
注意实现去对应的文件夹查看该服务的ordernumber
从启动里面删除
sudo update-rc.d -f
这样在所有的运行级别中就会删除掉关于该service的自启和关闭服务链接(删除的仅仅是链接,而不是/etc/init.d/文件夹中的脚本文件)
1.6 对于init [number]命令
我们在命令行中关闭操作系统或者是重新启动,使用的是
在本质上调用的是:
rc0.d 表示的关机 rc6.d表示的是重新启动
因此我们还可以在不重新启动操作系统的前提下,切换操作系统的RunLevel
sudo init
reflink: https://www.cnblogs.com/hbhzsysutengfei/p/ubuntu-runlevel-autostart-service.html
Ubuntu的默认开机的runlevel是2,可以用runlevel来查看当前的默认运行级别。
debian系(ubuntu是基于debian)的Linux一直是用runlevel 2来默认启动,并且runlevel定义也与redhat有区别。
debian的runlevel级别定义如下:
0 - Halt
1 - Single
2 - Full multi-user with display manager (GUI)
3 - Full multi-user with display manager (GUI)
4 - Full multi-user with display manager (GUI)
5 - Full multi-user with display manager (GUI)
6 - Reboot
可以发现2~5级是没有任何区别的。
######而redhat的runlevel级别定义如下:
######0 - Halt
######1 - Single
######2 - Not used/User definable
######3 - Full multi-user NO display manager
######4 - Not used/User definable
######5 - Full multi-user with display manager (GUI)
######6 - Reboot
对应的配置文件夹如下:
/etc/rc0.d Run level 0
/etc/rc1.d Run level 1
/etc/rc2.d Run level 2
/etc/rc3.d Run level 3
/etc/rc4.d Run level 4
/etc/rc5.d Run level 5
/etc/rc6.d Run level 6
这里的runlevel3-5都是进入图形界面。这与其他的linux发行版不太一致,通常runlevel 3是Multi user mode,即直接登录到字符界面;而runlevel 5是Multi user mode with GUI,即登录到图形界面。
现在把runlevel3改为Multi user mode需要如下步骤:
一、删除gdm的启动项
找到/etc/rc3.d中的gdm项,把它删除,如果想恢复,可以重新建立一个到/etc/init.d中的gdm链接。
二、修改默认的runlevel
如果暂时修改,可以使用telinit N,这里的N可以是runlevel的代码[0-6]。如果想长期修改,可以在/etc/下建立inittab文件(这个文件在其他发行版中是存在的,而在Debian及其衍生版中是没有的)。文件内容如下
id:3:initdefault:
注意最后的冒号,这样就说明启动时默认为runlevel 3。
sudo reboot试试看是不是到了文本界面下。
参考文献:
http://www.linuxdiyf.com/viewarticle.php?id=92001
http://www.debianadmin.com/debian-and-ubuntu-linux-run-levels.html
reflink: https://www.cnblogs.com/growup/archive/2011/07/13/2105389.html