Operating System - Linux
1753846 Members
7944 Online
108807 Solutions
New Discussion юеВ

Write to a log file in RHAS Linux 3.0

 
SOLVED
Go to solution
Jorge Cocomess
Super Advisor

Write to a log file in RHAS Linux 3.0

Greetings,

I have a cron job that goes out and modify the permission (chmod 666) on a text file once day. I would like to know how I can capture the changes in a log file? Or logs the event during as part of the cron job via e-mail.

What would you Linux expert do in my case??

Thank you in advance.

Jorge C.
12 REPLIES 12
Steven E. Protter
Exalted Contributor
Solution

Re: Write to a log file in RHAS Linux 3.0

Shalom Jorge,

One way is to redirect the output of the cron job.

jobname | mail -s "my cron job" yourname@yourdoiman.com

That will send the output of the job to the email address of your choice.

Or you could jobname >> logfile sending all output of the cron job to a logfile name of your choice.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Heironimus
Honored Contributor

Re: Write to a log file in RHAS Linux 3.0

cron will normally only send mail if the command generates output, which chmod will only do if there's an error. The fact that the job ran should already show up in /var/log/cron. If you want additional logging or e-mail notification you should probably write a script that runs chmod and sends mail or writes a logfile.
Jorge Cocomess
Super Advisor

Re: Write to a log file in RHAS Linux 3.0

I tried writing to a log file >> chmod.log but no output.

Heironimus - How would I began to write this script that will displayed the output to a log file? Do you have examples of these log files?

Thanks,

Jorge
skt_skt
Honored Contributor

Re: Write to a log file in RHAS Linux 3.0

cron log can be monirored from /var/log/cron
Ivan Ferreira
Honored Contributor

Re: Write to a log file in RHAS Linux 3.0

You have the following options:

Use the MAILTO option in crontab.
Redirect the output to a logfile, for example

5 1 * * * job.sh > /tmp/job.log 2>&1

This will redirect the standard output and error to the job.log file.

Just remember that chmod command does not provides any output.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Jorge Cocomess
Super Advisor

Re: Write to a log file in RHAS Linux 3.0

Ivan - I did what you've suggested and I am still not getting any output in the log file.

I checked and triple checked the syntax and it was correct.

Any ideas?

Thank you.

Jorge
skt_skt
Honored Contributor

Re: Write to a log file in RHAS Linux 3.0

chmod command do not create any output.That is why you are getting none on the output file.

chmod 644 test786;ll test786 >test786.out

cat test786.out
-rw-r--r-- 1 root root 384 Jun 4 15:52 test786
Jorge Cocomess
Super Advisor

Re: Write to a log file in RHAS Linux 3.0

I'm confused.

If chmod command does not create output, than how did you managed to create an output with your test??

Am i missing something here?? I just need to know if my script ran successfully.

Thanks,
Jorge
Vitaly Karasik_1
Honored Contributor

Re: Write to a log file in RHAS Linux 3.0

>I just need to know if my script ran successfully.

you can check this using one of two ways:
1) ls -l your_file
you should see proper permission

2) echo $?
you should see 0 - exit code of chown