Skip to content

EC2 ubuntu crob job failure

0

Hi,

I’m trying to set up a cron job on an Ubuntu EC2 instance, but it keeps failing with a Permission denied error.

These are the steps I followed:

  1. Edited the crontab:

    crontab -e
    
  2. Added the following entry to run every minute for testing:

    * * * * * /usr/bin/docker system prune -af
    
  3. Verified the entry was saved:

    crontab -l
    

The cron service is running, but when I check the logs I see:

Mar 10 18:06:01 remote-docker-engine-amd-01 cron[19243]: Permission denied
Mar 10 18:06:01 remote-docker-engine-amd-01 CRON[19243]: Permission denied

The command itself works when I run it manually as the same user:

/usr/bin/docker system prune -af

So Docker permissions seem fine when executed interactively, but the job fails when triggered by cron.

I also tried reinstalling the cron package, but the issue persists.

Am I missing something in the cron configuration or environment? Any guidance would be appreciated.

Thanks.

asked 2 months ago87 views
2 Answers
1

Hello.

Even if I set "sudo" in the command set in cron, it fails to execute?
If you run it as the root user, it should generally run successfully.
To troubleshoot, start by enabling sudo.
https://stackoverflow.com/questions/70099692/crontab-sudo-docker-permission-denied

EXPERT
answered 2 months ago
AWS
EXPERT
reviewed 2 months ago
0

When you run docker commands interactively, your shell session has access to the Docker socket (usually docker.sock) because your user is in the docker group. However, cron jobs run in a minimal environment without loading group memberships the same way.

For a quick fix, try to run the cron job as root:

sudo crontab -e

AWS
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.