0

I have the following script:

#!/bin/bash
echo "$(date +"%r")"

if [ "$FOLDER_ID" != "0" ]
then
  node /sync.js -d $FOLDER_ID -l /Downloads
fi

I'm using docker container and I have passed the env variable FOLDER_ID when started the container. This variable supposed to be set when starting the docker container by passing it as ENV.

When I run this script, the node command runs as expected, but when the cron job runs the script, it prints the date at but doesn't execute the node command correctly (I think the issue is related to the $FOLDER_ID variable).

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
  • 2
    I can't let it pass without commenting: echo "$(date +"%r")" is just a long way of saying date +%r. Separately, I don't suppose FOLDER_ID has any spaces or tabs in it? – Jeff Schaller Mar 09 '19 at 01:26
  • no it does not. I can use FOLDER_ID in the commandline with no problems – ialqwaiz Mar 09 '19 at 01:29
  • another common problem with cron jobs is that they (as you've seen) run with a new, bare, environment. Are there any other environment variables (perhaps related to the 'node' command or your script) that are also missing? – Jeff Schaller Mar 09 '19 at 01:46
  • Yes. I have two different folder_id variables and I have the same issue with the second part of the script. I assumed I didn't use the env variables correctly... – ialqwaiz Mar 09 '19 at 01:50
  • @ialqwaiz Cron jobs run in a limited environment with certain restrictions, one of which is a limited set of environment variables. If you're relying on specific environment variables (such as the FOLDER_ID variable), you'll need to set them explicitly before using them in your script. – Haxiel Mar 09 '19 at 04:45
  • Do you get any errors from running the script through the cron job? Errors from cron jobs are usually mailed to the owner of that cron job. I'm assuming it's $PATH that does not contain the directory where node is located. You can modify PATH in the script though. – Kusalananda Mar 09 '19 at 08:17
  • I had the script print the env and it appears that some variables are missing from the cron environment as you pointed out. Is there a way to make cron job runs in the same environment bash command line has? – ialqwaiz Mar 09 '19 at 09:25
  • @ialqwaiz No, bash and cron are two separate processes. The bash environment is contained in the specific bash process that is running. – dijksterhuis Mar 09 '19 at 21:19
  • @ialqwaiz also, why are you using cron in a docker container? Docker containers should just be for running your service/script. Deployment stuff - like scheduled runs - should be handled by something else (host system cron, Jenkins etc). – dijksterhuis Mar 09 '19 at 21:27

0 Answers0