crontab won't run from root

Hi,
I'm on 10.3.9 with a crontab issue.
It seems crontab won't run from root. The crontab is set to run a test script every hour, 00 * * * * /Users/station3/bashscripts/cronscript1
but it won't. I can run the script from the terminal while logged in as root just fine so i dont think it's a path to the script or permission. I get no errors either, it just doesnt run.
I can run the script from the crontab of an admin user just fine.
Any ideas?

G5, Mac OS X (10.3.9)

Posted on Dec 15, 2005 12:04 PM

Reply
8 replies

Dec 15, 2005 12:52 PM in response to greg humphrey

Not sure what you mean by "from root". Do you mean the root user's crontab or the crontab file in /etc? If you mean the root user's crontab, does the root user have any mail? (Type "from" at your shell prompt, or "mail" to read it). If you mean the crontab file in /etc, then note that the format of the file is not the same, the sixth field is the user to run the command in the seventh field as.

(I succesfully ran a test crontab file as the root user containing
* * * * * touch /tmp/testfileXXX
)

Dec 15, 2005 4:06 PM in response to jarik

I actually made a typo that solved my problem, but created many others.
First to clarify, when i say from root, I mean "superuser". I'll type in sudo -s and get a prompt for my password, which Ill enter. i'll then have the root# prompt.
In my original post I typed
00 * * * * /Users/station3/bashscripts/cronscript1
when the actual path i was using was
00 * * * * ~/bashscripts/cronscript1
Those paths should be equivalent because /Users/station3 = ~.
When each configuration is run from the terminal, I'll get a sucessful run.
However when I use the ~ in the path and the script is run form the crontab, the script DOESNT run at all. It DOES run from the crontab when I put the full path in.
How are running a script from the crontab and from the terminal different?

G5 Mac OS X (10.3.9)
Thanks!

Dec 15, 2005 4:31 PM in response to greg humphrey

The environment for cron isn't the same as the environment for a login. You can prove this with a little test script:

#!/bin/bash

printenv > /var/root/Desktop/cron.out

exit 0


Run the script as a cron job and then run it from terminal and compare the outputs. You may also want to run it as an at job and from X11, too.

Roger

Dec 15, 2005 5:08 PM in response to Community User

ok, ran your script as a cron job in the cron tab, nothing happened. ran it from the terminal command line and got permission denied.
I can see that yes, when i run cron it's in a different environment, but which one? and why does it change my file permissions on SOME files?
For example when I run this from the terminal, it works fine, I can still access all my files.

cp -R -f /Users/station3/GregsWerk /Users/station3/gregsecretbackup

HOWEVER!
When I run it from the crontab, several, but not all subfolders have had thier USER permissions changed to "system" and I cannot access them.
How do I get around that?

Dec 15, 2005 8:01 PM in response to greg humphrey

I can see that yes, when i run cron it's in a
different environment, but which one?


It uses the root environment. When you use sudo -s, you're getting root privileges, but keeping your regular login environment. If you want to truly become "root" with all the , you'll need to use sudo su -. That'll give you root's environment.

The simplest way to add an environment variable to a crontab is to just stick it at the top of the crontab. I always add my email address to my crontabs:

MAILTO='username@address.com'

That makes it easier to control where I get the email sent if there's output of some kind. You can also add the usual ".bashrc" and ".bash_profile" files to root's home directory at /var/root, but I'm not certain how to make cron see those without rebooting the computer. Read the bash man pages, particularly the invocation section for more information.

When I run it from the crontab, several, but not all
subfolders have had thier USER permissions changed to
"system" and I cannot access them.
How do I get around that?


Try using the -p switch with cp. That's alleged by the cp man page to preserve user ID, permissions, mod time, etc.

charlie

Dec 16, 2005 9:49 AM in response to Charles Minow

Ah, it's -p option that i wasnt using. I saw that in the manul but misread what it was for, thanks for clearing that up. When i added the -p option, i didnt get the weird permission changes. So, the sucessful backup script I run from the root's crontab.

rm -r /Users/station3/gregsecretbackup/GregsWerk
cp -R -f -p /Users/station3/GregsWerk /Users/station3/gregsecretbackup
chown -R station3 /Users/station3/gregsecretbackup/
chmod -R a+rw /Users/station3/gregsecretbackup/

I get no weird errors or permission changes, things just copy at the specified time. YAY!
Thanks!

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

crontab won't run from root

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.