天天看點

關于自定義腳本rc.local裡開機不啟動的問題--以tomcat和perl相關的腳本為例

本文将自己遇到的一些自定義腳本加入開機啟動項卻不成功的問題加以說明,花費了我很長時間才得以解決,當然也多謝了自己朋友的幫忙,正是因為他們的提醒,最後才找到了解決的辦法,謝謝他們!!!! 

系統是centos5.5 

應用:nginx+perl+fastcgi以及tomcat

現有幾個腳本需加入/etc/rc.local裡讓他們開機自己啟動,如下: 

# vi /etc/rc.local

/usr/local/nginx/sbin/start_perl_cgi.sh stop

/usr/local/nginx/sbin/start_perl_cgi.sh start

/usr/local/tomcat/bin/startup.sh

最後查找資料說tomcat不啟動是因為需要相關的環境變量,而且它需要的環境變量已經在/etc/profile裡進行了設定: 

<a target="_blank" href="http://blog.51cto.com/attachment/201111/105912779.jpg"></a>

但是死活不開機自動啟動,隻是找不到需要的jdk,如下設定即可: 

source /etc/profile

或者: 

export JAVA_HOME=/usr/local/jdk

這樣tomcat就能開機自動啟動了.

最讓人頭疼的是:/usr/local/nginx/sbin/start_perl_cgi.sh start怎麼弄都不成功,不開機啟動,但是手動執行卻總是能成功!

腳本内容如下,其實這個腳本很簡單,相信你應該沒問題的,這個腳本網上很多:

#vi /usr/local/nginx/sbin/start_perl_cgi.sh

#!/bin/bash

#set -x 

if [[ $# != 1 ]];then

echo "usage $0 start|stop|restart"

exit 1

fi

dir=/usr/local/nginx

stop ()

{

if [ -e $dir/logs/perl-fcgi.pid ];then

kill -USR1 `cat $dir/logs/perl-fcgi.pid` 2&gt;&amp;1&gt; /dev/null

rm -f $dir/logs/perl-fcgi.pid 2&gt;&amp;1&gt; /dev/null

if [ -e $dir/logs/perl-fcgi.sock ];then 

rm -f $dir/logs/perl-fcgi.sock 2&gt;&amp;1&gt; /dev/null

fi       ##紅色部分網上的沒有加if判斷語句和-f選項,在這裡我加了,首先對其進行判斷一下,不然在沒有該檔案的情況下會有提示資訊: 

<a target="_blank" href="http://blog.51cto.com/attachment/201111/110920509.jpg"></a>

echo "stop perl-fcgi done"

}

start ()

rm -f $dir/now_start_perl_fcgi.sh 2&gt;&amp;1 &gt;/dev/null

chown nobody.nobody $dir/logs

echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" &gt;&gt;$dir/now_start_perl_fcgi.sh

chown nobody.nobody $dir/now_start_perl_fcgi.sh

chmod u+x $dir/now_start_perl_fcgi.sh

sudo -u nobody $dir/now_start_perl_fcgi.sh

echo "start perl-fcgi done"

case $1 in

stop)

stop

;;

start)

start

restart)

esac

其實看完這個腳本是沒有問題的!請注意裡面藍色部分注釋了的内容(#set -x 這裡我沒有開啟調試資訊)

于是上網查找資料,有的說是rc.local裡面需要使用絕對路徑,有的說是權限問題.但是經過排查都不是這樣的問題. 

比如說不能調用有變量的路徑即:$dir

可以在rc.local裡實驗一下:

#vi /etc/rc.local

echo "hello,this is $dir" &gt; /home/zhangzj/ceshi

儲存退出,檢視: #cat /home/zhangzj/ceshi

hello,this is /usr/local/nginx   #說明這樣使用沒有問題

最後在一朋友的幫助下說開啟調試資訊即:将set -x 打開,進行如下操作:

/usr/local/nginx/sbin/start_perl_cgi.sh stop &gt; /home/zhangzj/stop

/usr/local/nginx/sbin/start_perl_cgi.sh start &gt;/home/zhangzj/start

儲存,退出:執行source /etc/rc.local 讓其生效,最好執行,檢視有沒有錯誤設定以免造成開機無法啟動的内容.檢視調試資訊:

#cat /home/zhangzj/stop

#cat /home/zhangzj/start  

看他們有什麼問題,然後針對問題進行解決.但是我這樣執行後我這裡沒有錯誤,于是我将機器重新開機,再次檢視上面調試資訊時,出現以下情況:

<a target="_blank" href="http://blog.51cto.com/attachment/201111/112741555.jpg"></a>

注意紅色的部分: sudo: sorry, you must have a tty to run sudo 

!!!!意思是執行sudo 的shell預設需要tty(終端),而這裡沒有.在 /etc/rc.local 中的指令,是沒有控制終端的。

上網查找資料說,隻需如下操作即可:

注釋掉 /etc/sudoers中 ‘Defaults requiretty ’.儲存退出,機器重新開機,問題得以解決.

<a target="_blank" href="http://blog.51cto.com/attachment/201111/113244873.jpg"></a>

關于requiretty的解釋如下:

If set, sudo will only run when the user is logged in to a real tty. When this flag is set, sudo can only be run from a login session and not via other means such as cron(8) or cgi-bin scripts. This flag is off by default.

1) Defaults  requiretty,修改為 #Defaults  requiretty,表示不需要控制終端。

2) Defaults  requiretty,修改為 Defaults:nobody !requiretty,表示僅 nobody 使用者不需要控制終端。

3) 如果修改為 Defaults:%nobody !requiretty,表示僅 nobody 組不需要控制終端。

Defaults後面如果有冒号,是對後面使用者的預設,如果沒有,則是對所有使用者的預設。就像配置檔案中自帶的一行:

Defaults    env_reset

其實這是一個很簡單的問題,但是我卻花了很長時間才得以解決,就是因為當時沒有想到開啟調試資訊,可見在一個腳本執行不成功時,開啟調試資訊:set -x 檢視其執行過程是多麼的重要,這樣也便于我們查找問題的關鍵所在.當然當你頭腦發昏時,也不妨問問你的朋友,他們的意見也許對你很重要!!!!

好了,就寫到這裡了,希望對碰到同樣問題的朋友有所幫助,有不明白的歡迎留言!

本文轉自 zhangzj1030 51CTO部落格,原文連結:http://blog.51cto.com/tech110/704830