cuteboyucsc
cuteboyucsc

Reputation: 91

Run php script as systemd service in centos7

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!

Upvotes: 4

Views: 9940

Answers (3)

Gergely Lukacsy
Gergely Lukacsy

Reputation: 3074

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 

Upvotes: 0

cuteboyucsc
cuteboyucsc

Reputation: 91

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

Upvotes: 5

Halayem Anis
Halayem Anis

Reputation: 7805

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 

Upvotes: 1

Related Questions