我想在停止或重新启动 systemd 服务之前将已加载的 apache 模块列表写入文件中的磁盘。
所以我在apache2.service中添加了
ExecStop=/usr/sbin/apachectl -M > /home/ciasto/services/logs/apache2/loaded_mods
但这会在停止时引发错误
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: failed (Result: exit-code) since Tue 2020-08-25 16:20:37 United; 3min 8s ago
Process: 7269 ExecStop=/usr/sbin/apachectl -M > /home/ciasto/services/logs/apache2/loaded_mods (code=exited, status=1/FAILURE)
Main PID: 6245 (code=exited, status=0/SUCCESS)
Aug 25 16:20:36 rockpi apachectl[7269]: -M : a synonym for -t -D DUMP_MODULES
Aug 25 16:20:36 rockpi apachectl[7269]: -t -D DUMP_INCLUDES: show all included configuration files
Aug 25 16:20:36 rockpi apachectl[7269]: -t : run syntax check for config files
Aug 25 16:20:36 rockpi apachectl[7269]: -T : start without DocumentRoot(s) check
Aug 25 16:20:36 rockpi apachectl[7269]: -X : debug mode (only one worker, do not detach)
Aug 25 16:20:36 rockpi apachectl[7269]: Action '-M > /home/ciasto/services/logs/apache2/loaded_mods' failed.
Aug 25 16:20:36 rockpi apachectl[7269]: The Apache error log may have more information.
Aug 25 16:20:36 rockpi systemd[1]: apache2.service: Control process exited, code=exited status=1
Aug 25 16:20:37 rockpi systemd[1]: apache2.service: Failed with result 'exit-code'.
Aug 25 16:20:37 rockpi systemd[1]: Stopped The Apache HTTP Server.
答案1
您似乎遇到了使用错误。 apachectl
正在响应,就好像您将其称为:
/usr/sbin/apachectl '-M > /home/ciasto/services/logs/apache2/loaded_mods'
您可以看到参数是:-M
, >
,/home/...
该ExecStop=
线路不支持sh
也不支持某些sh
运算符,例如&
,>
或|
。如果您想使用这些运算符直接调用 shell 并将命令作为参数传递:
ExecStop=+/bin/sh -c '/usr/sbin/apachectl -M > /home/ciasto/services/logs/apache2/loaded_mods'
正如您在评论中注意到的,您的服务已经User=www-data
。 www-data
可能没有 中事物的执行权限sbin
,或 中的写入权限/home/ciasto/*
。因此,您可能需要以 root 权限运行此命令(并且仅此命令),同时让其他行仍以www-data
.使用ExecStop=+/bin/sh
而不是ExecStop=/bin/sh
为你做到这一点。
一个小演示:
User=www-data
ExecStop=/usr/bin/whoami
ExecStop=+/usr/bin/whoami
将在日记中写下以下内容:
www-data
root