1

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.

  • Possible duplicate of askubuntu.com/questions/222512/… – Chev_603 Nov 12 '19 at 5:59
  • @Chev_603, I have gone through the link you have provided. In that case, the user have nothing to do with the error, since he has not set any cronjob. In my case, I'm getting this error, only when I set the cronjob, which is totally opposite to the other user's use case. – learner Nov 12 '19 at 6:06
  • Ah, my bad. Can I see your crontab file? Is the output not being redirected to the assigned log files? Or is it just that you are wondering why you are seeing that error message? – Chev_603 Nov 12 '19 at 6:40
  • Also, to suppress that output, in your cron file, adding >/dev/null 2>&1 should work ... or >some_log_file 2>&1 – Chev_603 Nov 12 '19 at 6:48
  • @Chev_603, I'm not just wondering about error, I'm not getting the output in the file. I have tried in various forms. Below are the two of them. I've also tried using tee command as shown in the second answer, the one with table of options. #* * * * * /opt/scripts/masterpad.sh &> /opt/scripts/logs/masterpad.log current form: * * * * * /opt/scripts/masterpad.sh – learner Nov 12 '19 at 7:28
1

logs/testpad1.log will behave differently depending on your current directory. It can also behave differently depending if cron is running as root or you are calling the script from your terminal.

The safe (and usually error correcting) thing to do is hard code the full path into the file name:

/home/me/scripts/logs/testpad1.log
/var/log/testpad1.log
  • Thank you very much for your answer Win. I can't believe all it has to do with is just the path. I thought, once full path to master script is provided, rest will be executed in relation with the path of master script. – learner Nov 12 '19 at 19:58
  • @learner I know I made that mistake first learning cron and I bet 90+% of everyone else has too. – WinEunuuchs2Unix Nov 12 '19 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.