Linux(CentOS7)开机自启动的三种是设置方式详细图解(init.d&fstab&rc.local) |
您所在的位置:网站首页 › cmos如何设置开机启动 › Linux(CentOS7)开机自启动的三种是设置方式详细图解(init.d&fstab&rc.local) |
Linux(CentOS7)设置开机自启动 一、创建执行文件 [root@CentOS7-0001 init.d]# cd /opt/script/ [root@CentOS7-0001 init.d]# touch auto_start.sh键入如下内容:( 使用root用户,开机自启动nginx) su - root /srv/software/nginx/nginx start 赋予可执行权限 [root@CentOS7-0001 ]# chmod -R 755 /opt/script/auto_start.sh方法一:更改/etc/rc.d/rc.local(常用) 以下三种里面配置都可以实现开机自启动,其实差不多,只是服务器启动的时候执行的顺序先后的问题。一般都是用rc.local。 1、/etc/init.d (少用) 目录存放开机初始化启动脚本,将脚本放入如:网络,环境变量等 2、/etc/fstab(少用) 系统初始化后,程序启动前加载(如磁盘挂载,mount命令等) 3、/etc/rc.local (常用) 系统完成之后,执行。(如:应用服务启动,nginx启动等) (常用) nfs放stab里面会在程序启动前加载上NFS文件系统,放到rc.local里如有程序依赖会造成程序启动加载时找不到路径。 以rc.local为例: 在Centos7下,rc.local文件,是个软链接实际文件不具备执行权限,设置开机启动,需要给文件授权 [root@CentOS7-0001 init.d]# ll /etc/rc.local lrwxrwxrwx. 1 root root 13 1月 25 09:42 /etc/rc.local -> rc.d/rc.local [root@CentOS7-0001 ]# chmod +x /etc/rc.d/rc.local [root@CentOS7-0001 /]# vi /etc/rc.d/rc.local文本结尾加入以下命令,保存退出,重启验证即可。 /opt/script/auto_start.sh 方法二:crontab定时计划中的@reboot(少用) [root@CentOS7-0001 /]# vi /etc/crontab在文本结尾加上以下命令,保存退出即可,重启验证即可。 @reboot /opt/script/auto_start.sh 方法三:使用systemd服务(少用) 在/etc/systemd/system下创建auto_start.service文件,将执行脚本设置成系统服务器,就可以使用系统自带的设置开机启动服务。 [root@CentOS7-0001 /]# vi /etc/systemd/system/auto_start.service//键入以下内容 [Unit] Description=The app auto run when reboot After=default.target [Service] ExecStart=/opt/script/auto_start.sh [Install] WantedBy=default.target //重启systemd服务、设置开机自启动,重启服务验证即可 [root@CentOS7-0001 /]# systemctl daemon-reload [root@CentOS7-0001 /]# systemctl enable auto_start.service [root@CentOS7-0001 /]# reboot |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |