使用ExecStartPost设置将Systemd配置为在httpd启动/重启后执行额外的脚本不起作用

贾斯汀·海兰德

每当httpd服务启动或重新启动时,我都需要执行一个PHP文件我发现Systemd的配置设置为** ExecStartPost,非常适合我需要做的事情。

我更新了/etc/systemd/system/multi-user.target.wants/httpd.service文件以反映以下内容:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecStartPost=/bin/php /usr/sbin/php_test
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

内容/ usr / sbin目录/ php_test为:

#!/bin/php

<?php
echo "Writing to /tmp/php-test.txt...\n";

$myfile = fopen("/tmp/php-test.txt", "w") or die("Unable to open file!");
$txt = "TEST! " . date("F j, Y, g:i a") . PHP_EOL;
fwrite($myfile, $txt);
fclose($myfile);

echo "Done!!\n";
?>

然后我chmod 777的php文件,并通过重新加载守护进程文件systemctl daemon-reload但是,当我重新启动httpd时,它没有创建我期望看到的/tmp/php-test.txt文件。

如果我/bin/php /usr/sbin/php_test通过命令行执行,则可以正常运行。

我找到了一个单独的StackOverflow线程,指出Systemd.service从下到上读取文件,因此我将ExecStartPost移到该行的右上方ExecStart,重新加载了守护程序文件,并再次重新启动了apache,但没有成功...

我在这里做错了什么?

谢谢!

更新1

我将httpd.service文件更改为以下内容:

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target
Documentation=man:httpd(8)
Documentation=man:apachectl(8)

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/httpd
ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND
ExecStartPost=/bin/bash -c "/bin/php -f /tmp/php_test"
ExecReload=/usr/sbin/httpd $OPTIONS -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
# We want systemd to give httpd some time to finish gracefully, but still want
# it to kill httpd after TimeoutStopSec if something went wrong during the
# graceful stop. Normally, Systemd sends SIGTERM signal right after the
# ExecStop, which would kill httpd. We are sending useless SIGCONT here to give
# httpd time to finish.
KillSignal=SIGCONT
PrivateTmp=true

[Install]
WantedBy=multi-user.target

现在,我得到一个错误(至少那要继续事情!)。当我通过查看日志时journalctl -xe,看到以下内容:

Apr 19 12:47:46 silo-stg-a01.cymedica.com systemd[1]: Starting The Apache HTTP Server...
-- Subject: Unit httpd.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit httpd.service has begun starting up.
Apr 19 12:47:46 silo-stg-a01.cymedica.com bash[13268]: Could not open input file: /tmp/php_test
Apr 19 12:47:46 silo-stg-a01.cymedica.com systemd[1]: httpd.service: control process exited, code=exited status=1
Apr 19 12:47:47 silo-stg-a01.cymedica.com systemd[1]: Failed to start The Apache HTTP Server.
-- Subject: Unit httpd.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit httpd.service has failed.
--
-- The result is failed.
Apr 19 12:47:47 silo-stg-a01.cymedica.com systemd[1]: Unit httpd.service entered failed state.
Apr 19 12:47:47 silo-stg-a01.cymedica.com systemd[1]: httpd.service failed.

错误是无法打开输入文件:/ tmp / php_test ..不确定那是什么意思。

而且我知道,即使PHP文件无法执行,在命令前面加上连字符也会使进程继续进行,但这不是我要解决的问题。我需要PHP脚本才能正确执行。

仅供参考,如果您想知道为什么我要执行/ bin / bash -c“ / bin / php -f / tmp / php_test”

而不仅仅是/ bin / php -f / tmp / php_test

我只是在尝试让它从bash命令执行php脚本。但是,如果我将其更改为just /bin/php -f /tmp/php_test,我将得到完全相同的错误journalctl

更新2

我注意到,如果我用ExecStartPostPHP命令将该替换为

ExecStartPost=/bin/logger "ExecStartPost 1"

ExecStart行的后面),它将ExecStartPost 1记录到日志中就好了...所以我认为它与php文件本身的执行方式有关

这里

您的单位文件中有:

PrivateTmp=true

这意味着systemd将为单元/tmp/var/tmp目录创建一个单独的命名空间删除该行以使用通常的方法/tmp

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章