# Cron output & logging

### A basic solution:

* use `$()` for executing `date` command and return output
* format **datetime** to UTC, escape the `%` character with `\`
* add `2>&1` at the end for streaming both `stdout` and `stderr` into that log file

### Example:

```
* * * * * echo "Test crontab log" > /tmp/crontab.log.$(date --utc +\%Y\%m\%d_\%H\%M\%SZ) 2>&1
```

### Output:

```
ls -lh /tmp | grep log

-rw-rw-r-- 1 ubuntu  ubuntu    17 May  4 05:06 crontab.log.20190504_050601Z
-rw-rw-r-- 1 ubuntu  ubuntu    17 May  4 05:07 crontab.log.20190504_050701Z
```

### Reference

1. <https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job/517052#517052>
2. <https://crontab.guru/>
3. <https://yasoob.me/posts/6-tips-before-you-write-your-next-bash-cronjob/>
