satin
satin

Reputation: 707

Running a "docker run" command via crontab does not work

Everyminute ( for Testing ) I am running a cronjob to create a container, Run a nodejs process in it and then remove the container as well.

It works as expected from a regular command, but fails to produce any results or output when running via crontab.

here is my crontab

*/1 * * * * sh /location_to_cron/cron.sh >> /location_to_cron/backup.log

Here is the content of the cron shell script

CONTAINER_NAME="node_backup_repo"

echo "Creating Container to create backup, name: $CONTAINER_NAME"

docker run --name $CONTAINER_NAME -v /location_to_backup_dir:/app --dns="my_custom_dns" -w /app node:16 "backupscript.js" >> /location_to_cron/backup.log

echo "Removing container $CONTAINER_NAME"
docker rm $CONTAINER_NAME

echo "All done..."

Syslog entries indicate that the crontab is being executed as my user, which is as expected. What could be wrong here?

Edit: Modified Working Script - Fix was to add path to docker binary.

CONTAINER_NAME="node_backup_repo"

echo "Creating Container to do Backup, name: $CONTAINER_NAME"

/snap/bin/docker run --name $CONTAINER_NAME -v /location_to_backup_dir:/app --dns="my_custom_dns" -w /app node:16 "backupscript.js" >> /location_to_cron/backup.log

echo "Removing container $CONTAINER_NAME"
/snap/bin/docker rm $CONTAINER_NAME

echo "All done..."

Upvotes: 0

Views: 1854

Answers (1)

rezshar
rezshar

Reputation: 630

use this:

*/1 * * * * sh /location_to_cron/cron.sh >> /location_to_cron/backup.log 2>&1

it can send stdout and stderr to backup.log.

Upvotes: 2

Related Questions