0

I have a crontab entry that will run a 'worker' script hourly. Within the 'worker' script I have multiple paths to other scripts like so;

#!/bin/bash
clear
echo "project-worker"
echo "Script to run checks on all jobs!"
source /home/user/project/project-jobs/cleanuptest1/project-mon-cleanuptest1.sh
source /home/user/project/project-jobs/sedtest1/project-mon-sedtest1.sh
jobslot=empty
exit

My issue is it only seems to be running the first script (cleanuptest1.sh) and ignoring the rest. Can anyone see where I'm going wrong at all? I read to call other scripts from within a script I should use source but is where I'm going wrong, it can't be used for multiple instances?

Many thanks in adavance!

5
  • 1
    Why are you using source to run the scripts? Unless you need environment variables from those scripts in the main one, best practice would dictate that you use . instead. Commented Oct 24, 2016 at 17:32
  • 2
    If you do need to use source/., you need to make sure the script isn't calling exit. Commented Oct 24, 2016 at 17:40
  • Thanks guys for you responses - @chepner - should I remove exit from script? Do you think it will be exiting the shell before the scripts finish? Just curious why I should remove it? Commented Oct 24, 2016 at 21:59
  • @keefer, imagine it as literally copying the subscript and pasting it inside the caller script substituting the source call. Commented Oct 25, 2016 at 11:28
  • @xvan Ahhhh - get you- thank you for clarifying. Commented Oct 26, 2016 at 15:40

1 Answer 1

3

Source should only be used if you need to preserve the called script environment on the callee.

If that's not the case you can execute the subscripts in subshells.

#!/bin/bash
clear
echo "project-worker"
echo "Script to run checks on all jobs!"
bash /home/user/project/project-jobs/cleanuptest1/project-mon-cleanuptest1.sh
bash /home/user/project/project-jobs/sedtest1/project-mon-sedtest1.sh
jobslot=empty
exit
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.