如何设置 cron 来调用 php 脚本 - bash: */5: No such file or directory for setting cron job

spy*_*ggr 0 cron magento

我们正在尝试为在 ubuntu 服务器中运行的 magento 站点设置 cron

我们正在尝试以下命令:

*/5 * * * * php -f /var/www/html/sitename/cron.php
Run Code Online (Sandbox Code Playgroud)

但我们收到如下错误:

-bash: */5: No such file or directory
Run Code Online (Sandbox Code Playgroud)

更新 1

在此处输入图片说明

更新 2 - 错误

PHP Warning:  require(app/bootstrap.php): failed to open stream: No such file or directory in /var/www/html/sitename/cron.php on line 30

PHP Fatal error:  require(): Failed opening required 'app/bootstrap.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/sitename/cron.php on line 30
Run Code Online (Sandbox Code Playgroud)

定时文件

<?php


// Change current directory to the directory of current script
chdir(dirname(__FILE__));

require 'app/bootstrap.php';
require 'app/Mage.php';

if (!Mage::isInstalled()) {
    echo "Application is not installed yet, please complete install wizard first.";
    exit;
}

// Only for urls
// Don't remove this
$_SERVER['SCRIPT_NAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_NAME']);
$_SERVER['SCRIPT_FILENAME'] = str_replace(basename(__FILE__), 'index.php', $_SERVER['SCRIPT_FILENAME']);

Mage::app('admin')->setUseSessionInUrl(false);

umask(0);

$disabledFuncs = explode(',', ini_get('disable_functions'));
$isShellDisabled = is_array($disabledFuncs) ? in_array('shell_exec', $disabledFuncs) : true;
$isShellDisabled = (stripos(PHP_OS, 'win') === false) ? $isShellDisabled : true;
$isShellDisabled = true;

try {
    if (stripos(PHP_OS, 'win') === false) {
        $options = getopt('m::');
        if (isset($options['m'])) {
            if ($options['m'] == 'always') {
                $cronMode = 'always';
            } elseif ($options['m'] == 'default') {
                $cronMode = 'default';
            } else {
                Mage::throwException('Unrecognized cron mode was defined');
            }
        } else if (!$isShellDisabled) {
            $fileName = basename(__FILE__);
            $baseDir = dirname(__FILE__);
            shell_exec("/bin/sh $baseDir/cron.sh $fileName -mdefault 1 > /dev/null 2>&1 &");
            shell_exec("/bin/sh $baseDir/cron.sh $fileName -malways 1 > /dev/null 2>&1 &");
            exit;
        }
    }

    Mage::getConfig()->init()->loadEventObservers('crontab');
    Mage::app()->addEventArea('crontab');
    if ($isShellDisabled) {
        Mage::dispatchEvent('always');
        Mage::dispatchEvent('default');
    } else {
        Mage::dispatchEvent($cronMode);
    }
} catch (Exception $e) {
    Mage::printException($e);
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

duf*_*fte 5

关于 cron

在这里引用维基百科

软件实用程序 Cron 是类 Unix 计算机操作系统中基于时间的作业调度程序。设置和维护软件环境的人员使用 cron 来安排作业(命令或 shell 脚本)在固定的时间、日期或间隔定期运行。它通常使系统维护或管理自动化。

请查看此链接以获取有关 cron 的更多信息

查看 crontab

输出/查看当前用户的 crontab

crontab -l
Run Code Online (Sandbox Code Playgroud)

要查看另一个用户的 crontab,您需要 sudo 权限

sudo crontab -l -u nameOfOtherUser
Run Code Online (Sandbox Code Playgroud)

编辑 crontab

编辑您的用户 crontab

crontab -e
Run Code Online (Sandbox Code Playgroud)

或通过其他用户

sudo crontab -e -u nameOfOtherUser
Run Code Online (Sandbox Code Playgroud)

你的 cron 会是什么样子

使用 php 完整路径的可能的 cron 行

*/5 * * * * /usr/bin/php  -q /var/www/html/sitename/cron.php
Run Code Online (Sandbox Code Playgroud)

或用于@Brian 提到的调试目的

*/5 * * * * /usr/bin/php  -q /var/www/html/sitename/cron.php > /var/www/html/sitename/cron-temp.log 2>&1
Run Code Online (Sandbox Code Playgroud)

这把被称为日志文件cron-temp.log/var/www/html/sitename/

关于参数

-q = 安静模式

你的待办事项

  • 编辑您的 cron 并添加新行,该行定义应调用哪个脚本的频率
  • 如果您正在使用此选项,请等到间隔触发并检查日志文件