我在/var/www/html/dbsync/index.php中有我的php脚本文件.当cd / var / www / html / dbsync /并运行php index.php它完美无缺.
我想通过sh文件调用PHP文件,SH文件的位置如下
/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时,它也可以完美地运行.
现在,如果我按如下方式设置crontab:
1 * * * * /var/www/html/dbsync/dbsync.sh /var/www/html/dbsync
但是,此crontab未按预期工作.
有什么不对?
解决方法:
如注释中所示,问题在于您没有定义应该使用哪个程序来执行脚本.考虑到cronjob是在一个微小的环境中执行的;在那里,可以假设不多.这就是我们定义完整路径等的原因.
所以你需要说:
1 * * * * /bin/sh /var/www/html/dbsync/dbsync.sh /var/www/html/dbsync
# ^^^^^^^
/ bin / sh是您要用于执行脚本的二进制文件.
否则,您可以设置脚本的执行权限并添加一个shell-script header,告诉它使用哪个解释器:
#!/bin/sh
如果这样做,则无需添加二进制文件的路径.
从Troubleshooting common issues with cron jobs开始:
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 * * * *将不会. Intead,它将运行at every 1st minute past every hour.所以如果你想每分钟运行它,比如* * * * *.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。