Linux将shell脚本设置为自动启动项

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的参数,告诉脚本现在要关闭程序,如果程序在关闭前要执行清理操作,就可以在这时完成了。

参考博客:http://blog.csdn.net/easonchenys/article/details/40892307

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

大道至简

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星火燎猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值