加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – cronjob不会执行一个独立运行的脚本

发布时间:2020-12-14 00:32:03 所属栏目:Linux 来源:网络整理
导读:我在/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/
我在/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.所以如果你想每分钟运行它,比如* * * * *.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读