SCRIPT WORKING PERFECTLY FROM COMMAND LINE BUT FAILING WITH CRONTAB
I was writing small bash script to take backup of a application configuration. It was tested from command line and everything looks fine. Later, I had setup a cronjob to run it at a scheduled time. But there was no backup. One of the common reasons for this is when we use relative paths in the script. crontab default working directory is user’s home directory. If scripts have relative paths, it will not be able to find right files and directories. I checked if there were any such relative paths but nothing was found.
To troubleshoot the issue, placed echo almost after every single statement. After analyzing these echo outputs, I suspected that crontab is not able to find one of the commands used in the script. But this utility was already installed and got confirmation by using “which” command. That means that PATH variable used by crontab is different from regular login shell. Got it confirmed by printing PATH variable. To fix the issue, modified PATH variable in the script to include the utility dir.
Later, I was wondering if there was any better way of fixing this instead of adding numerous echo statements. It turns out that, after little bit of web search, all the crontab jobs runtime logs will be written to /var/spool/mail/{username} file. I opened the file and the error was clearly written:
backup.sh: line 17: kubectl: command not found
Comments
Post a Comment