0

I have two cronjobs in my crontab. The first works; the second doesn't. Specifically,

40 18 * * * cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner /mnt/c/Users/Adrian/pick_of_the_day/`date +'%b-%d-winner'` 

doesn't work.

I have tried:

- commenting out the first one
- removing the full path for the destination file and checking the default DIR for my user
- using a relative path for the dest file
- looking through 5 other cronjob questions on this site
- looking for the cron log file but it doesn't exist.  Before someone tells me to check syslog, please note it doesn't exist on my installation.
- **I removed the ">>/var/log/cron_output.log 2>&1" and just left the command**

Aviator@AW:/var/log$ ls alternatives.log btmp dpkg.log fsck lxd upstart apt dist-upgrade fontconfig.log lastlog unattended-upgrades wtmp Aviator@AW:/var/log$

35 18,21 * * * /mnt/c/Users/Adrian/pick_of_the_day/winner
40 18 * * * cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner 
/mnt/c/Users/Adrian/pick_of_the_day/`date +'%b-%d-winner'` >>/var/log/cron_output.log 2>&1
Adrian
  • 341
  • 1
  • 3
  • 11
  • Have you try to create shell script instead of adding all commands in cron? – Romeo Ninov Feb 02 '19 at 07:21
  • That's actually a great idea. The first job that works is, in fact, a script. I can just add the cp line to the bottom. I am still curious why a simple cp is failing to execute in crontab though. – Adrian Feb 02 '19 at 19:23
  • maybe the execution of `date +'%b-%d-winner'` do not go well. Do you want to add it as answer (if this resolve your problem)? – Romeo Ninov Feb 02 '19 at 19:46
  • @RomeoNinov Thank you for the suggestion (to add it to my script) and it does work as expected - I just tested. I don't want to add it as an answer as the original question is still unsolved. – Adrian Feb 02 '19 at 19:52
  • instead of date execution add static string and try to run it again (in cron w/o script) – Romeo Ninov Feb 02 '19 at 19:58
  • A static filename at the end worked so it is related to date expansion. I tried `cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner /mnt/c/Users/Adrian/pick_of_the_day/$(date +'%b%mwin')` instead of backticked date expansion but that didn't work either. – Adrian Feb 02 '19 at 20:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187791/discussion-between-romeo-ninov-and-adrian). – Romeo Ninov Feb 02 '19 at 20:21

1 Answers1

1

% characters in cron need to be escaped whereas they don't if in a script or directly on the cmd line.

After escaping them the cronjob ran as expected.

cp /mnt/c/Users/Adrian/pick_of_the_day/daily_winner /mnt/c/Users/Adrian/pick_of_the_day/$(date +'\%b\%mwinner')

Relevant answers I just located: https://unix.stackexchange.com/questions/29578/how-can-i-execute-date-inside-of-a-cron-tab-job

https://unix.stackexchange.com/questions/252615/why-doesnt-date-f-t-work-in-crontab

Adrian
  • 341
  • 1
  • 3
  • 11