百分百源码网-让建站变得如此简单! 登录 注册 签到领金币!

主页 | 如何升级VIP | TAG标签

当前位置: 主页>网站教程>服务器> linux如何增加开机启动足本?-linux运维
分享文章到:

linux如何增加开机启动足本?-linux运维

发布时间:09/01 来源:未知 浏览: 关键词:
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如何增加开机启动足本?的细致内容,更多请关注 百分百源码网 其它相干文章!

打赏

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

百分百源码网 建议打赏1~10元,土豪随意,感谢您的阅读!

共有153人阅读,期待你的评论!发表评论
昵称: 网址: 验证码: 点击我更换图片
最新评论

本文标签

广告赞助

能出一分力是一分吧!

订阅获得更多模板

本文标签

广告赞助

订阅获得更多模板