【发布时间】: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/ 中的服务来执行此操作,但由于我正在启动一个应该是持久的连接,因此我必须使用 screen、nohup 或 disown。我已经尝试了这三个,但这些都不起作用,他们只是没有启动脚本。 (当时是 bash,我切换到 php 以便能够从 apache2 文件运行它)
【问题讨论】:
-
语法是用于 php 脚本还是用于执行 php 的调用脚本?在命令后怀疑右圆括号是有效的 php 语法
标签: php apache debian apache2 boot