3
  1. Say I have a script named "testcron" containing the following:

    #!/bin/bash echo This script ran on $(date)

  2. I then set the permissions: chmod ug+x testcron

  3. Finally, I copy this script to /etc/cron.hourly

  4. And I wait for something to happen at the next hour.

So, I found in the /var/log/cron the following entry: Jul 17 12:01:02 localhost run-parts(/etc/cron.hourly)[13218]: starting testcron Jul 17 12:01:02 localhost run-parts(/etc/cron.hourly)[13240]: finished testcron

However, I could not find the script's actual output anywhere. Where does the output of this script go? Is it mailed? The root has no mail account. Can I redirect the mail to another user for just this script or will all mail output end up in the user's account? Does it go to /var/log/messages? What is the correct way redirect the output to, say, a dedicated log file?

Note: The script I included above doesn't actually do anything useful but I want to adapt the principles to another script that does.

OS: centos 7

Thanks to all in advance, Allan

AWM
  • 45
  • 5

1 Answers1

2

On CentOS, it doesn’t seem to be logging output anywhere, just the fact that the cron has executed is stored in /var/log/cron.

If you need the output, you can use standard shell redirection. First create a file:

touch /var/log/cronoutput

Then capture the output of your cron, example:

* * * * * echo "hey" >> /var/log/cronoutput

Output:

tail /var/log/cronoutput
hey
hey
hey
hey
notStan
  • 313
  • 1
  • 9
  • There is no such file on this system. – AWM Jul 17 '19 at 16:16
  • There is a file named `/var/log/cron` that contains entries indicating the start and finish of cron jobs but no output from the actual script run. – AWM Jul 17 '19 at 16:17
  • Try `/var/log/messages` ? – notStan Jul 17 '19 at 16:18
  • That file exists but doen't have the output from the script. – AWM Jul 17 '19 at 16:18
  • Hmm, it doesn’t seem that it logs it. You’d have to create your own file to capture the output. Updating my answer. – notStan Jul 17 '19 at 16:25
  • If I am understanding your answer correctly, you are showing the entry used for the interactive command, `crontab -e` but I am not using `crontab -e`. Instead, I am using another script to copy the testcron script to `/etc/cron.hourly` and am trying to avoid any manual interaction. Is it OK to redirect the output from within the script? – AWM Jul 17 '19 at 16:35
  • OK I tried redirecting the output inside the script rather than using crontab -e to specify the redirection and it worked. So, even though I did not follow notStan's instructions precisely, his answer did lead me to the solution. – AWM Jul 18 '19 at 16:25