日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

Linux 最全的添加开机启动方法

發(fā)布時(shí)間:2023/12/18 linux 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux 最全的添加开机启动方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

添加開啟啟動(dòng)的方式一般有如下幾種

第一種:把開機(jī)命令,寫入到os的啟動(dòng)文件之中,伴隨著啟動(dòng)文件的讀取,順便幫你把要啟動(dòng)的任務(wù),一起啟動(dòng)了;
第二種:把啟動(dòng)任務(wù),放到os的啟動(dòng)目錄下。os啟動(dòng)過程之中,會(huì)執(zhí)行所有放在此目錄下的任務(wù);
第三種:os會(huì)給用戶提供了標(biāo)準(zhǔn)的方法,按照此流程接入;

系統(tǒng)啟動(dòng)時(shí)需要加載的配置文件

/etc/profile、/root/.bash_profile /etc/bashrc、/root/.bashrc /etc/profile.d/*.sh、/etc/profile.d/lang.sh /etc/sysconfig/i18n、/etc/rc.local(/etc/rc.d/rc.local)

一、直接修改配置文件,在配置文件中增加開機(jī)啟動(dòng)文件:

如:通過修改文件 /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開機(jī)啟動(dòng) /etc/init.d/nginx start # nginx開機(jī)啟動(dòng) supervisord -c /etc/supervisor/supervisord.conf # supervisord開機(jī)啟動(dòng) /bin/bash /server/scripts/test.sh >/dev/null 2>/dev/null# 3.最后修改rc.local文件的執(zhí)行權(quán)限 [root@localhost ~]# chmod +x /etc/rc.local [root@localhost ~]# chmod 755 /etc/rc.local

二、將需要啟動(dòng)的文件,放在os的啟動(dòng)讀取目錄下面

# 1.將(腳本)啟動(dòng)文件移動(dòng)到 /etc/init.d/或者/etc/rc.d/init.d/目錄下。(前者是后者的軟連接) mv /www/wwwroot/test.sh /etc/rc.d/init.d

三、通過chkconfig命令設(shè)置

# 1.將(腳本)啟動(dòng)文件移動(dòng)到 /etc/init.d/或者/etc/rc.d/init.d/目錄下。(前者是后者的軟連接) mv /www/wwwroot/test.sh /etc/rc.d/init.d# 2.啟動(dòng)文件前面務(wù)必添加如下三行代碼,否側(cè)會(huì)提示chkconfig不支持。 #!/bin/sh 告訴系統(tǒng)使用的shell,所以的shell腳本都是這樣 #chkconfig: 35 20 80 分別代表運(yùn)行級(jí)別,啟動(dòng)優(yōu)先權(quán),關(guān)閉優(yōu)先權(quán),此行代碼必須 #description: http server 自己隨便發(fā)揮!!!,此行代碼必須 /bin/echo $(/bin/date +%F_%T) >> /tmp/test.log# 3.增加腳本的可執(zhí)行權(quán)限 chmod +x /etc/rc.d/init.d/test.sh# 4.添加腳本到開機(jī)自動(dòng)啟動(dòng)項(xiàng)目中。添加到chkconfig,開機(jī)自啟動(dòng)。 [root@localhost ~]# cd /etc/rc.d/init.d [root@localhost ~]# chkconfig --add test.sh [root@localhost ~]# chkconfig test.sh on# 5.關(guān)閉開機(jī)啟動(dòng) [root@localhost ~]# chkconfig test.sh off# 6.從chkconfig管理中刪除test.sh [root@localhost ~]# chkconfig --del test.sh# 7.查看chkconfig管理 [root@localhost ~]# chkconfig --list test.sh

四、自定義服務(wù)文件,添加到系統(tǒng)服務(wù),通過Systemctl管理

1.寫服務(wù)文件

格式如下: [Unit]:服務(wù)的說明 Description:描述服務(wù) After:描述服務(wù)類別[Service]服務(wù)運(yùn)行參數(shù)的設(shè)置 Type=forking 是后臺(tái)運(yùn)行的形式 ExecStart 為服務(wù)的具體運(yùn)行命令 ExecReload 為服務(wù)的重啟命令 ExecStop 為服務(wù)的停止命令 PrivateTmp=True 表示給服務(wù)分配獨(dú)立的臨時(shí)空間 注意:啟動(dòng)、重啟、停止命令全部要求使用絕對(duì)路徑

2.文件保存在目錄下:以754的權(quán)限。目錄路徑:/usr/lib/systemd/system。

如上面的supervisord.service文件放在這個(gè)目錄下面。 [root@localhost ~]# cat /usr/lib/systemd/system/nginx.service [root@localhost ~]# cat /usr/lib/systemd/system/supervisord.service

3.設(shè)置開機(jī)自啟動(dòng)(任意目錄下執(zhí)行)

如果執(zhí)行啟動(dòng)命令報(bào)錯(cuò),則執(zhí)行:systemctl daemon-reload

設(shè)置開機(jī)自啟動(dòng) [root@localhost ~]# systemctl enable nginx.service ? ? [root@localhost ~]# systemctl enable supervisord停止開機(jī)自啟動(dòng) [root@localhost ~]# systemctl disable nginx.service [root@localhost ~]# systemctl disable supervisord驗(yàn)證一下是否為開機(jī)啟動(dòng) [root@localhost ~]# systemctl is-enabled nginx [root@localhost ~]# systemctl is-enabled supervisord

4.服務(wù)文件示例:

# supervisord.service進(jìn)程管理服務(wù)文件 [Unit] Description=Process Monitoring and Control Daemon ?# 內(nèi)容自己定義: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服務(wù)文件 [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服務(wù)文件 [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

總結(jié)

以上是生活随笔為你收集整理的Linux 最全的添加开机启动方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。