我试图在启动centos7时运行phpscript。当前systemd进程如下所示
[Unit]
Description=custom Service
After=network.target
[Service]
Type=forking
User=root
ExecStart=/usr/bin/php /var/www/htdocs/mysite/public/index.php abc xyz >> /var/log/custom.log 2>&1
[Install]
WantedBy=multi-user.target
但是上面的脚本没有传递参数。我该如何解决这个问题?谢谢!
作为替代方案,我创建了一个myphp.sh
bash脚本
#!/bin/bash
nohup /usr/bin/php /var/www/htdocs/mysite/public/index.php abc xyz & >> /var/log/custom.log 2>&1
,然后在systemd脚本
[Unit]
Description=custom Service
After=network.target
[Service]
Type=forking
User=root
ExecStart=/etc/init.d/myphp.sh
[Install]
WantedBy=multi-user.target
试试这个配置
[Service]
Type=forking
User=root
PHP_PARAM_1=abc
PHP_PARAM_2=xyz
ExecStart=/usr/bin/php /var/www/htdocs/mysite/public/index.php $PHP_PARAM_1 $PHP_PARAM_2>> /var/log/custom.log 2>&1
[Service]
Type=forking
User=root
Environment="abc xyz"
ExecStart=/usr/bin/php /var/www/htdocs/mysite/public/index.php $PHP_PARAM_1 $PHP_PARAM_2>> /var/log/custom.log 2>&1
这只是一种预感,但我认为ExecStart
选项(/usr/bin/php ...
)中的前缀混淆了参数顺序,这就是为什么你不能正确使用这些参数。我想你可以通过在php脚本中使用shebang来缓解这个问题:
#!/usr/bin/php
<?php
// YOUR PHP CODE HERE
您还需要为文件添加执行权限。这样,您就可以像使用任何其他shell脚本一样使用php脚本,因此您可以简单地从ExecStart
参数中省略前缀部分:
ExecStart=/var/www/htdocs/mysite/public/index.php $PHP_PARAM_1 $PHP_PARAM_2 >> /var/log/custom.log 2>&1