Linux系统下如何设置开机启动项

1.修改/etc/rc.d/rc.local文件,在rc.local 后面加上你的shell脚本命令

  • 赋值权限

chmod u+x app.sh

  • 修改后的rc.local如下

#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
/bin/bash /usr/myfoder/camtest/Debug/app.sh

本人使用这种方式测试失败,执行不成功

2. 使用systemctl命令

systemctl enable xxx.sh

本人测试失败,提示 systemctl access denied

3. 使用最原始的方式,在rc.d里面创建启动项

  • 新建shell脚本

#!/bin/sh
#chkconfig: 2345 80 90
#description:AutoStart

#下面是脚本正文

.....

其中AutoStart是启动的脚本名称描述,与shell脚本文件名相同!

  • 把Shell脚本放入/etc/rc.d/init.d/目录下

sudo mv ./AutoStart /etc/rc.d/init.d/AutoStart

  • 添加脚本的可执行权限

chmod u+x /etc/rc.d/init.d/AutoStart

  • 使用chkconfig命令把脚本添加进开机启动项目中

chkconfig --add AutoStart

chkconfig AutoStart on

执行完成后,可以检查一下/etc/rc.d/rc*.d下有没有生成相应的启动链接,指向刚才的脚本,80指的是启动的顺序,90指的是停止的顺序

可以在AutoStart命令中加入接受start和stop的输入参数的设置,因为系统在启动时会传递给脚本一个start的参数,告诉脚本现在要启动,在关闭时会传递给脚本一个stop的参数,告诉脚本现在要关闭程序,如果程序在关闭前要执行清理操作,就可以在这时完成了。

感谢以上提供技术分享的朋友

大道至简