的cronjob不执行一个脚本,工作正常独立 [英] cronjob does not execute a script that works fine standalone
问题描述
我在 /var/www/html/dbsync/index.php
我的PHP脚本文件。当 CD的/ var / www / html等/ dbsync /
并运行 PHP的index.php
它完美的作品。
I have my php script file in /var/www/html/dbsync/index.php
. When cd /var/www/html/dbsync/
and run php index.php
it works perfectly.
我想通过sh的文件来调用PHP文件,SH文件的位置是如下
I want to call PHP file through sh file, the location of SH file is as below
/var/www/html/dbsync/dbsync.sh
这是 dbsync.sh
文件的内容:
/usr/bin/php /var/www/html/dbsync/index.php >> /var/www/html/dbsync/myscript.log 2>&1 -q -f
当我 CD的/ var / www / html等/ dbsync /
并运行 ./ dbsync.sh
它的工作原理完美的为好。
When I cd /var/www/html/dbsync/
and run ./dbsync.sh
it works perfectly as well.
现在,如果我设置了如下的crontab:
Now if I set up crontab as below:
1 * * * * /var/www/html/dbsync/dbsync.sh /var/www/html/dbsync
不过,这的crontab未正常工作。
However, this crontab is not working as expected.
什么可能是错的?
推荐答案
作为评论看到,问题是,你是不是定义应该使用什么程序来执行脚本。考虑到,一个cronjob是在一个很小的环境中执行账户;在那里,没有太多可以假设。这就是为什么我们定义完整路径,等等。
As seen in comments, the problem is that you are not defining what program should be used to execute the script. Take into account that a cronjob is executed in a tiny environment; there, not much can be assumed. This is why we define full paths, etc.
所以,你需要这样说:
1 * * * * /bin/sh /var/www/html/dbsync/dbsync.sh /var/www/html/dbsync
# ^^^^^^^
/ bin / sh的
是您要用来执行脚本二进制文件。
/bin/sh
being the binary you want to use to execute the script.
否则,您可以设置执行权限的脚本,并添加 shell脚本头告诉它什么际preTER使用方法:
Otherwise, you can set execution permissions to the script and add a shell-script header telling it what interpreter to use:
#!/bin/sh
如果你这样做,加入二进制文件的路径是没有必要的。
If you do this, adding the path of the binary is not necessary.
使用相对路径。如果你的cron作业正在执行一些脚本
样,你一定要确保使用脚本中唯一的绝对路径。
例如,如果你的脚本位于/path/to/script.phpand
你试图打开一个在同一目录下名为file.php文件,
你不能使用相对路径例如fopen(file.php)。该文件必须
从它的绝对路径调用,像这样:FOPEN(/path/to/file.php)。
这是因为cron作业不一定从目录中运行
该脚本的位置,以便所有路径必须专门调用。
Using relative paths. If your cron job is executing a script of some kind, you must be sure to use only absolute paths inside that script. For example, if your script is located at /path/to/script.phpand you're trying to open a file called file.php in the same directory, you cannot use a relative path such as fopen(file.php). The file must be called from its absolute path, like this: fopen(/path/to/file.php). This is because cron jobs do not necessarily run from the directory in which the script is located, so all paths must be called specifically.
另外,我知道你想运行此的每一分钟。如果是这样, 1 * * * *
不会做。这一翻译,它会在每个第一分钟过去每隔一小时运行 。所以,如果你想运行的每一分钟,说 * * * * *
。
Also, I understand you want to run this every minute. If so, 1 * * * *
won't do. Intead, it will run at every 1st minute past every hour. So if you want to run it every minute, say * * * * *
.
这篇关于的cronjob不执行一个脚本,工作正常独立的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!