-   Linux - General (
-   -   Root crontab running script, log re-directs fail. Explanation needed. (

mattst 10-27-2011 07:22 AM

Root crontab running script, log re-directs fail. Explanation needed.

Recently I wrote a script to check when a product was available on a web site - basically: wget web page, grep for 'out of stock', if text not found email me to let me know.

Being new to crontabs I did not realize users had their own, I only knew about root's crontab. So I added a line to root's crontab to run my script every hour during office hours. The script ran fine except the logging did not take place - here's what happened:


# snip...


DateTimeStampStart=`date +%Y-%m-%d-%H-%M`

echo "Starting $ScriptName at: $DateTimeStampStart" >> $LogFile
echo "Wget url: $UrlAddress" >> $LogFile
echo "Saving url as: $SaveUrlAs" >> $LogFile

# ...snip

Both the log file and the script where owned by my username, not by root, but despite using the full path for the log file no log entries were made in it at all (despite all aspects of my code making log entries as demonstrated by the examples above).

When I realized each user could have their own crontab, I deleted the line which ran my script from root's crontab, and added it to my username's crontab instead. At this point the logging all worked perfectly. Problem solved.

However -I'm always trying to further my understanding of Linux- I would like to understand why the logging did not work when the script was run from root's crontab. Can someone explain why to me please.

Many thanks.

digitaldude 10-27-2011 08:18 AM

The user should have write access to the logs.

catkin 10-27-2011 08:22 AM

The redirection looks OK (and running the same script as your username proves it) as long as the log file was writeable for root ... was it?

mattst 10-27-2011 09:00 AM

Thanks guys.

I think not - I thought root had 'absolute power' and could read/write/exe to every file - I take it that is not the case then?!

Since the machine I am using is a one user PC, would it be sensible to add root to my group (my group name is the same as my username)? That way in future I can allow root access to a file with:


chmod ug+rwx file

catkin 10-27-2011 10:17 AM

Or, assuming there is a root group and root is in it, you could (as root) change the group of the file to root:

chgrp root /home/user/Scripts/logs/check-web-page-for-text.log
chmod g+w /home/user/Scripts/logs/check-web-page-for-text.log

mattst 10-27-2011 10:25 AM

Thanks catkin.

I didn't realize that a file's group could be a group that the file owner is not a member of.


All times are GMT -5. The time now is 01:46 PM.