unable to run bash script from bash script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















I've created a script that has to be executed on boot and has to run another script (which sets some variables) before executing other commands.



After writing the script, I've successfully executed it in the console, but if the script is added to crontab, the other script inside this one is not executed.



The script (main-script.sh) that has been added to crontab is the following:



#!/bin/bash
source /home/pi/test-script.sh
python mycustompythonscript.py


and the test-script.sh is the following (I replaced the actual script with this one for testing):



#!/bin/bash
echo "greetings from test script" > test.txt


So when i run main-script.sh from console, test-script.sh is executed (the file is created) and the python script is executed. Upon rebooting the RPi, however, the python script is executed, but the test-script.sh is not executed (test.txt file is not created).



Does anyone know what could be the possible reason that test-script.sh is not executed in case the main-script.sh is executed by cron?










share|improve this question

















  • 1





    Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

    – Sergiy Kolodyazhnyy
    Feb 23 at 9:16











  • You can set SHELL=/bin/bash in your crontab which makes life easier.

    – Freddy
    Feb 23 at 9:27











  • I don't know how it finds the python script: You don't give an absolute path.

    – ctrl-alt-delor
    Feb 23 at 9:54











  • @SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

    – ctrl-alt-delor
    Feb 23 at 10:22











  • @ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

    – Sergiy Kolodyazhnyy
    Feb 23 at 10:30















1















I've created a script that has to be executed on boot and has to run another script (which sets some variables) before executing other commands.



After writing the script, I've successfully executed it in the console, but if the script is added to crontab, the other script inside this one is not executed.



The script (main-script.sh) that has been added to crontab is the following:



#!/bin/bash
source /home/pi/test-script.sh
python mycustompythonscript.py


and the test-script.sh is the following (I replaced the actual script with this one for testing):



#!/bin/bash
echo "greetings from test script" > test.txt


So when i run main-script.sh from console, test-script.sh is executed (the file is created) and the python script is executed. Upon rebooting the RPi, however, the python script is executed, but the test-script.sh is not executed (test.txt file is not created).



Does anyone know what could be the possible reason that test-script.sh is not executed in case the main-script.sh is executed by cron?










share|improve this question

















  • 1





    Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

    – Sergiy Kolodyazhnyy
    Feb 23 at 9:16











  • You can set SHELL=/bin/bash in your crontab which makes life easier.

    – Freddy
    Feb 23 at 9:27











  • I don't know how it finds the python script: You don't give an absolute path.

    – ctrl-alt-delor
    Feb 23 at 9:54











  • @SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

    – ctrl-alt-delor
    Feb 23 at 10:22











  • @ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

    – Sergiy Kolodyazhnyy
    Feb 23 at 10:30













1












1








1


0






I've created a script that has to be executed on boot and has to run another script (which sets some variables) before executing other commands.



After writing the script, I've successfully executed it in the console, but if the script is added to crontab, the other script inside this one is not executed.



The script (main-script.sh) that has been added to crontab is the following:



#!/bin/bash
source /home/pi/test-script.sh
python mycustompythonscript.py


and the test-script.sh is the following (I replaced the actual script with this one for testing):



#!/bin/bash
echo "greetings from test script" > test.txt


So when i run main-script.sh from console, test-script.sh is executed (the file is created) and the python script is executed. Upon rebooting the RPi, however, the python script is executed, but the test-script.sh is not executed (test.txt file is not created).



Does anyone know what could be the possible reason that test-script.sh is not executed in case the main-script.sh is executed by cron?










share|improve this question














I've created a script that has to be executed on boot and has to run another script (which sets some variables) before executing other commands.



After writing the script, I've successfully executed it in the console, but if the script is added to crontab, the other script inside this one is not executed.



The script (main-script.sh) that has been added to crontab is the following:



#!/bin/bash
source /home/pi/test-script.sh
python mycustompythonscript.py


and the test-script.sh is the following (I replaced the actual script with this one for testing):



#!/bin/bash
echo "greetings from test script" > test.txt


So when i run main-script.sh from console, test-script.sh is executed (the file is created) and the python script is executed. Upon rebooting the RPi, however, the python script is executed, but the test-script.sh is not executed (test.txt file is not created).



Does anyone know what could be the possible reason that test-script.sh is not executed in case the main-script.sh is executed by cron?







bash shell-script raspberry-pi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 23 at 8:58









Niko GamulinNiko Gamulin

1355




1355







  • 1





    Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

    – Sergiy Kolodyazhnyy
    Feb 23 at 9:16











  • You can set SHELL=/bin/bash in your crontab which makes life easier.

    – Freddy
    Feb 23 at 9:27











  • I don't know how it finds the python script: You don't give an absolute path.

    – ctrl-alt-delor
    Feb 23 at 9:54











  • @SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

    – ctrl-alt-delor
    Feb 23 at 10:22











  • @ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

    – Sergiy Kolodyazhnyy
    Feb 23 at 10:30












  • 1





    Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

    – Sergiy Kolodyazhnyy
    Feb 23 at 9:16











  • You can set SHELL=/bin/bash in your crontab which makes life easier.

    – Freddy
    Feb 23 at 9:27











  • I don't know how it finds the python script: You don't give an absolute path.

    – ctrl-alt-delor
    Feb 23 at 9:54











  • @SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

    – ctrl-alt-delor
    Feb 23 at 10:22











  • @ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

    – Sergiy Kolodyazhnyy
    Feb 23 at 10:30







1




1





Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

– Sergiy Kolodyazhnyy
Feb 23 at 9:16





Depending on how you're executing main-script.sh in crontab, it might be executed via /bin/sh instead of /bin/bash. In case of source command it's bash-specific. Try to replace that with . /home/pi/test-script.sh - that should work across the board on all POSIX compliant, bourne-like shells. Let me know if that works for you, and if it does I'll post it as a proper answer

– Sergiy Kolodyazhnyy
Feb 23 at 9:16













You can set SHELL=/bin/bash in your crontab which makes life easier.

– Freddy
Feb 23 at 9:27





You can set SHELL=/bin/bash in your crontab which makes life easier.

– Freddy
Feb 23 at 9:27













I don't know how it finds the python script: You don't give an absolute path.

– ctrl-alt-delor
Feb 23 at 9:54





I don't know how it finds the python script: You don't give an absolute path.

– ctrl-alt-delor
Feb 23 at 9:54













@SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

– ctrl-alt-delor
Feb 23 at 10:22





@SergiyKolodyazhnyy The script has a hash-bash-slash-bin-slash-bash #!/bin/bash, so a modern sh will launch bash.

– ctrl-alt-delor
Feb 23 at 10:22













@ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

– Sergiy Kolodyazhnyy
Feb 23 at 10:30





@ctrl-alt-delor As I mentioned in the comment, "Depending on how you're executing main-script.sh". A sh /path/to/main.sh definitely won't like bashisms (unless it's CentOS where /bin/sh is /bin/bash ).

– Sergiy Kolodyazhnyy
Feb 23 at 10:30










1 Answer
1






active

oldest

votes


















1














You need to specify an absolute path for test.txt. It won't put it where you expect.



do sudo find / -iname "test.txt" to find it.



(The python script should also have a full path name.)






share|improve this answer























  • python seems to be in /usr/bin, so it's found.

    – Freddy
    Feb 23 at 10:24






  • 1





    @Freddy Python script, not python interpreter.

    – ctrl-alt-delor
    Feb 23 at 10:25







  • 1





    Oops, you're right!

    – Freddy
    Feb 23 at 10:26











Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502481%2funable-to-run-bash-script-from-bash-script%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









1














You need to specify an absolute path for test.txt. It won't put it where you expect.



do sudo find / -iname "test.txt" to find it.



(The python script should also have a full path name.)






share|improve this answer























  • python seems to be in /usr/bin, so it's found.

    – Freddy
    Feb 23 at 10:24






  • 1





    @Freddy Python script, not python interpreter.

    – ctrl-alt-delor
    Feb 23 at 10:25







  • 1





    Oops, you're right!

    – Freddy
    Feb 23 at 10:26
















1














You need to specify an absolute path for test.txt. It won't put it where you expect.



do sudo find / -iname "test.txt" to find it.



(The python script should also have a full path name.)






share|improve this answer























  • python seems to be in /usr/bin, so it's found.

    – Freddy
    Feb 23 at 10:24






  • 1





    @Freddy Python script, not python interpreter.

    – ctrl-alt-delor
    Feb 23 at 10:25







  • 1





    Oops, you're right!

    – Freddy
    Feb 23 at 10:26














1












1








1







You need to specify an absolute path for test.txt. It won't put it where you expect.



do sudo find / -iname "test.txt" to find it.



(The python script should also have a full path name.)






share|improve this answer













You need to specify an absolute path for test.txt. It won't put it where you expect.



do sudo find / -iname "test.txt" to find it.



(The python script should also have a full path name.)







share|improve this answer












share|improve this answer



share|improve this answer










answered Feb 23 at 10:20









ctrl-alt-delorctrl-alt-delor

12k42561




12k42561












  • python seems to be in /usr/bin, so it's found.

    – Freddy
    Feb 23 at 10:24






  • 1





    @Freddy Python script, not python interpreter.

    – ctrl-alt-delor
    Feb 23 at 10:25







  • 1





    Oops, you're right!

    – Freddy
    Feb 23 at 10:26


















  • python seems to be in /usr/bin, so it's found.

    – Freddy
    Feb 23 at 10:24






  • 1





    @Freddy Python script, not python interpreter.

    – ctrl-alt-delor
    Feb 23 at 10:25







  • 1





    Oops, you're right!

    – Freddy
    Feb 23 at 10:26

















python seems to be in /usr/bin, so it's found.

– Freddy
Feb 23 at 10:24





python seems to be in /usr/bin, so it's found.

– Freddy
Feb 23 at 10:24




1




1





@Freddy Python script, not python interpreter.

– ctrl-alt-delor
Feb 23 at 10:25






@Freddy Python script, not python interpreter.

– ctrl-alt-delor
Feb 23 at 10:25





1




1





Oops, you're right!

– Freddy
Feb 23 at 10:26






Oops, you're right!

– Freddy
Feb 23 at 10:26


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


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




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f502481%2funable-to-run-bash-script-from-bash-script%23new-answer', 'question_page');

);

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






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?