linux开机启动项设置

添加自定义启动脚本

在使用linux时常常需要自己写一些脚本程序使之开机启动,这时就要借助/etc/rc.local这个文件来帮助我们完成这个任务

关于 rc.local

rc.local快要被废弃了

下面是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.

简单来讲rc.local中将会放置使用者自定义的开机启动程序,在linux启动的最后阶段,系统将会执行其中的在exit 0之前的命令

添加启动项

知道rc.local的作用后,添加开机启动项将会变得轻而易举

下面是例子:

开机执行命令

希望开机的时候启动shadowsocks,只需要在exit 0前面添加

1
sslocal -c ss.conf

开机执行脚本

希望开机执行’/home/plusls/ss.sh’,只需在exit 0前添加(需保证该脚本有可执行权限)

1
/home/plusls/ss.sh

设置服务开机启动

这里的服务指 sysV 编写的启动脚本

关于服务

与windows一样,linux同样存在服务的概念,控制服务运行的脚本,存放于/etc/init.d

查询服务状态

通过service servicename status查看服务状态

例:

1
2
3
4
5
plusls@plusls-chromebook:~$ sudo service l2tpd  status
● xl2tpd.service - LSB: layer 2 tunelling protocol daemon
Loaded: loaded (/etc/init.d/xl2tpd; generated; vendor preset: enabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)

设置服务状态(对于debian系的系统)

可以通过service servicename status来设置服务状态,status一般包括start,restart,stop(设置的状态仅对当前有效)

例:

1
service sshd stop

禁用服务开机启动

例:

1
update-rc.d -f apache2 remove

参数-f是强制删除符号链接

增加服务开机启动

例:

1
update-rc.d apache2 defaults