running bash script from cron job Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Where can I set environment variables that crontab will use?Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?Pipe to/from the clipboard in Bash scriptHow do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Echo newline in Bash prints literal n
What is a more techy Technical Writer job title that isn't cutesy or confusing?
Is the Mordenkainens' Sword spell underpowered?
Does the transliteration of 'Dravidian' exist in Hindu scripture? Does 'Dravida' refer to a Geographical area or an ethnic group?
Problem with display of presentation
Short story about astronauts fertilizing soil with their own bodies
Flight departed from the gate 5 min before scheduled departure time. Refund options
Besides transaction validation, are there any other uses of the Script language in Bitcoin
How does the body cool itself in a stillsuit?
Understanding piped commands in GNU/Linux
malloc in main() or malloc in another function: allocating memory for a struct and its members
The Nth Gryphon Number
Is the time—manner—place ordering of adverbials an oversimplification?
How to make an animal which can only breed for a certain number of generations?
French equivalents of おしゃれは足元から (Every good outfit starts with the shoes)
How does TikZ render an arc?
Order between one to one functions and their inverses
Why do C and C++ allow the expression (int) + 4*5;
What is the proper term for etching or digging of wall to hide conduit of cables
Why weren't discrete x86 CPUs ever used in game hardware?
An isoperimetric-type inequality inside a cube
When to apply negative sign when number is squared
Does the main washing effect of soap come from foam?
Did any compiler fully use 80-bit floating point?
Why not use the yoke to control yaw, as well as pitch and roll?
running bash script from cron job
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Where can I set environment variables that crontab will use?Get the source directory of a Bash script from within the script itselfHow to check if a string contains a substring in BashHow to check if a program exists from a Bash script?How do I tell if a regular file does not exist in Bash?Pipe to/from the clipboard in Bash scriptHow do I split a string on a delimiter in Bash?Extract filename and extension in BashHow to concatenate string variables in BashHow do I set a variable to the output of a command in Bash?Echo newline in Bash prints literal n
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
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
linux bash cron
|
show 2 more comments
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
linux bash cron
5
You never setFOLDER_ID, where is it supposed to come from?
– Barmar
Mar 9 at 0:44
2
Cron jobs don't run shell startup scripts like.profileor.bashrc, if that's where you normally set the variable.
– Barmar
Mar 9 at 0:45
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
1
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0: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 9 at 9:27
|
show 2 more comments
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
linux bash cron
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
linux bash cron
linux bash cron
edited Mar 9 at 1:12
ialqwaiz
asked Mar 9 at 0:43
ialqwaizialqwaiz
8913
8913
5
You never setFOLDER_ID, where is it supposed to come from?
– Barmar
Mar 9 at 0:44
2
Cron jobs don't run shell startup scripts like.profileor.bashrc, if that's where you normally set the variable.
– Barmar
Mar 9 at 0:45
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
1
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0: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 9 at 9:27
|
show 2 more comments
5
You never setFOLDER_ID, where is it supposed to come from?
– Barmar
Mar 9 at 0:44
2
Cron jobs don't run shell startup scripts like.profileor.bashrc, if that's where you normally set the variable.
– Barmar
Mar 9 at 0:45
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
1
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0: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 9 at 9:27
5
5
You never set
FOLDER_ID, where is it supposed to come from?– Barmar
Mar 9 at 0:44
You never set
FOLDER_ID, where is it supposed to come from?– Barmar
Mar 9 at 0:44
2
2
Cron jobs don't run shell startup scripts like
.profile or .bashrc, if that's where you normally set the variable.– Barmar
Mar 9 at 0:45
Cron jobs don't run shell startup scripts like
.profile or .bashrc, if that's where you normally set the variable.– Barmar
Mar 9 at 0:45
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
1
1
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0:59
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0:59
1
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 9 at 9:27
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 9 at 9:27
|
show 2 more comments
1 Answer
1
active
oldest
votes
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.
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 9 at 9: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/.envand thensource /.envat the top of your bash script.
– lyte
Mar 9 at 21:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072878%2frunning-bash-script-from-cron-job%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 9 at 9: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/.envand thensource /.envat the top of your bash script.
– lyte
Mar 9 at 21:32
add a comment |
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.
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 9 at 9: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/.envand thensource /.envat the top of your bash script.
– lyte
Mar 9 at 21:32
add a comment |
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.
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.
answered Mar 9 at 1:03
lytelyte
79869
79869
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 9 at 9: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/.envand thensource /.envat the top of your bash script.
– lyte
Mar 9 at 21:32
add a comment |
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 9 at 9: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/.envand thensource /.envat the top of your bash script.
– lyte
Mar 9 at 21:32
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 9 at 9:28
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 9 at 9: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 9 at 21:32
@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 9 at 21:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55072878%2frunning-bash-script-from-cron-job%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
You never set
FOLDER_ID, where is it supposed to come from?– Barmar
Mar 9 at 0:44
2
Cron jobs don't run shell startup scripts like
.profileor.bashrc, if that's where you normally set the variable.– Barmar
Mar 9 at 0:45
You could set environment variables in the crontab file, as this answer says.
– Stephen P
Mar 9 at 0:48
1
Unix & Linux would probably be a better place to ask how to make an environment variable global in a docker container.
– Barmar
Mar 9 at 0: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 9 at 9:27