Skip to content

Direct STDERR output when listing crontab to /dev/null #20951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/internal/Magento/Framework/Crontab/CrontabManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See COPYING.txt for license details.
*/


namespace Magento\Framework\Crontab;

use Magento\Framework\App\Filesystem\DirectoryList;
Expand Down Expand Up @@ -40,31 +41,35 @@ public function __construct(
}

/**
* Build tasks block start text.
*
* @return string
*/
private function getTasksBlockStart()
{
$tasksBlockStart = self::TASKS_BLOCK_START;
if (defined('BP')) {
$tasksBlockStart .= ' ' . md5(BP);
$tasksBlockStart .= ' ' . hash("sha256", BP);
}
return $tasksBlockStart;
}

/**
* Build tasks block end text.
*
* @return string
*/
private function getTasksBlockEnd()
{
$tasksBlockEnd = self::TASKS_BLOCK_END;
if (defined('BP')) {
$tasksBlockEnd .= ' ' . md5(BP);
$tasksBlockEnd .= ' ' . hash("sha256", BP);
}
return $tasksBlockEnd;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getTasks()
{
Expand All @@ -82,7 +87,7 @@ public function getTasks()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function saveTasks(array $tasks)
{
Expand Down Expand Up @@ -118,8 +123,7 @@ public function saveTasks(array $tasks)
}

/**
* {@inheritdoc}
* @throws LocalizedException
* @inheritdoc
*/
public function removeTasks()
{
Expand Down Expand Up @@ -182,7 +186,7 @@ private function cleanMagentoSection($content)
private function getCrontabContent()
{
try {
$content = (string)$this->shell->execute('crontab -l');
$content = (string)$this->shell->execute('crontab -l 2>/dev/null');
} catch (LocalizedException $e) {
return '';
}
Expand All @@ -203,6 +207,7 @@ private function save($content)

try {
$this->shell->execute('echo "' . $content . '" | crontab -');
// phpcs:disable Magento2.Exceptions.ThrowCatch
} catch (LocalizedException $e) {
throw new LocalizedException(
new Phrase('Error during saving of crontab: %1', [$e->getPrevious()->getMessage()]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See COPYING.txt for license details.
*/


namespace Magento\Framework\Crontab\Test\Unit;

use Magento\Framework\Crontab\CrontabManager;
Expand All @@ -16,6 +17,9 @@
use Magento\Framework\Filesystem\Directory\ReadInterface;
use Magento\Framework\Filesystem\DriverPool;

/**
* Tests crontab manager functionality.
*/
class CrontabManagerTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -58,7 +62,7 @@ public function testGetTasksNoCrontab()

$this->shellMock->expects($this->once())
->method('execute')
->with('crontab -l', [])
->with('crontab -l 2>/dev/null', [])
->willThrowException($localizedException);

$this->assertEquals([], $this->crontabManager->getTasks());
Expand All @@ -74,7 +78,7 @@ public function testGetTasks($content, $tasks)
{
$this->shellMock->expects($this->once())
->method('execute')
->with('crontab -l', [])
->with('crontab -l 2>/dev/null', [])
->willReturn($content);

$this->assertEquals($tasks, $this->crontabManager->getTasks());
Expand All @@ -88,17 +92,17 @@ public function getTasksDataProvider()
return [
[
'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
'tasks' => ['* * * * * /bin/php /var/www/magento/bin/magento cron:run'],
],
[
'content' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
'tasks' => [
'* * * * * /bin/php /var/www/magento/bin/magento cron:run',
'* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run',
Expand Down Expand Up @@ -127,7 +131,7 @@ public function testRemoveTasksWithException()

$this->shellMock->expects($this->at(0))
->method('execute')
->with('crontab -l', [])
->with('crontab -l 2>/dev/null', [])
->willReturn('');

$this->shellMock->expects($this->at(1))
Expand All @@ -148,7 +152,7 @@ public function testRemoveTasks($contentBefore, $contentAfter)
{
$this->shellMock->expects($this->at(0))
->method('execute')
->with('crontab -l', [])
->with('crontab -l 2>/dev/null', [])
->willReturn($contentBefore);

$this->shellMock->expects($this->at(1))
Expand All @@ -166,17 +170,17 @@ public function removeTasksDataProvider()
return [
[
'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
],
[
'contentBefore' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento setup:cron:run' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
'contentAfter' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
],
[
Expand Down Expand Up @@ -276,7 +280,7 @@ public function testSaveTasks($tasks, $content, $contentToSave)

$this->shellMock->expects($this->at(0))
->method('execute')
->with('crontab -l', [])
->with('crontab -l 2>/dev/null', [])
->willReturn($content);

$this->shellMock->expects($this->at(1))
Expand All @@ -292,9 +296,9 @@ public function testSaveTasks($tasks, $content, $contentToSave)
public function saveTasksDataProvider()
{
$content = '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * /bin/php /var/www/magento/bin/magento cron:run' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL;
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL;

return [
[
Expand All @@ -303,52 +307,52 @@ public function saveTasksDataProvider()
],
'content' => $content,
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * ' . PHP_BINARY . ' run.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
],
[
'tasks' => [
['expression' => '1 2 3 4 5', 'command' => 'run.php']
],
'content' => $content,
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '1 2 3 4 5 ' . PHP_BINARY . ' run.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
],
[
'tasks' => [
['command' => '{magentoRoot}run.php >> {magentoLog}cron.log']
],
'content' => $content,
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php >>'
. ' /var/www/magento2/var/log/cron.log' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
],
[
'tasks' => [
['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
],
'content' => $content,
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
. ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
],
[
'tasks' => [
['command' => '{magentoRoot}run.php % cron:run | grep -v "Ran \'jobs\' by schedule"']
],
'content' => '* * * * * /bin/php /var/www/cron.php',
'contentToSave' => '* * * * * /bin/php /var/www/cron.php' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . md5(BP) . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_START . ' ' . hash("sha256", BP) . PHP_EOL
. '* * * * * ' . PHP_BINARY . ' /var/www/magento2/run.php'
. ' %% cron:run | grep -v \"Ran \'jobs\' by schedule\"' . PHP_EOL
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . md5(BP) . PHP_EOL,
. CrontabManagerInterface::TASKS_BLOCK_END . ' ' . hash("sha256", BP) . PHP_EOL,
],
];
}
Expand Down