我在/var/www/html/dbsync/index.PHP中有我的PHP脚本文件.当cd / var / www / html / dbsync /并运行PHP index.PHP它完美无缺.
/var/www/html/dbsync/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] 举报,一经查实,本站将立刻删除。