方法1:通过systemd服务 (推荐)

1、添加rc-local.service

cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target
EOF

2、新建rc-local文件

cat > /etc/rc.local <<EOF
#!/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.
 
# bash /root/bindip.sh
 
exit 0
EOF

3、添加权限并设置开机自启

chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service

4、编辑脚本,加入需要开机自启的命令

vim /etc/rc.local

方法2:通过创建rc.d脚本(以rclone为例) 部分系统不生效(不推荐)

  • 创建文件 vi etc/init.d/rclone.sh
#! /bin/sh
export RCLONE_DRIVE_ACKNOWLEDGE_ABUSE=true
rclone mount GoogleDrive:/  /root/fileServer/GoogleDrive --allow-other --allow-non-empty --vfs-cache-mode writes &
/root/fileServer/server -path /root/fileServer &
  • /etc/rc5.d/ 中创建软连接
cd /etc/rc5.d/ && ln -s ../init.d/rclone.sh  S10rclone.sh
  • reboot 重启vps