0

I have a crontab set up to run a python script called from a docker-compose command:

* * * * * cd path/to/repo && docker-compose run worker python -c "from directory.module import test_function; test_function()"

The module & test_function looks like this:

import logger

def test_function():
    logger.info('Hello')

The docker container is up and running. However, when I go to check my log file, nothing has been written to it. I'm not sure what I need to do to get this working correctly.

Kevin G
  • 168
  • 2
  • 15
  • Can you verify that the job actually runs? Do you get any errors in your email? Please review https://stackoverflow.com/questions/22743548/cronjob-not-running for troubleshooting instructions and [edit] to update accordingly. – tripleee Dec 30 '21 at 18:28
  • There is a message in `/var/mail/username`. It says `/bin/sh: docker-compose: command not found` – Kevin G Dec 30 '21 at 19:46
  • So there, you probably have to fix your `PATH`. – tripleee Dec 30 '21 at 19:49

1 Answers1

0

So, I created a shell script (test_cronjob.sh) to run the commands I need.

#!/bin/bash

PATH=/usr/local/bin/
docker-compose -f /full/path/to/docker-compose.yml run worker python -c "from directory.module import test_function; test_function()"

And edited my crontab -e:

* * * * * /bin/sh /full/path/to/test_cronjob.sh

Or just simply editing the crontab -e without the shell script, like below:

* * * * * PATH=/usr/local/bin/ docker-compose -f /full/path/to/docker-compose.yml run worker python -c "from directory.module import test_function; test_function()"
Kevin G
  • 168
  • 2
  • 15
  • 1
    If you supply the ful path to `docker-compose`, overriding the `PATH` with a bogus value seems wrong, and will probably wreck any other `cron` jobs of yours. Just to be explicit, you should *add* this directory to the default `PATH`, not lose the previous values (`/bin:/usr/bin` etc are quite crucial for normal system operation). – tripleee Dec 31 '21 at 05:28