The script is working when executed from terminal but, it's giving "(CRON) info (No MTA installed, discarding output)" information when executed from crontab.
Below are my script contents. I have 2 scripts, and a master scripts which executes these two scripts sequentially. You can create them as it is in your system too for testing. I haven't written anything complex here.
testpad1.sh. Script is written with error intentionally.
#!/bin/bash
echo "This is script one.
sleep 3
testpad2.sh
#!/bin/bash
echo "This is script two"
sleep 3
masterpad.sh
#!/bin/bash
mkdir -p logs
./testpad1.sh &> logs/testpad1.log
echo `date`
./testpad2.sh &> logs/testpad2.log
echo `date`
As you can see, the script masterpad.sh redirects other two scripts' output to their respective log files.
Here is the cronjob.
* * * * * /opt/scripts/masterpad.sh
It's working without issues when executed from terminal, by creating testpad1.log and testpad2.log along with expected output.
But if I schedule the same in crontab, it's giving "(CRON) info (No MTA installed, discarding output)" error. This error was checked by excecuting, "grep CRON /var/log/syslog"
.
Thank you.
Update
Since a user in comment section doubted this post with other post, Below is the output to show that, error was not there previously but have occurred only after I added the cronjob. This is the previous output of "grep CRON /var/log/syslog" command.
CMD (/opt/scripts/masterpad.sh &> /opt/scripts/logs/masterpad.log)
CMD (/opt/scripts/masterpad.sh)
CRON[6656]: (CRON) info (No MTA installed, discarding output)
CRON[6733]: (padmahasa) CMD (/opt/scripts/masterpad.sh)
CRON[6732]: (CRON) info (No MTA installed, discarding output)
Update 2: Summarized answer from comments and accepted answer.
To get both STDOUT and STDERR from other scripts that are invoked from master script or command (which crontab executes), can use both "&>" or still better option "|& tee" redirection or you can use any other suitable option as in this table but, to capture STDOUT and STDERR of master script or command, "/absolute/path/to/script > absolute/path/to/logfile 2>&1" is the only format that works. Hence the modifications to
1. masterpad.sh
#!/bin/bash
mkdir -p logs
./testpad1.sh |& tee logs/testpad1.log
echo `date`
./testpad2.sh |& tee logs/testpad2.log
echo `date`
2. crontab job
* * * * * /opt/scripts/masterpad.sh > /opt/scripts/logs/masterpad.log 2>&1
Thank you all for supporting.
>/dev/null 2>&1
should work ... or>some_log_file 2>&1
– Chev_603 Nov 12 '19 at 06:48#* * * * * /opt/scripts/masterpad.sh &> /opt/scripts/logs/masterpad.log
current form:* * * * * /opt/scripts/masterpad.sh
– learner Nov 12 '19 at 07:28/bin/sh
(which is thedash
shell by default) -&>
is a bashism, dash will treat it as an instruction to run the command in the background (&
) and redirect standard output only (>
) – steeldriver Nov 12 '19 at 11:40/opt/scripts/masterpad.sh &> /opt/scripts/logs/masterpad.log
in the crontab by/opt/scripts/masterpad.sh > /opt/scripts/logs/masterpad.log 2>&1
? – steeldriver Nov 12 '19 at 14:14* * * * * /opt/scripts/masterpad.sh
. Still no log file generated. – learner Nov 12 '19 at 14:27&>
" or still better option "|& tee
" redirection but, to capture STDOUT and STDERR of master script or command, "/absolute/path/to/script > absolute/path/to/logfile 2>&1
" is the only format that works. – learner Nov 13 '19 at 04:07