I need to run a console script as a service under Centos 7. I’ve created my service unit as follows:
[Unit]
Description=My Useful Service
Requires=mysqld.service
After=mysqld.service
[Service]
User=root
Type=forking
TimeoutSec=0
PIDFile=/var/run/my-useful.pid
KillMode=process
ExecStart=/var/www/vhosts/usefulservice.com/framework/launchme.sh
Restart=no
RestartSec=40s
[Install]
WantedBy=default.target
The shell script [size="2"]/var/www/vhosts/usefulservice.com/framework/launchme.sh contains:[/size]
[size="2"]
#!/usr/bin/env bash
BASEDIR=$(dirname $0)
${BASEDIR}/yii useful
[/size]
The Problem:
-
When the script is launched by systemd, it won’t connect to MySQL (Exception: SQLSTATE[HY000] [2002] Connection refused)
-
When I manually launch the script from the shell (using the exact path as defined in [size="2"]ExecStart) it runs perfectly fine.[/size]
[size="2"]- If I add some dummy code without database connection, the script runs fine from both systemd and the shell[/size]
- I’ve replaced 127.0.0.1 with localhost in the MySQL DSN and the exception changes to: [size=“2”]Exception: SQLSTATE[HY000] [2003] No such file or directory.[/size]
[size=“2”]I’m lost here. I suspect there are some [/size]environmental[size=“2”] variables missing but I have no idea.[/size]
Any help will be really appreciated…