Linux开机自动启动服务

当我们的Linux,关机后,或者重启后,有些服务需要人工启动才能有。为了解决这个问题,我找了一个办法,用一个脚本,开机启动想启动的服务。

方法一:(强烈推荐)

1、写一个脚本auto.sh

vim   /hom/xxx/auto.sh

chmod   +x   auto.sh

auto.sh中

 1 #!/bin/sh
 2 
 3 #auto.sh log
 4 DIR=/var/log
 5 LOGFILE=auto_start_log.txt
 6 echo start system by rc.local `date "+%Y-%m-%d %H:%M:%S"` >> $DIR/$LOGFILE
 7 
 8 # auto service
 9 systemctl restart firewalld.service
10 echo  `date "+%Y-%m-%d %H:%M:%S"`  restart firewalld  >> $DIR/$LOGFILE
View Code

 

 

2、添加开机启动项到rc.local

echo  /hom/xxx/auto.sh >> /etc/rc.local


 

方法二:以下方法不好用,脚本会被执行2次。。。。额。。。

1 cd /etc/rc.d/init.d/     进入该文件夹
2 vi auto.sh        编写脚本
3 chmod +x auto.sh        给脚本添加执行权限
4 chkconfig --add auto.sh      添加脚本至开机自启
5 chkconfig auto.sh on     开启
6 reboot        重启

 

参考:

https://blog.csdn.net/softwarenb/article/details/80275479

https://www.cnblogs.com/xianhaiyan/p/7650391.html

https://blog.csdn.net/jk110333/article/details/8590746/

cd /etc/rc.d/init.d/ 进入该文件夹vi auto.sh 编写脚本chmod +x auto.sh 给脚本添加执行权限chkconfig --add auto.sh  添加脚本至开机自启chkconfig auto.sh on 开启reboot 重启

posted @ 2019-07-08 16:49  安迪9468  阅读(2528)  评论(0编辑  收藏  举报