Reputation: 29
I pass the file path, containing variables to be sourced, as an argument to my Bash script. The file is created on Windows, in case that makes any difference.
The following check is performed:
CONFIG_FILE=$1
if [[ -f ${CONFIG_FILE} ]]; then
echo "Is a file"
. ${CONFIG_FILE}
else
echo "Not a file"
fi
When I run the script manually, from the command line, the check is fine and the variables get sourced.
However, when I set up a Cron job using
*/1 * * * * /full/path/to/script.sh /full/path/to/configfile
I get "Not a file" printed out.
I attempted every single setup I found online to solve this:
I am setting up the crontab with the proper user under whom the script should be run. The user has access rights to the location of the files (as can be observed through running the script from the command line).
Looking for further advice on what can be attempted next.
Upvotes: 0
Views: 1457
Reputation: 1675
What you're doing here is (I think) making sure that there is a separate argument behind your /path/config-file
. Your original problem seems to be that on Unix your config file was stated as /path/config-file\r
(note the trailing \r). You are doing it by adding an argument -q\r
so that the config file itself is "clean" of the carriage return. You could add blabla\r
for that matter instead of -q\r
. Your script never interprets that extra argument; but if you put it on the cron line then your config file argument is "protected", because there's stuff following it, that's all.
What you also could do, is make sure that your cron defintion is Unix-styled (\n
terminted lines) instead of DOS styled (\r\n
terminated lines). There's probably a utility dos2unix
on your Unix box to accomplish that.
Or you could remove the crontab on Unix using crontab -r
and then re-create the crontab using crontab -e
. Just don't upload files that were created on MS-DOS (or derived).
Upvotes: 1
Reputation: 29
Found another attempt and it worked. I added the -q flag in the cronjob line.
*/1 * * * * /path/script.sh /path/config-file -q
Source: Cron Job error "Could not open input file"
Can someone please explain to me what does it do? I am not so literate in bash.
Upvotes: 0