Why are you using “sudo”?
The script is run as root anyway, no?
And you should “cd” to the directory whery you want to create the file, as someone else mentioned while I wrote this…
The file probably got created somewhere where you did not expect it…
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:~> mydir=/tmp
jetchisel@Localhost:~> cd "$mydir"
jetchisel@Localhost:/tmp>
Without an existing directory it is not. (or variable is empty)
jetchisel@Localhost:/tmp> mydir=
jetchisel@Localhost:/tmp> cd "$mydir"
jetchisel@Localhost:~>
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.
Between you and me, i don’t think i need to explain everything like i always did here in the forum but i always assume the OP needs some explanation even if it means complicated to others
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?
>>
>> /var/log/messages
>>
>> 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)
On 2014-09-08 12:16, poobear85 wrote:
>
> Hi all,
>
> I am having an issue with my cron jobs not running, so my /etc/crontab
> looks like this:
…
> This hasn’t run for the last three days, the script does a SQL backup,
> so what I done was created a cron job like the below:
>
> 00 09 * * * env > /tmp/env.output
If that line is in “/etc/crontab”, it is trying to run as user “env” the
file “/tmp/env.output”, which is probably not what you want.
Unless the above is in a user crontab - but you have not said so.
–
Cheers / Saludos,
Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)