user1631261
user1631261

Reputation: 5

Having issues executing script via cron job

Following script when executed directly on the shell works fine but when executed via a cronjob is erroring out.

bash-3.00$ cat scr.sh 
#!/usr/local/bin/bash
DATE=$(date +%g%m%e)
INPUT_FILES=$DATE"_*.txt"
ALL_FILES=$DATE"*.txt"
echo INPUT=$INPUT_FILES
echo FILES=$ALL_FILES
for i in $(ls $INPUT_FILES); do echo $i; done

When directly executing works fine.

When installing the same via crontab and executing I get the following error message:

INPUT=120828_*.txt
FILES=120828*.txt
ls: 120828_*.txt: No such file or directory

For some reason the ls in the for loop is not able to interpret the embedded '*'.

Any ideas on what can be going wrong.

thanks for the help.

Upvotes: 0

Views: 63

Answers (1)

Rohan
Rohan

Reputation: 53386

Current working directory (CWD) is different when you run from cron. In the script you may want to first cd into the directory.

Upvotes: 3

Related Questions