If you want to run a script that lies in the current working directory of the process invoking it, just use ./that-script
.
"$(pwd)/that-script"
in POSIX-like shells (and cron
should invoke a POSIX-like shell to parse the commands lines) is a poorer version of "$PWD/that-script"
. Poorer because it has to run an extra process and also because $(...)
strips all trailing newline characters, so it won't work if the path to the current working directory stored in $PWD
ends in newline characters.
The difference between ./that-script
and "$PWD/that-script"
(which will be expanded to /path/to/working/directory/that-script
) is that the former is a relative path and the latter in an absolute path. In either case, if that-script
is not to be found in the current working directory of the shell process that cron
invokes to parse that command line, that will fail.
By default jobs in your crontab
are generally started in your home directory.
If that-script
is not in that directory, you'd use /actual/path/to/that-script
. If it were also important that the script be run with its current working also directory being /actual/path/to
, you'd do:
cd /actual/path/to && ./that-script
Or:
cd /actual/path/to && "$PWD/that-script"
If you want to run that-script
from another-script
and both scripts reside in the same directory. You can generally derive that directory from the value of $0
in other-script
.
For instance, if other-script
is a zsh
script:
bash -- $0:h/that-script
If not and assuming $0
doesn't end in newline characters:
bash -- "$(dirname -- "$0")/that-script"
The dirname of $0
may be a relative path. To make it an absolute path, you can do, in zsh:
bash -- $0:P:h/that-script
Other shells:
dir=$(CDPATH= cd -P -- "$(dirname -- "$0")" && pwd -P) || exit
bash -- "$dir/that-script"
cd
to change to that directory. I'm just guessing that this is the issue, so I'm not turning this into a real answer. – Kusalananda Nov 18 '22 at 12:56$(pwd)
, why would you need this to be dynamic instead of using the actual path? – terdon Nov 18 '22 at 14:31Next time, if the full absolute path is different, I will have to modify the script. If I forget to modify it, it will fail. – Matthew Wai Nov 19 '22 at 03:17