Just keep in mind you need to check if the directory exist before you cd from a script and also exit immediately if the directory does not exist. Why? just a simple cd will bring you to “$HOME” in this case /root. It is not always intended/accepted specially if it involve rm ;).
With an existing directory it is fine. (or variable is not empty)
jetchisel@Localhost:~> cd "$mydir"
Without an existing directory it is not. (or variable is empty)
jetchisel@Localhost:/tmp> cd "$mydir"
Just imagine running an rm after the cd, then the next step would be restoring your “$HOME” directory from backup.
Or just create it if it doesn’t exists, probably with “mkdir -p /path/to/directory”.
No need to exit immediately in this case.
If you don’t check for the existence and try to cd to a non-existing directory, the cd will fail and your script will immediately abort, anyway.
Why? just a simple cd will bring you to “$HOME” in this case /root.
True, but if you run something like “cd /path/to/directory” this can not happen, as you do not just call cd. So I don’t see the point in mentioning this here.
You’re thinking of using a variable that might be empty, right?
Well, if you use a variable, then do not forget to set it to the directory you want to cd to, yes.
In this case even with an empty variable nothing bad will happen though, just the files will end up in /root, which might even be intended.
Thanks all, cron was working. I assumed the file creation was happening in the location of the script, but it was happening in ‘root’ user profile. Adding the cd command in the script has fixed the issue, sorry to have troubled so many people!
Well, but at the moment the question is why the script/cronjob doesn’t create the files. No need for a lecture about how to check whether a directory exists and only execute the code if it does at this point, IMHO.
Right now, it’s better to just cd to an existing directory, and see whether the files are there afterwards.
By doing it in a more complicated way like you describe, you only increase the chances that some other errors creep in that prevent the script from working as intended.
Makes it even more complicated to find out why it doesn’t work then.
On 2014-09-08 13:06, poobear85 wrote:
> robin_listas;2663426 Wrote:
>> On 2014-09-08 12:16, poobear85 wrote:
>>> Is there a log file I can review to see why this failed?
>> And mail.
> The script has run the commands, so the schedule is working but it
> hasn’t created the file expected, would anyone be able to help?
Then read your system mail! :-))
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)