1. 说明:
    ubuntu-16.10 开始不再使用initd管理系统,改用systemd… 其中包括用 systemctl 命令来替换了 service 和 chkconfig 的功能。

"systemd is now used for user sessions. System sessions had already been provided by systemd in previous Ubuntu releases"
systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。
执行ls /lib/systemd/system 可以看到有很多启动脚本,找到我们需要的 rc.local.service

  1. 查看rc.local.service
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
[Unit] 段: 启动顺序与依赖关系
[Service] 段: 启动行为,如何启动,启动类型
[Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动
  1. 修改rc.local.service,新增Install段
    /etc/rc.local 的启动顺序是在网络后面,但是显然它少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。 因此我们就需要在后面帮他加上 [Install] 段:
[Install]  
WantedBy=multi-user.target  
Alias=rc-local.service
  1. 在/etc/systemd/system 目录下创建软链接
ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/
  1. 在/etc目录下创建rc.local文件
touch /etc/rc.local
  1. 添加以下内容
#!/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.

/usr/bin/python /usr/local/bin/ssserver -c /etc/shadowsocks.json -d start  ## ss的开机启动命令

exit 0
  1. 给/etc/rc.local添加执行权限并启用服务
chmod 755 /etc/rc.local               ## 添加执行权限
systemctl enable rc-local             ## 启用服务
systemctl start rc-local.service      ## 启动服务
systemctl status rc-local.service     ## 查看状态

这样就可以在/etc/rc.local 添加开机启动任务了