在apache2启动时启动php脚本

在apache2启动时启动php脚本,php,apache,debian,apache2,boot,Php,Apache,Debian,Apache2,Boot,我试图运行一个php脚本来恢复服务器崩溃、重启或smth后的状态。 因为php脚本需要数据库来运行,所以我首先尝试在init.d中创建一个文件来运行它,但没有成功,它只是在需要时启动。 所以现在我认为这是在apache2初创公司上运行脚本的最简单的方法。 因此,目前我添加了php-q/var/www/scripts/testing.php&到/etc/init.d/apache2中的do_start(),如下所示: do_start() { # Return #

我试图运行一个php脚本来恢复服务器崩溃、重启或smth后的状态。 因为php脚本需要数据库来运行,所以我首先尝试在init.d中创建一个文件来运行它,但没有成功,它只是在需要时启动。 所以现在我认为这是在apache2初创公司上运行脚本的最简单的方法。 因此,目前我添加了
php-q/var/www/scripts/testing.php&
/etc/init.d/apache2
中的
do_start()
,如下所示:

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
                return 1
        fi

        if apache_conftest ; then
                $APACHE2CTL start
                php -q /var/www/scripts/testing.php &
                ;;
                apache_wait_start $?
                return $?
        else
                APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed."
                return 2
        fi
}
 restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop stop
        case "$?" in
                0|1)
                        do_start
                        case "$?" in
                                0)
                                        log_end_msg 0
                                        ;;
                                1|*)
                                        log_end_msg 1 # Old process is still or failed to running
                                        print_error_msg
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        # Failed to stop
                        log_end_msg 1
                        print_error_msg
                        exit 1
                        ;;
        php -q /var/www/scripts/testing.php &
        ;;
        esac
        ;;
<?php
file_put_contents('/var/www/html/log', "301,$timestamp,Recreating all connections after restart,N/A\n",FILE_APPEND);
?>
但是因为这根本不起作用,我还将这个php执行添加到了链接中提到的
restart)
部分。这看起来像这样:

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
                return 1
        fi

        if apache_conftest ; then
                $APACHE2CTL start
                php -q /var/www/scripts/testing.php &
                ;;
                apache_wait_start $?
                return $?
        else
                APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed."
                return 2
        fi
}
 restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop stop
        case "$?" in
                0|1)
                        do_start
                        case "$?" in
                                0)
                                        log_end_msg 0
                                        ;;
                                1|*)
                                        log_end_msg 1 # Old process is still or failed to running
                                        print_error_msg
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        # Failed to stop
                        log_end_msg 1
                        print_error_msg
                        exit 1
                        ;;
        php -q /var/www/scripts/testing.php &
        ;;
        esac
        ;;
<?php
file_put_contents('/var/www/html/log', "301,$timestamp,Recreating all connections after restart,N/A\n",FILE_APPEND);
?>
但脚本仍然没有运行。php脚本如下所示:

do_start()
{
        # Return
        #   0 if daemon has been started
        #   1 if daemon was already running
        #   2 if daemon could not be started

        if pidofproc -p $PIDFILE "$DAEMON" > /dev/null 2>&1 ; then
                return 1
        fi

        if apache_conftest ; then
                $APACHE2CTL start
                php -q /var/www/scripts/testing.php &
                ;;
                apache_wait_start $?
                return $?
        else
                APACHE2_INIT_MESSAGE="The apache2$DIR_SUFFIX configtest failed."
                return 2
        fi
}
 restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
        do_stop stop
        case "$?" in
                0|1)
                        do_start
                        case "$?" in
                                0)
                                        log_end_msg 0
                                        ;;
                                1|*)
                                        log_end_msg 1 # Old process is still or failed to running
                                        print_error_msg
                                        exit 1
                                        ;;
                        esac
                        ;;
                *)
                        # Failed to stop
                        log_end_msg 1
                        print_error_msg
                        exit 1
                        ;;
        php -q /var/www/scripts/testing.php &
        ;;
        esac
        ;;
<?php
file_put_contents('/var/www/html/log', "301,$timestamp,Recreating all connections after restart,N/A\n",FILE_APPEND);
?>

因为我希望它尽可能简单,但是日志文件仍然是空的。我愿意接受任何解决我问题的想法


p、 美国:我已经尝试通过
/etc/systemd/system/
中的服务来实现这一点,但由于我正在启动一个应该是持久的连接,我必须使用
屏幕
nohup
断开
。我试过这三个,但都不管用,他们只是没有启动脚本。(当时是bash,为了能够从apache2文件运行它,我切换到php)

您不应该使用apache启动脚本,而是遵循您的第一个想法,使用自己的init脚本,除非您的php脚本依赖于apache的存在

只需在/etc/init.d中放置一个shell脚本
callmyphp
,该脚本调用php解释器并将php脚本作为参数传递,如下所示:

#!/bin/sh
/usr/bin/php -q /path/to/myphp.php
不要忘记使用
chmod 755/etc/init.d/callmyphp
使调用脚本成为executabel

然后通过符号链接将调用脚本添加到所需的运行级别,即运行
update rc.d callmyphp defaults


另请参见

语法是针对php脚本还是执行php的调用脚本?怀疑命令后的右括号是否为有效的php语法