[英]Init.d script hanging
I have an init.d script that looks like: 我有一个init.d脚本,看起来像:
#!/bin/bash
# chkconfig 345 85 60
# description: startup script for swapi
# processname: swapi
LDIR=/var/www/html/private/daemon
EXEC=swapi.php
PIDF=/var/run/swapi.pid
IEXE=/etc/init.d/swapi
### BEGIN INIT INFO
# Provides: swapi
# Required-Start: $local_fs
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: startup script for swapi
# Description: startup script for swapi.php which processes actionq into switch
### END INIT INFO
if [ ! -f $LDIR/$EXEC ]
then
echo "swapi was not found at $LDIR/$EXEC"
exit
fi
case "$1" in
start)
if [ -f $PIDF ]
then
echo "swapi is currently running. Killing running process..."
$IEXE stop
fi
$LDIR/$EXEC >> $LDIR/swapi.log & MYPID=$!
echo $MYPID > $PIDF
echo "swapi is now running."
;;
stop)
if [ -f $PIDF ]
then
echo "Stopping swapi."
PID_2=`cat $PIDF`
if [ ! -z "`ps -f -p $PID_2 | grep -v grep | grep 'swapi'`" ]
then
kill -9 $PID_2
fi
rm -f $PIDF
else
echo "swapi is not running, cannot stop it. Aborting now..."
fi
;;
force-reload|restart)
$0 stop
$0 start
;;
*)
echo "Use: /etc/init.d/swapi {start|stop|restart|force-reload}"
exit 1
esac
And then I have a keepalive cronjob that calls this if the pid goes down. 然后我有一个keepalive cronjob,如果pid下降,就会调用它。 Problem is that that keepalive script hangs whenever I run it like a cron job (ie. run-parts /var/www/html/private/fivemin), (the keepalive script being in /var/www/html/private/fivemin).
问题是每当我像cron作业一样运行keepalive脚本时就会挂起(即run-parts / var / www / html / private / fivemin),(keepalive脚本在/ var / www / html / private / fivemin中) 。
Is there something funky in my init.d script that I am missing? 在我的init.d脚本中是否有一些我缺少的东西?
I have been racking my brain on this problem for hours now! 我几个小时以来一直在讨论这个问题! I am on centos4 btw.
我顺便说一下。
Thanks for any help. 谢谢你的帮助。 -Eric
-Eric
EDIT: 编辑:
The keepalive/cronjob script was simplified for testing to a simple: keepalive / cronjob脚本被简化为测试简单:
#!/usr/bin/php
<?
exec("/etc/init.d/swapi start");
?>
The strange this is the error output from swapi.php is put into /var/spool/mail like normal cron output except that I have all the output being dumped into the swapi.log in the init.d script? 奇怪的是这是从swapi.php输出的错误被放入/ var / spool / mail中,就像普通的cron输出一样,除了我将所有输出都转储到init.d脚本中的swapi.log中?
When I run keepalive.php from the cli (as root from /) it operates exactly as I would expect it to. 当我从cli运行keepalive.php(作为/的root)时,它的操作完全符合我的预期。
When keepalive runs ps aux | 当keepalive运行ps aux | grep php looks like:
grep php看起来像:
root 4525 0.0 0.0 5416 584 ? S 15:10 0:00 awk -v progname=/var/www/html/private/fivemin/keepalive.php progname {????? print progname ":\n"????? progname="";???? }???? { print; }
root 4527 0.7 1.4 65184 14264 ? S 15:10 0:00 /usr/bin/php /var/www/html/private/daemon/swapi.php
And If I do a: 如果我这样做:
/etc/init.d/swapi stop
from the cli then both programs are no longer listed. 从cli开始,两个程序都不再列出。
Swapi ls -l looks like: Swapi ls -l看起来像:
-rwxr-xr-x 1 5500 5500 33148 Aug 29 15:07 swapi.php
Here is what the crontab looks like: 这是crontab的样子:
*/5 * * * * root run-parts /var/www/html/private/fivemin
Here is the first bit of swapi.php 这是swapi.php的第一个位
#!/usr/bin/php
<?
chdir(dirname( __FILE__ ));
include("../../config/db.php");
include("../../config/sql.php");
include("../../config/config.php");
include("config_local.php");
include("../../config/msg.php");
include("../../include/functions.php");
set_time_limit(0);
echo "starting @ ".date("Ymd.Gi")."...\n";
$actionstr = "";
while(TRUE){
I modified the init.d script and put init above the variable declarations, it did not make a difference. 我修改了init.d脚本并将init放在变量声明之上,它没有什么区别。
The answer was that bash was staying open because my init.d script was not redirecting the stderr output. 答案是bash保持打开状态,因为我的init.d脚本没有重定向stderr输出。 I have now changed it to
我现在把它改成了
$LDIR/$EXEC &> $LDIR/swapi.log & MYPID=$!
And it now functions perfectly. 它现在功能完美。
Thanks for the help everyone! 感谢大家的帮助!
When you run a command from cron
, the environment is not the same as when it is run from the bash command line after logging in. I would suspect in this case that the sh
is not able to understand swapi.php
as a PHP command. 当您从
cron
运行命令时,环境与登录后从bash命令行运行时的环境不同。我怀疑在这种情况下sh
不能将swapi.php
理解为PHP命令。
Do a 做一个
which php
to see where your php binary is and add this to your init.d script 查看php二进制文件的位置并将其添加到init.d脚本中
PHP=/usr/bin/php
...
$PHP $LDIR/$EXEC >> $LDIR/swapi.log & MYPID=$!
Probably not that important but you may want to redirect the output from the cron
line 可能不那么重要,但您可能想要重定向
cron
行的输出
0 * * * * /path/to/script 2>&1 >> /dev/null
for instance. 例如。
Make sure your script has the correct execution permissions, the right owner, and the first lines should look like this: 确保您的脚本具有正确的执行权限,正确的所有者,并且第一行应如下所示:
#!/usr/bin/php
<?php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.