0

I'm trying to delete logs file in a container with cron:

$ crontab -e
# I add this line to file, must execute each 10 minutes
10 * * * * docker exec -it  explorer.org1.company.fr sh -c 'rm /opt/logs/app/app.log-*'

When I check cron logs with

 grep CRON /var/log/syslog

I get:

Jan 22 09:10:01 ip-172-31-46-123 CRON[4029]: (ubuntu) CMD (docker exec -it  explorer.org1.company.fr sh -c 'rm /opt/logs/app/app.log-*')

Which seems to indicate that cron command triggered.

But when I check the files with:

docker exec -it  explorer.org1.company.fr sh -c 'ls /opt/logs/app'

the files haven't been deleted.

If I execute the command manually, it will delete the 2 files, so it confirms command is working.

Why is the command not effective when running with cron ???

2 Answers2

3

maybe docker isn't in crons $PATH ? Note that cron sets its own $PATH var. Just that cron tells the cronjob gets executed, it doesnt tell you if the command was found. (But the status mail you'd receive on failure would tell it).

also there is a failure in

# I add this line to file, must execute each 10 minutes
10 ...

it should be

*/10
  • docker is in the $PATH, I can confirm it manually. As for the */10, it will also work, but the job is triggering, so this is not the issue. – Juliatzin del Toro Jan 22 '20 at 09:23
  • 1
    cron sets its own path variable and a cronjob will always trigger, the system checks for the file as it tries to access it, not befor running a cronjob. You can verify it by adding a "* * * * * iDontExist" job. – Zoë van Borg Jan 22 '20 at 09:29
2

After installing Postfix / Mutt, I could get the error message:

the input device is not a TTY

That is discussed here

Removing -it after docker exec did the trick!

Thanks for your help!