Chinaunix首页 | 论坛 | 博客
  • 博客访问: 924655
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: LINUX

2011-11-03 10:32:30

  1. 由于自己的nginx 用了perl-cgi 脚本文件,同时也借用了网上的写的启动脚本,在手动的情况下没有任何问题,
  2. 但是在开机启动时就是不能启动。
  3. 第一种解决方法
  4. 1.启动perl-cgi 的perl 脚本
  5. ~]# cat /usr/local/nginx/sbin/start_perl_cgi.sh
  6. #!/bin/bash
  7. set -x
  8. # Path manipulation
  9. if [[ $# != 1 ]];then
  10. echo "usage $0 start|stop|restart"
  11. exit 1
  12. fi
  13. dir=/usr/local/nginx
  14. stop ()
  15. {
  16. if [ -e $dir/logs/perl-fcgi.pid ];then
  17. kill -USR1 `cat $dir/logs/perl-fcgi.pid` 2>&1> /dev/null
  18. rm -f $dir/logs/perl-fcgi.pid 2>&1> /dev/null
  19. fi
  20. if [ -e $dir/logs/perl-fcgi.sock ];then
  21. rm -f $dir/logs/perl-fcgi.sock 2>&1> /dev/null
  22. fi
  23. echo "stop perl-fcgi done"
  24. }
  25. start ()
  26. {
  27. rm -f $dir/now_start_perl_fcgi.sh 2>&1 >/dev/null
  28. chown nobody.nobody $dir/logs
  29. echo "$dir/perl-fcgi.pl -l $dir/logs/perl-fcgi.log -pid $dir/logs/perl-fcgi.pid -S $dir/logs/perl-fcgi.sock" >>$dir/now_start_perl_fcgi.sh
  30. chown nobody.nobody $dir/now_start_perl_fcgi.sh
  31. chmod u+x $dir/now_start_perl_fcgi.sh
  32. sudo -u nobody $dir/now_start_perl_fcgi.sh #没改之前的
  33. #su - nobody -c '/usr/local/nginx/now_start_perl_fcgi.sh' #修改后用这个正常
  34. echo "start perl-fcgi done"
  35. }
  36. case $1 in
  37. stop)
  38. stop
  39. ;;
  40. start)
  41. start
  42. ;;
  43. restart)
  44. stop
  45. start
  46. ;;
  47. esac
  48. 2 然后加到rc.local文件中
  49. /usr/local/nginx/sbin/start_perl_cgi.sh stop
  50. /usr/local/nginx/sbin/start_perl_cgi.sh start

  51. 启动失败
  52. 日志没有任何信息。

  53. 第二种
  54.     然后我尝试用系统服务启动试试
  55. 我在etc 下 编辑/etc/init.d/perlcgi 启动脚本
  56. ]# cat /etc/init.d/perlcgi
  57. #!/bin/bash
  58. ###BEGIN INIT INFO
  59. # perl-fcgi This shell script takes care of starting and stopping perl-fcgi
  60. #
  61. # chkconfig: 2345 80 30
  62. # description: perlfcgi
  63. # processname: perlfcgi
  64. # pidfile: /usr/local/nginx/perl-fcgi.pid
  65. # description: start and stop perl_cgi for nginx
  66. # name:feiyang
  67. ###END INIT INFO
  68. #set -x
  69. PERL=/usr/bin/perl
  70. KILLALL=/usr/bin/killall
  71. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  72. if [[ $# != 1 ]];then
  73. echo "usage $0 start|stop|restart"
  74. exit 1
  75. fi
  76. dir=/usr/local/nginx
  77. stop ()
  78. {
  79. #pkill -f $dir/perl-fcgi.pl
  80. kill $(cat $dir/logs/perl-fcgi.pid)
  81. rm -f /usr/local/nginx/logs/perl-fcgi.pid 2>/dev/null
  82. rm -f /usr/local/nginx/logs/perl-fcgi.sock 2>/dev/null
  83. echo "stop perl-fcgi done"
  84. }
  85. start ()
  86. {
  87. kill $(cat $dir/logs/perl-fcgi.pid)
  88. rm -f /usr/local/nginx/now_start_perl_fcgi.sh 2>/dev/null
  89. rm -f /usr/local/nginx/logs/perl-fcgi.pid 2>/dev/null
  90. rm -f /usr/local/nginx/logs/perl-fcgi.sock 2>/dev/null
  91. chown nobody.nobody /usr/local/nginx/logs
  92. echo "/usr/bin/perl /usr/local/nginx/perl-fcgi.pl -l /usr/local/nginx/logs/perl-fcgi.log -pid /usr/local/nginx/logs/perl-fcgi.pid -S /usr/local/nginx/logs/perl-fcgi.sock" >>/usr/local/nginx/now_start_perl_fcgi.sh
  93. chown nobody.nobody /usr/local/nginx/now_start_perl_fcgi.sh
  94. chmod u+x /usr/local/nginx/now_start_perl_fcgi.sh
  95. sudo -u nobody -s "/usr/local/nginx/now_start_perl_fcgi.sh"
  96. echo "start perl-fcgi done"
  97. }
  98. case $1 in
  99. stop)
  100. stop
  101. ;;
  102. start)
  103. start
  104. ;;
  105. restart)
  106. stop
  107. start
  108. ;;
  109. esac

  110. 2.添加系统服务
  111. ~]# chkconfig --add perlcgi
  112. ~]# chkconfig perlcgi on
  113. ~]# chkconfig --list | grep perlcgi
  114. perlcgi 0:off 1:off 2:on 3:on 4:on 5:on 6:off

  115. ~]# find /etc/rc.d -name '*perl*' -print
  116. /etc/rc.d/init.d/perlcgi
  117. /etc/rc.d/rc6.d/K30perlcgi
  118. /etc/rc.d/rc3.d/S80perlcgi
  119. /etc/rc.d/rc2.d/S80perlcgi
  120. /etc/rc.d/rc4.d/S80perlcgi
  121. /etc/rc.d/rc5.d/S80perlcgi
  122. /etc/rc.d/rc0.d/K30perlcgi
  123. /etc/rc.d/rc1.d/K30perlcgi

  124.  #] service perlcgi start
  125.    
  126. 没用问题

  127. 开机启动 不成功
  128.     但是开机启动是perl-cgi.pid 会存在 然后你启动失败,在启动之前rm -f 删除pid文件,不加-f 不能删掉,这点要记住,但是失败。

  129.       开始第三种

  130. 1.打开脚本调试信息 ‘set -x’
  131.     
  132. 选项 说明
  133.  -a 自动向已经修改的变量或为导出后序命令的变量作出标志
  134.  -b 不是在原提示符之前,而是立即引发终止后台任务的状态表表
  135.  -e 如果命令带非零值返回,立即退出
  136. -f 禁止带扩展名的路径
  137. -h 定义函数时,定位和存储函数命令,当函数被执行时,通常查询 函数命令
  138. -k 所有的关键词参数,而不只是那些命令名前的关键词参数,被放 在环境命令中
  139. -m 监视器模式,启动任务控制.此选项默认支持系统shell交互.后 台进程以单独的进程组运行,在每次完成任务时显示包含退出的 状态行
  140. -n 读取命令但不执行命令.通常监查shell脚本的句法错误.交互 shell被忽略
  141. -o option-name 选项名可以是下列之一: 选
  142. 项 说明
  143. allexport 同-a选项 braceexpand shell执行花括号扩展,在默认情况下起作用
  144. emacs 使用emacs风格命令行编辑接口.除非shell以-noline-editing 选项启动,否则当shell交互时,通过默认启动该选项
  145. errexit 同-e选项
  146. histexpand 同-H选项
  147. ignoreeof 其结果是好像shell命令IGNOREEOF=10被执行 interactive 允许单词前带#号,以使得在交互shell中忽略命令行的全部字符
  148. -commands monitor 同-m选项 noclobber 同-C选项
  149. noexec 同-n选项 noglob 同-f选项 nohash 同-d
  150. 选项 notify 同-b选项 nounset 同-u选项
  151. physical 同-p选项 posix 改变BASH属性以匹配标准,默认操作不同于POSIX1003.2标准
  152. verbose 同-v选项 vi 使用vi风格的命令行编辑器
  153. XTRACE 同-x选项,如果没有给出选项名,显示当前选项值
  154. -p 打开特权模式(在此模式,$ENV文件被处理,不能从环境中继承 shell函数.如果是有效用户ID而不是实用户组则自动启动.关闭 此选项将使得有效用户和组IDs设置实用户和组IDs)
  155. -t 在读取命令并执行之后退出 -u 当执行参数括展时,把非设置变量作为错误处理(如果扩展企图 出现在非设置变量中,shell显示错误信息.如果不是交互式,则 带非凌值退出)
  156. -v 输入行被读取时,显示shell输入行
  157.  -x 在每个简单命令被扩展之后,显示PS4扩展值,之后是要执行的命令
  158. -l 保存和恢复绑定在命令中的名称
  159. -d 禁止执行查找散列命令(通常,命令被保存在散列表中,一旦被找到 就不再继续查找)
  160.  -C 效果好像是执行了noclobber=shell命令
  161. -H 使用!风格的历史替代(当shell交互时,在默认情况下,此选项有效)
  162. -P 如果设置此参数,当执行改变目录命令cd时,不遵循符号链接,而是 使用实际的目录
  163.  -- 如果在选项后没有参数,不设置位置参数.否则,即使一些参数以a 选项开始,也要把位置参数设置为argument - 结束选项的信号,将引发其余的参数被赋值到位置参数中(-x和-v 选项被关闭.如果没有argument,位置参数将保留未改变的参数)

  164.    然后在rc.local 编辑如下
  165. /usr/local/nginx/sbin/start_perl_cgi.sh stop 2> /home/aa
  166. /usr/local/nginx/sbin/start_perl_cgi.sh start 2> /home/bb
  167.   在bb中看到如下

  168.     发现sudo 错误
  169.  

  170.  将上面那个Defaults requiretty注释掉就ok了。
  171.    说明sudo 切换用户时是需要终端,或另一种是将sudo改为su 一切都ok。
  172.  
  173. 最后我采用了系统服务启动的形式

  174. ~]# cat /etc/init.d/perlcgi
  175. #!/bin/bash
  176. ###BEGIN INIT INFO
  177. # perl-fcgi This shell script takes care of starting and stopping perl-fcgi
  178. #
  179. # chkconfig: 2345 80 30
  180. # description: perlfcgi
  181. # processname: perlfcgi
  182. # pidfile: /usr/local/nginx/perl-fcgi.pid
  183. # description: start and stop perl_cgi for nginx
  184. # name:feiyang
  185. ###END INIT INFO
  186. set -x
  187. if [[ $# != 1 ]];then
  188. echo "usage $0 start|stop|restart"
  189. exit 1
  190. fi
  191. dir=/usr/local/nginx
  192. stop ()
  193. {
  194. #pkill -f $dir/perl-fcgi.pl
  195. kill $(cat $dir/logs/perl-fcgi.pid)
  196. rm -f /usr/local/nginx/logs/perl-fcgi.pid 2>/dev/null
  197. rm -f /usr/local/nginx/logs/perl-fcgi.sock 2>/dev/null
  198. echo "stop perl-fcgi done"
  199. }
  200. start ()
  201. {
  202. rm -f /usr/local/nginx/now_start_perl_fcgi.sh 2>/dev/null
  203. chown nobody.nobody /usr/local/nginx/logs
  204. echo "/usr/bin/perl /usr/local/nginx/perl-fcgi.pl -l /usr/local/nginx/logs/perl-fcgi.log -pid /usr/local/nginx/logs/perl-fcgi.pid -S /usr/local/nginx/logs/perl-fcgi.sock" >>/usr/local/nginx/now_start_perl_fcgi.sh
  205. chown nobody.nobody /usr/local/nginx/now_start_perl_fcgi.sh
  206. chmod u+x /usr/local/nginx/now_start_perl_fcgi.sh
  207. su - nobody -c '/usr/local/nginx/now_start_perl_fcgi.sh'
  208. echo "start perl-fcgi done"
  209. }
  210. case $1 in
  211. stop)
  212. stop
  213. ;;
  214. start)
  215. start
  216. ;;
  217. restart)
  218. stop
  219. start
  220. ;;
  221. esac

  222. 然后一切ok了。

  223. 假如还不行的话,我给你一个脚本试试,哈哈 很简单的哦

  224. #!/bin/bash
  225. sleep 5
  226. proce=`ps -ef | grep perl-fcgi.pl | grep -v grep`
  227. num=`echo $?`
  228. if [ $num != 0 ];then
  229. /usr/local/nginx/sbin/start_perl_cgi.sh stop
  230. /usr/local/nginx/sbin/start_perl_cgi.sh start
  231. else

  232. fi
  233. 让后将脚本放到rc.local中
阅读(1972) | 评论(0) | 转发(0) |
0

上一篇:perl 利用函数将获取主机名转换为ip

下一篇:没有了

给主人留下些什么吧!~~