bash script runs different when cron executed
I'm fairly new to cron job, but I have put together a bash script.
When running the bash script itself, it operates as expected. However, when running from cron daemon, it runs a defunct and if output is redirected to an error log I receive: ls: *.tar.gz: No such file or directory Here is the contents of the bash script I am trying to use: Code:
root@linux [/cpbackup/daily]# cat cpanel2mosso.sh http://www.gorillasmashserver.com/node/11 Please let me know if I am missing something simple, or complex... Thanks! Jack |
When in cron, always give fullpaths to cmds, as cron's $PATH is minimal, so /bin/date instead of date, /bin/egrep instead of egrep, etc.
Also, a cron job has no default dir; you need to cd into the correct dir first. |
obviously you script wants to list all tar files in a directory (but which one???) First cd into that directory then do your for each in ....
If you are trying to get all tar.gz files on your machine, you can possibly use find |
Hello,
Thank you both for catching that ! :) Wish I caught that before posting :o Thanks, Jack |
jalder,
don't worry about it :) There are always tricks to things we are new to. I'll give you a few pointers on how I write automated scripts (many will think some of it too much, but I don't think there is such a thing). I take chrism's "full path" suggestion a little further among other things. First thing in the script, I identify the logfile: Code:
#!/bin/bash Second, I create a "logEntry" function that basically just dumps out a date-prepended log entry. Very nice and handy for when you need to know specifics about when the log entries were written: Code:
function logEntry() Code:
logEntry "This thing doesn't work at all" Code:
2009-02-20 10:48: This thing doesn't work at all Code:
function findWhich() Next, I'm going to put variable declaractions for every binary I use in the script. However, I want to have a quick and easy way to check whether these assignments worked or failed, so I write a quick function just for testing the return values of these: Code:
function canWeFindIt() Code:
findWhich Next, I mark the main part of the script, just for fast visual reference: Code:
################################ main Code:
$PHP ./script.php >> /dev/null 2>&1 Here is an example: If you issue the "which ls" command, it will echo something like "/bin/ls". If you add the >> /dev/null, as "which ls >> /dev/null", it will return nothing. But, if you change ls to something random letter combo that is definitely not a binary, like "which fdsasdfdsas >> /dev/null", notice now we DO have output, and it was not dumped into /dev/null. Because it is stderr output. If you add our handy dandy output stream redirection: "which fdsasdfdsas >> /dev/null 2>&1" Now nothing. Very nice to keep unwanted output from log files and such when doing script automation. Anyway, I hope these things help you and if not, maybe I taught you something. If not, maybe I'll teach something to someone else who reads this. :) |
All times are GMT -5. The time now is 07:49 PM. |