热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

linux怎样添加开机启动脚本?-linux运维

linux添加开机启动脚本的方法:1、修改开机启动文件【etcrc.local】;2、写一个shell脚本;3、完成chkconfig命令设置;4、自定义服务文件且添加到系统服务,并通过Systemctl管理。

/etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)

一、修改开机启动文件:/etc/rc.local(或者/etc/rc.d/rc.local)

# 1.编辑rc.local文件
[root@localhost ~]# vi /etc/rc.local
 
# 2.修改rc.local文件,在 exit 0 前面加入以下命令。保存并退出。
/etc/init.d/mysqld start                     # mysql开机启动
/etc/init.d/nginx start                     # nginx开机启动
supervisord -c /etc/supervisor/supervisord.conf         # supervisord开机启动
/bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null
 
# 3.最后修改rc.local文件的执行权限
[root@localhost ~]# chmod +x /etc/rc.local
[root@localhost ~]# chmod 755 /etc/rc.local

二、自己写一个shell脚本

将写好的脚本(.sh文件)放到目录 /etc/profile.d/ 下,系统启动后就会自动执行该目录下的所有shell脚本。

三、通过chkconfig命令设置

# 1.将(脚本)启动文件移动到 /etc/init.d/或者/etc/rc.d/init.d/目录下。(前者是后者的软连接)
mv /www/wwwroot/test.sh /etc/rc.d/init.d
 
# 2.启动文件前面务必添加如下三行代码,否侧会提示chkconfig不支持。
#!/bin/sh             告诉系统使用的shell,所以的shell脚本都是这样
#chkconfig: 35 20 80        分别代表运行级别,启动优先权,关闭优先权,此行代码必须
#description: http server     自己随便发挥!!!,此行代码必须
/bin/echo $(/bin/date +%F_%T) >> /tmp/test.log
 
# 3.增加脚本的可执行权限
chmod +x /etc/rc.d/init.d/test.sh
 
# 4.添加脚本到开机自动启动项目中。添加到chkconfig,开机自启动。
[root@localhost ~]# cd /etc/rc.d/init.d
[root@localhost ~]# chkconfig --add test.sh
[root@localhost ~]# chkconfig test.sh on
 
# 5.关闭开机启动
[root@localhost ~]# chkconfig test.sh off
 
# 6.从chkconfig管理中删除test.sh
[root@localhost ~]# chkconfig --del test.sh
 
# 7.查看chkconfig管理
[root@localhost ~]# chkconfig --list test.sh

四、自定义服务文件,添加到系统服务,通过Systemctl管理

1.写服务文件:如nginx.service、redis.service、supervisord.service

[Unit]:服务的说明
Description:描述服务
After:描述服务类别
 
[Service]服务运行参数的设置
Type=forking      是后台运行的形式
ExecStart        为服务的具体运行命令
ExecReload       为服务的重启命令
ExecStop        为服务的停止命令
PrivateTmp=True     表示给服务分配独立的临时空间
注意:启动、重启、停止命令全部要求使用绝对路径
 
[Install]        服务安装的相关设置,可设置为多用户
WantedBy=multi-user.target

2.文件保存在目录下:以754的权限。目录路径:/usr/lib/systemd/system。如上面的supervisord.service文件放在这个目录下面。

[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service

3.设置开机自启动(任意目录下执行)。如果执行启动命令报错,则执行:systemctl daemon-reload

设置开机自启动
[root@localhost ~]# systemctl enable nginx.service   
[root@localhost ~]# systemctl enable supervisord
 
停止开机自启动
[root@localhost ~]# systemctl disable nginx.service
[root@localhost ~]# systemctl disable supervisord
 
验证一下是否为开机启动
[root@localhost ~]# systemctl is-enabled nginx
[root@localhost ~]# systemctl is-enabled supervisord

4.其他命令

启动nginx服务
[root@localhost ~]# systemctl start nginx.service
 
停止nginx服务
[root@localhost ~]# systemctl start nginx.service
 
重启nginx服务
[root@localhost ~]# systemctl restart nginx.service
 
查看nginx服务当前状态
[root@localhost ~]# systemctl status nginx.service
 
查看所有已启动的服务
[root@localhost ~]# systemctl list-units --type=service

5.服务文件示例:

# supervisord.service进程管理服务文件
[Unit]
Description=Process Monitoring and Control Daemon  # 内容自己定义:Description=Supervisor daemon
After=rc-local.service nss-user-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop= /usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
Restart=on-failure
RestartSec=42s
KillMode=process
 
[Install]
WantedBy=multi-user.target
# nginx.service服务文件
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
 
[Install]
WantedBy=multi-user.target
# redis.service服务文件
[Unit]
Description=Redis
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /etc/redis.conf
ExecStop=kill -INT `cat /tmp/redis.pid`
User=www
Group=www
 
[Install]
WantedBy=multi-user.target

推荐教程:《linux视频教程》

以上就是linux怎样添加开机启动脚本?的详细内容,更多请关注 第一PHP社区 其它相关文章!


推荐阅读
  • 负载均衡_Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分
    nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解 ... [详细]
  • 本文介绍了在无法联网的情况下,通过下载rpm包离线安装zip和unzip的方法。详细介绍了如何搜索并下载合适的rpm包,以及如何使用rpm命令进行安装。 ... [详细]
  • Linux一键安装web环境全攻略
    摘自阿里云服务器官网,此处一键安装包下载:点此下载安装须知1、此安装包可在阿里云所有Linux系统上部署安装,此安装包包含的软件及版本为& ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • Metasploit攻击渗透实践
    本文介绍了Metasploit攻击渗透实践的内容和要求,包括主动攻击、针对浏览器和客户端的攻击,以及成功应用辅助模块的实践过程。其中涉及使用Hydra在不知道密码的情况下攻击metsploit2靶机获取密码,以及攻击浏览器中的tomcat服务的具体步骤。同时还讲解了爆破密码的方法和设置攻击目标主机的相关参数。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
  • 众筹商城与传统商城的区别及php众筹网站的程序源码
    本文介绍了众筹商城与传统商城的区别,包括所售产品和玩法不同以及运营方式不同。同时还提到了php众筹网站的程序源码和方维众筹的安装和环境问题。 ... [详细]
  • 一次上线事故,30岁+的程序员踩坑经验之谈
    本文主要介绍了一位30岁+的程序员在一次上线事故中踩坑的经验之谈。文章提到了在双十一活动期间,作为一个在线医疗项目,他们进行了优惠折扣活动的升级改造。然而,在上线前的最后一天,由于大量数据请求,导致部分接口出现问题。作者通过部署两台opentsdb来解决问题,但读数据的opentsdb仍然经常假死。作者只能查询最近24小时的数据。这次事故给他带来了很多教训和经验。 ... [详细]
  • 面试经验分享:华为面试四轮电话面试、一轮笔试、一轮主管视频面试、一轮hr视频面试
    最近有朋友去华为面试,面试经历包括四轮电话面试、一轮笔试、一轮主管视频面试、一轮hr视频面试。80%的人都在第一轮电话面试中失败,因为缺乏基础知识。面试问题涉及 ... [详细]
  • Linux下部署Symfoy2对app/cache和app/logs目录的权限设置,symfoy2logs
    php教程|php手册xml文件php教程-php手册Linux下部署Symfoy2对appcache和applogs目录的权限设置,symfoy2logs黑色记事本源码,vsco ... [详细]
  • (九)Docker常用安装
    一、总体步骤1、搜索镜像2、拉取镜像3、查看镜像4、启动镜像5、停止镜像6、移除镜像二、安装tomcat1、dockerhub上面查找tomcat镜像 dockersearchto ... [详细]
  • 有意向可以发简历到邮箱内推.简历直达组内Leader.能做同事的话,内推奖励全给你. ... [详细]
  • 本文介绍了在MacOS系统上安装MySQL的步骤,并详细说明了如何设置MySQL服务的开机启动和如何修改MySQL的密码。通过下载MySQL的macos版本并按照提示一步一步安装,在系统偏好设置中可以找到MySQL的图标进行设置。同时,还介绍了通过终端命令来修改MySQL的密码的具体操作步骤。 ... [详细]
author-avatar
暮色归家
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有