系统相关
首页 > 系统相关> > linux-挂init.d脚本

linux-挂init.d脚本

作者:互联网

我有一个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

然后我有一个keepalive cronjob,如果pid下降,则调用此命令.问题在于,无论何时我像cron作业一样运行keepalive脚本(例如,运行部件/ var / www / html / private / fivemin),该脚本都会挂起(keepalive脚本位于/ var / www / html / private / fivemin中) .

我的init.d脚本中缺少一些时髦的东西吗?

我已经花了好几个小时思考这个问题了!我在centos4 btw上.

谢谢你的帮助.
-埃里克

编辑:

keepalive / cronjob脚本已简化为测试:

#!/usr/bin/php
<?

exec("/etc/init.d/swapi start");

?>

奇怪的是,这与正常的cron输出一样,将swapi.php的错误输出放到/ var / spool / mail中,除了我将所有输出都转储到init.d脚本中的swapi.log中?

当我从cli(作为/的根目录)运行keepalive.php时,它的运行完全符合我的预期.

当keepalive运行ps aux | 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

如果我这样做:

/etc/init.d/swapi stop

从cli开始,则不再列出这两个程序.

Swapi ls -l看起来像:

-rwxr-xr-x  1 5500 5500 33148 Aug 29 15:07 swapi.php

这是crontab的样子:

*/5 * * * * root run-parts /var/www/html/private/fivemin

这是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){

我修改了init.d脚本,并将init放在变量声明的上方,这没有什么不同.

解决方法:

答案是bash保持打开状态,因为我的init.d脚本未重定向stderr输出.我现在将其更改为

$LDIR/$EXEC &> $LDIR/swapi.log & MYPID=$!

现在,它可以完美运行.

感谢大家的帮助!

标签:init-d,bash,centos,linux
来源: https://codeday.me/bug/20191023/1916194.html