OK, My Bad… cron Starts in $HOME
This morning I got news that one of my cron jobs was behaving badly - and by badly I mean very badly. The script was built to clear out the logs of some very verbose processes that are really not very necessary to monitor, but if they get too big and can't be written to, will crash the processes. And those processes are important.
The problem was that when running the script I made the assumption that I was in the directory I was in when I ran it manually. This was a big mistake and all the scripts I typically create have at the top:
# # Now let's get right into it... # program=`basename $0` dir=`dirname $0` cd $dir echo 'Running '${program}' from '${dir}
so that we get into the directory of the script and then all the moves are relative to that directory. The problem was that this was clearing log files but the names of the log files weren't specific. This means that without this code, the directories I tried to clear weren't there, and then without sufficient checks to make sure they were there, I started clearing out all the files from the $HOME. Not good.
Thankfully, we have .snapshots of all the directories so that everything I cleared out was easily restored. But I needed to fix things so it didn't happen next week. In addition to the code (above) I also added in checking code for each directory so that I know the directory exists and don't get into the situation where I think I cd somewhere, and don't really move at all.
It was funny, in a way. It's been a very long time - about 15 years, the last time I remember making a script that wiped out data accidentally. Then, too, I was able to get it back from backups, but it was still a shock those first twenty seconds when it was gone and there was no 'undo' to easily bring it back. Everyone makes mistakes, me too. Live, learn, and code better next time.