最詳細的CentOS7設(shè)置自定義開機啟動服務(wù)教程

CentOS 7 添加開機啟動有兩種方式:

一鞋屈、添加開機啟動服務(wù)

Centos 系統(tǒng)服務(wù)腳本目錄:

/usr/lib/systemd/

有系統(tǒng)(system)和用戶(user)之分,如需要開機沒有登陸情況下就能運行的程序,存在系統(tǒng)服務(wù)(system)里项贺,即:

/usr/lib/systemd/system/

反之勇边,用戶登錄后才能運行的程序,存在用戶(user)里横腿,服務(wù)以.service結(jié)尾颓屑。

/usr/lib/systemd/user/

這邊以nginx開機運行為例:

1.建立服務(wù)文件

vim /lib/systemd/system/nginx.service

[Unit]    
Description=nginx    
After=network.target         
[Service]    
Type=forking    
User=nginx
Group=nginx
ExecStart=/etc/init.d/nginx start    
ExecReload=/etc/init.d/nginx restart    
ExecStop=/etc/init.d/nginx  stop    
PrivateTmp=true    
[Install]    
WantedBy=multi-user.target   

Description:描述服務(wù)
After:描述服務(wù)類別
[Service]服務(wù)運行參數(shù)的設(shè)置
Type=forking是后臺運行的形式
User服務(wù)啟動用戶
Group服務(wù)啟動用戶組
ExecStart為服務(wù)的具體運行命令
ExecReload為重啟命令
ExecStop為停止命令
PrivateTmp=True表示給服務(wù)分配獨立的臨時空間

注意:[Service]的啟動斤寂、重啟、停止命令全部要求使用絕對路徑
[Install]服務(wù)安裝的相關(guān)設(shè)置揪惦,可設(shè)置為多用戶

2.保存目錄

以754的權(quán)限保存在目錄:

/lib/systemd/system

3.設(shè)置開機自啟動

systemctl enable nginx.service  

4.其他命令

任務(wù) 舊指令 新指令
使某服務(wù)自動啟動 Chkconfig --level 3 httpd on systemctl enable httpd.service
使某服務(wù)不自動啟動 chkconfig --level 3 httpd off systemctl disable httpd.service
檢查服務(wù)狀態(tài) service httpd status systemctl status httpd.service (服務(wù)詳細信息)
systemctl is-active httpd.service(僅顯示是否Active)
顯示所有已啟動的服務(wù) chkconfig --list systemctl list-unit-files
systemctl list-units --type=service
啟動某服務(wù) service httpd start systemctl start httpd.service
停止某服務(wù) service httpd stop systemctl stop httpd.service
重啟某服務(wù) service httpd restart systemctl restart httpd.service

啟動nginx服務(wù):

systemctl start nginx.service

設(shè)置開機自啟動:

systemctl enable nginx.service

停止開機自啟動:

systemctl disable nginx.service 

查看服務(wù)當(dāng)前狀態(tài):

systemctl status nginx.service

重新啟動服務(wù):

systemctl restart nginx.service

查看所有已啟動的服務(wù):

systemctl list-units --type=service
systemctl list-unit-files

常用服務(wù)文件

1.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
2.mysql.service

[Unit]
Description=mysql
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
#ExecReload=/usr/local/mysql/support-files/mysql.server restart
#ExecStop=/usr/local/mysql/support-files/mysql.server stop
#PrivateTmp=true

[Install]
WantedBy=multi-user.target

3.php-fpm.service

[Unit]
Description=php
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm

[Install]
WantedBy=multi-user.target

4.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

5.supervisord.service

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
SysVStartPriority=99

[Install]
WantedBy=multi-user.target

二遍搞、添加開機啟動腳本

在centos7中增加腳本有兩種常用的方法,以腳本autostart.sh為例:

#!/bin/bash
#description:開機自啟腳本
/mnt/shell/autostart.sh  #啟動nginx

方法一

1器腋、賦予腳本可執(zhí)行權(quán)限(/mnt/shell/autostart.sh是你的腳本路徑)

chmod +x /mnt/shell/autostart.sh 

2溪猿、打開/etc/rc.d/rc/local文件,在末尾增加如下內(nèi)容

/mnt/shell/autostart.sh 

3纫塌、在centos7中诊县,/etc/rc.d/rc.local的權(quán)限被降低了,所以需要執(zhí)行如下命令賦予其可執(zhí)行權(quán)限

chmod +x /etc/rc.d/rc.local

方法二

1措左、將腳本移動到/etc/rc.d/init.d目錄下

mv  /mnt/shell/autostart.sh /etc/rc.d/init.d

2依痊、增加腳本的可執(zhí)行權(quán)限

chmod +x  /etc/rc.d/init.d/autostart.sh

3、添加腳本到開機自動啟動項目中

cd /etc/rc.d/init.d
chkconfig --add autostart.sh
chkconfig autostart.sh on
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末怎披,一起剝皮案震驚了整個濱河市胸嘁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌凉逛,老刑警劉巖性宏,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異鱼炒,居然都是意外死亡衔沼,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門昔瞧,熙熙樓的掌柜王于貴愁眉苦臉地迎上來指蚁,“玉大人,你說我怎么就攤上這事自晰∧” “怎么了?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵酬荞,是天一觀的道長搓劫。 經(jīng)常有香客問我,道長混巧,這世上最難降的妖魔是什么枪向? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮咧党,結(jié)果婚禮上秘蛔,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好深员,可當(dāng)我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布负蠕。 她就那樣靜靜地躺著,像睡著了一般倦畅。 火紅的嫁衣襯著肌膚如雪遮糖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天叠赐,我揣著相機與錄音欲账,去河邊找鬼。 笑死芭概,一個胖子當(dāng)著我的面吹牛敬惦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谈山,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼俄删,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了奏路?” 一聲冷哼從身側(cè)響起畴椰,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鸽粉,沒想到半個月后斜脂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡触机,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年帚戳,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片儡首。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡片任,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出蔬胯,到底是詐尸還是另有隱情对供,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布氛濒,位于F島的核電站产场,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏舞竿。R本人自食惡果不足惜京景,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望骗奖。 院中可真熱鬧确徙,春花似錦靡菇、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鼻吮。三九已至育苟,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間椎木,已是汗流浹背违柏。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留香椎,地道東北人漱竖。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像畜伐,于是被迫代替她去往敵國和親馍惹。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容