测试脚本
编写测试脚本如下:
#!/bin/bash
while [ true ]
do
echo "llll"
sleep 2
done
2.修改rc.local文件
如果系统存在/etc/rc.local文件,在exit 0前加上需要开机自启的命令即可。注意,rc.local需要有可执行权限。如果没有rc.local文件,则需创建该文件,rc.local文件内容如下:
#!/bin/sh
/home/kylin/test.sh &
exit 0
查看rc-local服务状态
root@kylin-pc:/home/kylin/桌面# systemctl status rc-local
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /usr/lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (running) since Tue 2023-12-05 21:40:42 CST; 1min 3s ago
Docs: man:systemd-rc-local-generator(8)
Tasks: 2 (limit: 2243)
Memory: 564.0K
CGroup: /system.slice/rc-local.service
├─ 832 /bin/bash /home/kylin/test.sh
└─5426 sleep 2
12月 05 21:41:27 kylin-pc rc.local[832]: llll
12月 05 21:41:29 kylin-pc rc.local[832]: llll
12月 05 21:41:31 kylin-pc rc.local[832]: llll
12月 05 21:41:33 kylin-pc rc.local[832]: llll
12月 05 21:41:35 kylin-pc rc.local[832]: llll
12月 05 21:41:37 kylin-pc rc.local[832]: llll
12月 05 21:41:39 kylin-pc rc.local[832]: llll
12月 05 21:41:41 kylin-pc rc.local[832]: llll
12月 05 21:41:43 kylin-pc rc.local[832]: llll
12月 05 21:41:45 kylin-pc rc.local[832]: llll
systemctl start rc-local #启动服务
然后重启,系统重启后,通过ps命令可以查看开机自启的脚本
root@kylin-pc:/home/kylin/桌面# ps -ef | grep test
root 832 1 0 21:40 ? 00:00:00 /bin/bash /home/kylin/test.sh
root 6680 5270 0 21:51 pts/0 00:00:00 grep --color=auto test
root@kylin-pc:/home/kylin/桌面#
3. init.d方式
在/etc/inid.d目录下创建开机自启脚本
root@kylin-pc:/home/kylin# cat /etc/init.d/test_start
#!/bin/sh
/home/kylin/test.sh &
root@kylin-pc:/home/kylin#
root@kylin-pc:/etc/init.d# ln -s /etc/init.d/test_start /etc/rc3.d/S90test_start
root@kylin-pc:/etc/init.d# ln -s /etc/init.d/test_start /etc/rc4.d/S90test_start
root@kylin-pc:/etc/init.d# ln -s /etc/init.d/test_start /etc/rc5.d/S90test_start
root@kylin-pc:/home/kylin# ls -l /etc/rc5.d/S90test_start
lrwxrwxrwx 1 root root 22 12月 5 22:00 /etc/rc5.d/S90test_start -> /etc/init.d/test_start
root@kylin-pc:/home/kylin#
root@kylin-pc:/home/kylin/桌面# ps -ef | grep test
root 1016 1 0 22:03 ? 00:00:00 /bin/bash /home/kylin/test.sh
root 5257 5220 0 22:04 pts/0 00:00:00 grep --color=auto test
root@kylin-pc:/home/kylin/桌面#
或者使用update-rc.d命令加入开机自启,此时,开机自启脚本需要加入LSB信息,内容如下:
root@kylin-pc:/home/kylin/桌面# cat /etc/init.d/test_start
#!/bin/bash
### BEGIN INIT INFO
# Provides: test
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts test
# Description: starts the test
### END INIT INFO
/home/kylin/test.sh &
root@kylin-pc:/home/kylin/桌面#
update-rc.d使用方法如下
root@kylin-pc:/home/kylin/桌面# update-rc.d --help
usage: update-rc.d [-f] <basename> remove
update-rc.d [-f] <basename> defaults
update-rc.d [-f] <basename> defaults-disabled
update-rc.d <basename> disable|enable [S|2|3|4|5]
-f: force
The disable|enable API is not stable and might change in the future.
root@kylin-pc:/home/kylin/桌面#
4./etc/profile.d/方式
把自启动脚本放在/etc/profile.d/
其实该文件主要是用来加载环境变量的,最好不要使用该方式
root@kylin-pc:/home/kylin/桌面# ps -ef | grep test
kylin 1332 1328 0 22:26 ? 00:00:00 /bin/bash /home/kylin/test.sh
root 5297 5253 0 22:27 pts/0 00:00:00 grep --color=auto test
root@kylin-pc:/home/kylin/桌面#
注意该开机自启运行方式是以登录用户的权限运行的,不是以root用户的方式运行的。
5.基于systemd服务
1、进入启动目录:/etc/systemd/system/
2、编写启动脚本并保存,下述是一个简单的模板
[Unit]
#描述服务的简短说明,用于标识和识别服务。
Description=My Service
#指定服务应该在哪个目标之后启动。network.target服务将在网络目标(network.target)之后启动,确保网络已经可用。
After=network.target
[Service]
#指定要执行的命令或脚本的路径。
ExecStart=/path/to/your/script.sh
#指定服务的工作目录,即执行命令或脚本时的当前目录
WorkingDirectory=/path/to/your/working/directory
#定义服务在失败或意外退出后的行为,always表示无论什么原因导致服务退出,都会自动重新启动。
Restart=always
[Install]
#指定服务应该被安装到哪个目标。multi-user.target表示服务将被安装到multi-user.target,即多用户目标,表示服务将在多用户模式下启动。
WantedBy=multi-user.target
# 刷新配置(必须先执行)
systemctl daemon-reload
#服务加入开机自启
systemctl enable ***.service
#启动
systemctl start ***.service
#查看状态
systemctl status ***.service
6. /etc/xdg/autostart 方式
利用Linux的 .desktop文件实现开机启动。
在/etc/xdg/autostart 目录下建立一个 test.desktop文件,并对文件进行以下编辑。
操作步骤
打开/etc/xdg/autostart目录
cd /etc/xdg/autostart
建立test.desktop文件
touch test.desktop
编写文件并保存
sudo vim test.desktop
添加如下代码:
[Desktop Entry]
Name=Test
Exec=/root/Test
Type=Application
此设置开机自启动的方法与rc.local方法不同的是,此方法适合桌面级软件的开机自启动(软件有界面)
7. 定时任务方式
可以参考如下:
Linux定时任务详解