您可以使用两个文件来实现:
在项目的根目录中创建文件夹和类,例如:
crons / CronprocessApp.php
    <?php
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use phpseclib\Net\SFTP;
use phpseclib\Crypt\RSA;
class CronprocessApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface{
    public function __construct(
    \Magento\Framework\App\State $state,\Magento\Framework\App\Response\Http $response)
    {
        $this->_response = $response;
        //$state->setAreaCode('any area'); // or 'adminhtml', depending on your needs
        $state->setAreaCode('adminhtml'); // or 'adminhtml', depending on your needs
    }
    public function launch()
    {
        /** @var \Vendor\Module\Cron\Test $cron */
        $cron = \Magento\Framework\App\ObjectManager::getInstance()
            ->create('Custom\Preorder\Cron\ChangeVisiblityNonPreorderProduct'); //pass the name of your cron class path 
        $cron->execute();       
        return $this->_response;
    }
    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }
}
?>
创建另一个类文件:
crons / Cronprocess.php
 <?php
require __DIR__ . '/../app/bootstrap.php';
require __DIR__ . '/../crons/cronprocessApp.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('CronprocessApp');
$bootstrap->run($app);
要运行cron,请使用项目根路径进入cli并运行以下命令:
php crons/cronprocess.php
               
              
require '../app/bootstrap.php';同样在TestApp.php上也需要设置:return $this->_response;否则它将在phpstorm屏幕截图上抛出错误,但没有实际代码。我将尝试以此编辑您的答案。