-1

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).

Any suggestions? Thanks

ialqwaiz
  • 199
  • 1
  • 4
  • 5
    You never set `FOLDER_ID`, where is it supposed to come from? – Barmar Mar 09 '19 at 00:44
  • 2
    Cron jobs don't run shell startup scripts like `.profile` or `.bashrc`, if that's where you normally set the variable. – Barmar Mar 09 '19 at 00:45
  • You _could_ set environment variables in the crontab file, as [this answer](https://stackoverflow.com/a/10657111/17300) says. – Stephen P Mar 09 '19 at 00:48
  • The variable is already set in the env and readable from the command line. – ialqwaiz Mar 09 '19 at 00:49
  • I'm using docker container and have passed the env variable FOLDER_ID when started the container – ialqwaiz Mar 09 '19 at 00:54
  • 1
    [unix.se] would probably be a better place to ask how to make an environment variable global in a docker container. – Barmar Mar 09 '19 at 00:59
  • 1
    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:27

1 Answers1

0

As a few people pointed out in comments FOLDER_ID needs to be set somewhere, are you setting it in your crontab line?

You could try adjusting the crontab line to call the script with bash -x or put a set -x near the top of the script, this will cause bash to "print commands and their arguments as they are executed". If you are receiving the emails from cron you should be able to review that email and find output like:

++ date +%r
+ echo '11:55:41 AM'
11:55:41 AM
+ '[' '' '!=' 0 ']'
+ node /sync.js -d -l /Downloads
...

Note that the lines beginning with a + are displaying what bash is about to execute as the command.

It is worth also beginning bash scripts with some flags to improve strictness e.g:

#!/bin/bash
set -euo pipefail

In this case the -u flag would result in something like line 5: FOLDER_ID: unbound variable and a non-zero exit status assuming the FOLDER_ID is not correctly set, helping to highlight the problem.

P.s It's worth using set on its own line and not on the hash bang (#!/bin/bash -euo pipefail) as keeping it separate ensures that debugging with bash -x doesn't accidentally turn off intended flags.

lyte
  • 1,162
  • 10
  • 9
  • It was indeed the env variable issue. 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:28
  • @ialqwaiz It'll never be exactly the same as your environment in your interactive bash may have come from multiple locations e.g in this case docker is passing the env var only to the entrypoint command and you'd have to chase it all the way through to ensure everything maps over - more realistically, adjust your entrypoint to store the var you care about in something like `/.env` and then `source /.env` at the top of your bash script. – lyte Mar 09 '19 at 21:32