CentOS 开机自启动脚本
開機時執行自己的腳本.
1.編寫自己的服務腳本
進入系統服務腳本目錄:
cd /etc/rc.d/init.d/
vi test
內容如下:
#!/bin/bash
#
# chkconfig: - 57 75
# description: test service
start() {
echo "Starting test ..."
echo 3 >> /tmp/sunyu.txt
}
stop() {
echo "Stopping test ..."
echo 2 >> /tmp/sunyu.txt
}
# See how we were called.
case "$1" in
??start)
start
;;
??stop)
stop
;;
??restart|force-reload)
stop
start
;;
??*)
echo $"Usage: $0 {start|stop|restart|force-reload}"
exit 2
esac
要注意的是, 文件的格式要是 unix. 可以通過 VI 命令 :set ff? 查看.如果不是, 執行的時候會報錯:
/bin/bash^M: bad interpreter
:set fileformat=unix --> 設置編碼為unix(還可以設置為 doc)
VI 中保存
將該腳本設置為可執行:
chmod +x test
可以看到. 啟動服務的時候會輸出一行字. 然后往 /tmp/sunyu.txt 中寫入一個 3. 關閉時會往文件中寫入一個 2 如果該文件不存在, 請先行自己在 /tmp 下新建:?
cd /tmp/
touch sunyu.txt
然后將文件設置為可寫:
chmod 777 sunyu.txt
2.添加系統服務
chkconfig --add test
3.系統服務自啟動
chkconfig --level 345 test on
4.測試
service test start
然后查看 /tmp/sunyu.txt 中的內容: cat /tmp/sunyu.txt
service test stop
然后再查看上面文件中的內容
重啟:
reboot
再查看 /tmp/sunyu.txt 文件中的內容.如果看到變化表示服務添加成功.
此例通過后, 可在腳本中的 start stop 中做自己想做的其它事了.
轉載于:https://blog.51cto.com/332532/1891431
總結
以上是生活随笔為你收集整理的CentOS 开机自启动脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【云中沙箱】如何快速使用阿里云快速搭建论
- 下一篇: Spring @Scheduled关键字