Log error from cron job into log file

Hi

Please would it be right to log the errors from a script running in cron in the following manner:

/datax/vendor/dump.sh > /datax/vendor/err.log 2>&1

Anything you can do in a script that you run interactively you can do in a script run by cron. However, try to always use full pathnames since cron won't know about path parameters (unless you've set them within the script), and always set all variables within the script since cron won't know about such variables set in the likes of .profile

Otherwise, yes, no problem. Give it a test run.

It depends if the account you are running it as has permissions to write to (or create) the file. It will fail if you try to write to a directory.

If it's not working, do you have anything in the cron log file?

Robin

it has permissions, I am setting up now first time

Yes, thats OK ... but now how will you recognise a normal execution from a bad? You will always have to look at your log... I always separate them in cron if the job is important, <job>.log and <job>.err and if in a rush will look if .err is greater than 0...

@vbe......yes, that's a very good point.

Ok, @vbe, please can you share or give an example of the syntax?

cd /opt/HDVM/bin/; (./HiScan -s exoc:2005 -t /var/opt/HDVM/log/HiScan.msg >> /var/opt/HDVM/log/HiScan.log) \
 2> /var/opt/HDVM/log/HiScan.err

@vbe

correct if I am wrong, I just need to replace my original syntax with

/datax/vendor/dump.sh 2 > /datax/vendor/err.log 

to only log errors?

You are not interested by the output of dump.sh?

in my code

 2> /var/opt/HDVM/log/HiScan.err

this file should always be of 0 size, since it is for errors only the last execution is important (no need of history...) thats is why you have also the log file...

You also need to ensure that you have 2> rather than 2 > The former will redirect standard error, the latter will give the value 2 as a parameter and then redirect standard output.

Robin

:slight_smile: ... would it be

/datax/vendor/dump.sh >> /datax/vendor/dump.log \ 2> /datax/vendor/err.log

@fretagi.......the point that vbe made was that if you mix the normal output of the script with the error output of the script, then errors are not easily spotted because you have to read the whole damned thing. Therefore, vbe recommends sending the normal output to one file, and error output to another file.

Sorry to intrude.

:b::slight_smile:
You dont need the backslash if it holds nicely on one line...

1 Like

@hicksd8
I understood exactly what he meant, that is why my last command, perhaps you did not see.

@vbe

So I should remove

\

If its a one liner that holds on one line then yes...

I usually use backslahes because I limit to around 80 char the lengh of a line in my crontabs for ease of reading...

1 Like