Search code examples
ubuntucron

Linux Crontab does not copy file


I have a problem with the following crontab entry.

14 8 * * * sudo cp /home/george/service/logs/daily.log "/home/george/service/logs/daily_$(date +%Y%m%d -d "yesterday").log"

This entry is supposed to be executed every day at 8:14 sever time.

It`s purpose is to take a back up of a daily log for safekeeping and further analysis.

I tested the script as is before entering it to crontab and worked as expected.

Also I decided to used sudo as I didnt want to be bothered with user privileges.

I am sure that the cron service is running because there is an other cronjob that writes every day at this log file.

I am running an Ubuntu 18 server and the cron jobs are written in /etc/crontab.
The inititial cron jobs were written by a colleague of mine using the following command.

sudo nano /etc/crontab

All the other cron jobs are working perfectly, just this new copy script is not.

Any ideas?


Solution

  • Processes which starts via crontab usually using sh (not bash) so you need to write full path of the commands. In your example i think it will like this:

    14 8 * * * /usr/bin/sudo /usr/bin/cp  /home/george/service/logs/daily.log "/home/george/service/logs/daily_$(date +%Y%m%d -d "yesterday").log"
    

    Also you can check cron job output in /var/log/syslog:

    sudo grep CRON /var/log/syslog
    

    For cron jobs manipulating you can use command crontab with keys -e (edit) -l (list):

    sudo crontab -e
    

    Also this one i think will good tool for you:

    https://crontab.guru/