0

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..."
satin
  • 697
  • 2
  • 7
  • 19
  • Without `-it` there will be no results on standard output. – tripleee Jul 23 '21 at 15:20
  • It's not supposed to be interactive. But it does need to be connected to STDOUT for a cron? – satin Jul 23 '21 at 15:22
  • `cron` doesn't care, and clearly runs it happily. But you won't get any results without I think at least `-t`, but maybe you need `-i` too. – tripleee Jul 23 '21 at 15:24
  • Vaguely relevant? https://stackoverflow.com/questions/16232661/how-to-get-the-output-from-docker-run-i – tripleee Jul 23 '21 at 15:25
  • Can you `cd /location_to_backup_dir; node backupscript.js` instead of trying to run a Docker container? – David Maze Jul 23 '21 at 15:27
  • My server is not installed with node and I want to run the file in a docker to keep the system clean and not install the deps for node or node itself :). – satin Jul 23 '21 at 15:31
  • Does this answer your question? [CronJob not running](https://stackoverflow.com/questions/22743548/cronjob-not-running) – tripleee Jul 23 '21 at 16:12

1 Answers1

2

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.

rezshar
  • 570
  • 1
  • 6
  • 20
  • 1
    Don't know why this is downvotes, but it helped. crontab was not able to find docker binary. using `/snap/bin/docker` as absolute path to the command helped and it works now. – satin Jul 23 '21 at 15:29