I have a script which I call every 10 minutes with a cronjob:
*/10 * * * * ~/mydirectory/myscript.sh
Now, inside ~/mydirectory
there is also another script, let's say myotherscript.sh
which I'd like to call from inside myscript.sh
. Both scripts have been chmod
'ed to be executable and when I execute myscript.sh
from the command line inside ~/mydirectory
everything works fine. I execute it as follows:
. myscript.sh
But it doesn't look like the cronjob works. When I remove the reference to the second script from inside the primary script, it works though so I suspect there is something wrong with the way I reference the second script. This is what the contents of myscript.sh
looks like:
#! /bin/bash
do-something
do-something-else
. myotherscript.sh
Could it be that, when the cronjob runs, the current directory is not ~/mydirectory
and so the cronjob can't find the myotherscript.sh
file? If so, how to I get it to see the file? I don't want to specify the absolute path inside the primary script as I might want to move it (and myotherscript.sh
) to another directory at some point or I might want to rename ~/mydirectory
at some point and I don't then want to have to change the contents of myscript.sh
to reference the new absolute path.
BONUS QUESTION: Right now, when it's time for the cronjob to execute I basically just hold thumbs and hope for the best but I have no way of seeing whether it was successful and if it failed, why it failed. Any tips on how I can see why the cronjob didn't execute as expected?
Best Answer
OK, so glenn jackman's answer works and it also answered my bonus question but I have since figured out another and what I believe to be more elegant way of making sure the cronjob runs in the directory in which the scripts are located.
Simply by replacing
with
And that solves the problem. Everything works.