0

I have a very simple cron task I want to run hourly, its a bash script that I want to store the output of in a daily log file. The crontab looks like:

[srvc_user]$ crontab -l
15 * * * * /path/to/script 2&>1 >> /path/to/logs/script_$(date +"%Y%m%d").log

What I'm seeing from the syslog is that the task is running hourly as scheduled but no new log files are being produced:

[root]# journalctl -u crond.service -g script
Aug 17 04:15:02 myhost.server.com CROND[3827797]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 05:15:01 myhost.server.com CROND[3742589]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 06:15:01 myhost.server.com CROND[3655718]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 07:15:01 myhost.server.com CROND[3567401]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 08:15:01 myhost.server.com CROND[3479789]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)
Aug 17 09:15:01 myhost.server.com CROND[3386346]: (srvc_user) CMD (/path/to/script 2&>1 >> /path/to/logs/script_$(date +").log)

[srvc_user]$ ls /path/to/logs
## No new files here!

I can run the script on its own as the crontab user with no problems; I can also copy-paste the full crontab line and it runs the script and pipes the output to the logfile as expected:

[srvc_user]$ /path/to/script
# Normal output here!

[srvc_user]$ /path/to/script 2&>1 >> /path/to/logs/script_$(date +"%Y%m%d").log
[srvc_user]$ ls /path/to/logs
# New files here!

I also verified that the crontab user has access to the log folder:

[srvc_user]$ ls /path/to
drwxr-xr-x 2 srvc_user users    4096 Aug 17 01:26 logs
# other files...

Not sure what to look at next.

  • 1
    escape the percent signs, turn `%` to `\%` – Barry Carter Aug 17 '22 at 14:29
  • That was it, thank you! I over-edited my original post, I see how I could have gotten that from the journalctl output now. Thank you so much! – GodLovesUGaius Aug 17 '22 at 14:36
  • Also, your redirects are in the wrong order. To send both standard and error output to the log, you need to put `2&>1` *after* the `>>` redirect. See [BashFAQ #55](http://mywiki.wooledge.org/BashFAQ/055). – Gordon Davisson Aug 17 '22 at 17:48

0 Answers0