简体   繁体   English

PHP 脚本未通过 cron 作业运行

[英]PHP Script is not running via cron job

I have a php script that queries a MySQL and IBM Informix database (located in other host), generates json files, handles the information and inserts it into the MySQL database. I have a php script that queries a MySQL and IBM Informix database (located in other host), generates json files, handles the information and inserts it into the MySQL database.

The script has a main file and another that has the query handling functions.该脚本有一个主文件和另一个具有查询处理功能的文件。 Staying like this:保持这样:

/opt/project
     script.php
     functions.php

The script.php requires the functions.php file, generate json where is being executed based on queries to databaes, and inserts the data handled.脚本.php需要functions.php文件,根据对数据库的查询生成正在执行的json,并插入处理的数据。

I can run the script smoothly using absolute or relative path.我可以使用绝对或相对路径顺利运行脚本。

Inside /opt/project: /opt/project 内部:

# php script.php

Somewhere else:别的地方:

 # /usr/bin/php /opt/project/scrpt.php

However, when its executed by cron job, doesn't work.但是,当它由 cron 作业执行时,它不起作用。 I did already set up informing environment variables, performed log tests, and even created a shell script to run script.php with cron running the shell script.我确实已经设置了通知环境变量,执行了日志测试,甚至创建了一个 shell 脚本来运行 script.php,cron 运行 shell 脚本。

Server PATH (CentOS 7): /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin服务器路径(CentOS 7):/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin

Crontab contents tried: crontab 内容试过:

SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
* * * * * /usr/bin/php /opt/project/script.php

With root ahead command:使用 root 提前命令:

SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
* * * * * root /usr/bin/php /opt/project/script.php

Changing directory:更改目录:

SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
* * * * * cd /opt/project && /usr/bin/php script.php

Cron to run shell script instead php directly: Cron 直接运行 shell 脚本而不是 php:

SHELL=/usr/local/bin   
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
* * * * * cd /opt/project && sh run_script.sh

Where shell script has the following content:其中shell脚本有以下内容:

#!/usr/bin/env bash

cd /opt/project

PHP=`which php`
$PHP script.php

The cron logs didn't show anything wrong, and if I redirect the output to a file it's create the file, but didn't run the php script, even by shell script to do that. cron 日志没有显示任何错误,如果我将 output 重定向到一个文件,它会创建该文件,但没有运行 php 脚本,即使是通过 Z2591C98B70119FE624898B1E4ZB 脚本执行该脚本。 I tried with not set SHELL and PATH in the crontab, but doesn't work too.我尝试在 crontab 中未设置 SHELL 和 PATH ,但也不起作用。

Please try:请试试:

* * * * * /full/path/to/your/run_script.sh

instead:反而:

* * * * * cd /opt/project && sh run_script.sh

if you want your bash or sh script to be executed the path to the script should be absolute path as above or:如果您希望执行 bash 或 sh 脚本,则脚本的路径应为上述绝对路径,或者:

./run_script.sh

note the ./ .注意./ otherwise the script will not be executed.否则脚本将不会被执行。

also make your bash script exectuable by:还可以通过以下方式使您的 bash 脚本可执行:

chmod +x run_script.sh

you may also run the script directly without run_script.sh from cron:您也可以直接运行脚本,而无需来自 cron 的run_script.sh

* * * * * /absolute/path/to/php -f "/absolute/path/to/script.php"

for changing current directory to the one that your php script sits in you may use this at the beginning of your php script:要将当前目录更改为 php 脚本所在的目录,您可以在 php 脚本的开头使用它:

chdir(dirname(__FILE__));

because when you run php script by cron then the current dir for php is not the script but different (due to the fact it was started by cron)因为当您通过 cron 运行 php 脚本时,php 的当前目录不是脚本而是不同的(因为它是由 cron 启动的)

The problem was the variables of the SDK to connect with IBM Infomix database.问题是 SDK 的变量与 IBM Infomix 数据库连接。 Although I had set the PATH, it was necessary to export the environment variables from IBM Informix that I define when I did install the Informix SDK.尽管我已经设置了 PATH,但必须从 IBM Informix 中导出我在安装 Informix SDK 时定义的环境变量。 The strange was that even with the PDO exeptions no one error was generate when I redirect error standard output with &>.奇怪的是,即使使用 PDO 异常,当我使用 &> 重定向错误标准 output 时也没有产生任何错误。

May exist an most elegant way to do that but I can't play with live environment.可能存在一种最优雅的方式来做到这一点,但我不能在现场环境中玩耍。 Follow that cron job that currently works:遵循当前有效的 cron 作业:

SHELL=/usr/local/bin
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/IBM/informix/bin
14,29,44,59 * * * * root export INFORMIXSERVER=<data_source_name> && export INFORMIXDIR=<path_to_informix_sdk> && export INFORMIXTMP=<path_infomix_tmp> && export INFORMIXSQLHOSTS=<path_to_sqlhost_file> && export $PATH:$INFORMIXDIR/bin && /usr/bin/php /opt/project/script.php &> /var/log/project/task.log

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM