背景
首先,rc.local是Linux啟動程序在login程序前執行的最后一個腳本,有的服務器中在rc.local中可能會有一句touch /var/lock/subsys/local,這是干什么的呢,在百度中沒找到,最終在Linuxquestions.org論壇成功找到滿意的解答。
touch
首先要了解touch這個命令是做什么用的,在此用於創建一個不存在的文件,詳細了解請見Linux touch命令
解釋
/var/lock/subsys/local這個文件的存在證明rc.local這個腳本已經執行過了,目的在於避免這個腳本重復執行,除非這個文件不存在時,它才失效,也就是當系統關閉(shut down)時會發生,翻譯的不是很好,原文是
What this does is create a lock file that tells the system that ‘local’ is up and running already. It prevents the script from being run twice, as it will fail until the lockfile is removed, which will happen when you shut down.
Typically this is used with bigger services such as database servers and so forth to make sure they are not started twice.
這樣做的目的是用於一些大型服務,例如數據庫服務器,以確保它不會啟動兩次的情況出現。
至於touch出的這個文件什么時候被讀取以避免再次生成,論壇兩哥們是這么解釋的
To tell the truth I don’t think anything looks there. I have never seen a lock mechanism in place for the rc.local script. Like I was saying, usually it is only used for bigger services. However, it is usually checked by the script itself.
If you have a look at one of your more complicated rc scripts you may see in the ‘start’ function something like (pseudo code):
Code:
start() { if /var/lock/subsys/myapp exists; then echo "myapp already started" exit else touch /var/lock/subsys/myapp /command/to/start/myapp fi }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
也就是會在一個地方,會有類似與上面start()這樣的方法會去判斷是否已經存在一個local文件,如果有就不重復創建,如果沒有就創建一個,后面再讀取的時候就不會去創建,以此避免系統重復啟動。