4

I 'm trying to run a phpscript on startup of centos7. Currently systemd process looks like below

[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

But above script is not passing arguments. How could I fix the issue ? Thanks!

cuteboyucsc
  • 91
  • 2
  • 7

3 Answers3

5

As an alternative, I've created a myphp.sh bash script

#!/bin/bash
nohup /usr/bin/php /var/www/htdocs/mysite/public/index.php abc xyz & >> /var/log/custom.log 2>&1

and then in systemd script

[Unit]
Description=custom Service
After=network.target

[Service]
Type=forking
User=root
ExecStart=/etc/init.d/myphp.sh

[Install]
WantedBy=multi-user.target
tvl
  • 3,868
  • 2
  • 16
  • 35
cuteboyucsc
  • 91
  • 2
  • 7
1

Give a try with this configuration

[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 

UPDATE

[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 
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
  • 3
    `ExecStart` does not support shell features like redirection. It's what would be passed to `execve()`, which is just a path to a program and a list of arguments, separated by spaces. That's why this doesn't work. – siride Apr 25 '18 at 15:28
0

This is but a hunch, but I think the prefix in the ExecStart option (/usr/bin/php ...) is messing up the argument ordering and that's why you cant use those args properly. I suspect you can mitigate this issue by using a shebang in your php script:

#!/usr/bin/php
<?php

// YOUR PHP CODE HERE

You also need to add exec rights to the files. This way, you can use the php script just like any other shell script, so you can just simply omit the prefix part from your ExecStart parameter:

ExecStart=/var/www/htdocs/mysite/public/index.php $PHP_PARAM_1 $PHP_PARAM_2 >> /var/log/custom.log 2>&1 
Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28