【问题标题】:Launch php script at apache2 startup在 apache2 启动时启动 php 脚本
【发布时间】:2021-02-12 19:23:07
【问题描述】:

我正在尝试运行一个 php 脚本来在服务器崩溃、重新启动或 smth 后恢复状态。 因为 php 脚本需要数据库才能运行,所以我首先尝试通过在 init.d 中创建一个文件来运行它,但没有成功,它只是在需要时启动。 所以现在我认为这是在 apache2 启动时运行脚本的最简单方法,如描述的here。 所以目前我在/etc/init.d/apache2 中添加了php -q /var/www/scripts/testing.php & ;;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
}

但是因为这根本不起作用,所以我还将这个 php 执行添加到链接中提到的restart) 部分。这看起来像这样:

 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 脚本如下所示:

<?php
file_put_contents('/var/www/html/log', "301,$timestamp,Recreating all connections after restart,N/A\n",FILE_APPEND);
?>

因为我希望它尽可能简单,但日志文件仍然是空的。我对解决我的问题的任何想法持开放态度。

p.s.:我已经尝试通过 /etc/systemd/system/ 中的服务来执行此操作,但由于我正在启动一个应该是持久的连接,因此我必须使用 screennohupdisown。我已经尝试了这三个,但这些都不起作用,他们只是没有启动脚本。 (当时是 bash,我切换到 php 以便能够从 apache2 文件运行它)

【问题讨论】:

  • 语法是用于 php 脚本还是用于执行 php 的调用脚本?在命令后怀疑右圆括号是有效的 php 语法

标签: php apache debian apache2 boot


【解决方案1】:

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

只需将 shell 脚本 callmyphp 放入 /etc/init.d 中,它会调用 php 解释器并将您的 php 脚本作为参数传递,例如:

#!/bin/sh
/usr/bin/php -q /path/to/myphp.php

不要忘记使用chmod 755 /etc/init.d/callmyphp 使您的调用脚本可执行。

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

另见https://debian-administration.org/article/28/Making_scripts_run_at_boot_time_with_Debian

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-03
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-10-25
    • 1970-01-01
    相关资源
    最近更新 更多