The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   cron /bin/sh: root: command not found (http://hintsforums.macworld.com/showthread.php?t=4706)

bluehz 08-15-2002 10:07 AM

cron /bin/sh: root: command not found
 
I thoroughly understand the cron process and how to right cront tasks but I do not understand why the tasks always fail with:

cron /bin/sh: root: command not found

Its the user "root" that is failing. If you remove the user field the script runs fine - but that makes no sense.

For example - from the system crontab:

51 9 * * * root periodic daily

This is a straight up stock entry - should not fail. But whenever it attempts to run - instead of completing successefully it always generates the :

cron /bin/sh: root: command not found

Anybody have any suggestions?

osxpez 08-15-2002 10:14 AM

I don't know about BSD. But in Linux crontab i don't recall any user field. The error message also suggests that cron expects the command to execute in the sixth field.

Try: man crontab -S 5
And see what it says about the fields cron expects. I would check myself, but I'm not anywhere near an OS X machine. (Which is a bit painful for an OS X junkie like myself. =)

bluehz 08-15-2002 10:16 AM

Exactly - I use a Linux box on my LAN for some stuff and it does not have a USER field. But all the stock - preinstalled OS X crontab's do.

osxpez 08-15-2002 10:28 AM

I agree, it makes little sense. You haven't accidently installed a more Linux-like cron that happens to be run instead of the OS X one?

bluehz 08-15-2002 10:39 AM

not that I know of...wonder of Fink has its own cron....I'll have to check into that - sometimes /sw/bin stuff can get in the way of legitimate binaries...

pmccann 08-15-2002 10:42 AM

Dunno if this is your problem, but the distinction you're butting up against here is (I think) present in all cron implementations. Users don't get to specify the user (so there's no user field in users' crontabs) but the root user (being the great superstar that he or she is) gets to pick the user that the job runs under, and hence has a different format for the crontab. In particular, they get one extra field to fill in.

So my guess is that you're editing your own crontab in such a manner that the cron daemon thinks you want to execute the "root" command. You'll have to edit root's crontab (ie /etc/crontab) if you're trying to get a command to run "as" root.
Or just rip out the root and have the next command in that line execute as your poor old mortal self.

Then again I might be on drugs.

Cheers,
Paul

mervTormel 08-15-2002 10:50 AM

note that the format of /etc/crontab and users % crontab -l are differrent by the user column...
Code:

$ less /etc/crontab | fold -sw 60
#      $NetBSD: crontab,v 1.13 1997/10/26 13:36:31 lukem
Exp $
#
# /etc/crontab - root's crontab
#
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log

#min    hour    mday    month  wday    user    command

# Disabled by default, since it causes disk access every 10
minutes,
# which is useless unless you use at(1).  Enable as needed.
#*/10  *      *      *      *      root
/usr/libexec/atrun

# do daily/weekly/monthly maintenance
#15    3      *      *      *      root    sh
/etc/daily      2>&1 | tee /var/log/daily.out  | mail -s
"`hostname` daily output"  root
#30    4      *      *      6      root    sh
/etc/weekly    2>&1 | tee /var/log/weekly.out  | mail -s
"`hostname` weekly output"  root
#30    5      1      *      *      root    sh
/etc/monthly 2>&1 | tee /var/log/monthly.out | mail -s
"`hostname` monthly output" root
#10      *      *      *      *      root   
/sw/sbin/anacron -s

$ crontab -l | fold -sw 60

MAILTO=merv

#min    hour    mday    month  wday    command

#*/51      *      *      *      *     
/Users/merv/bin/zgetip | mail -s "zgetip" merv

#*/2 * * * * /usr/bin/curl 'ftp.mindspring.com/~xxxx/' |
tee /tmp/curl.log

#*/2    *      *      *      *      curl
'www.mindspring.com/~xxxx/' | tee /tmp/curl.log

#*/2    *      *      *      *      sh
/Users/merv/work/add.sh >> /tmp/501/foo.log 2>&1

stomp! paul said it well. my illustration is very ugly, but, for your personal crontabs, you remove the user column. even if you % sudo crontab -u root -e

paul may still be on drugs.

bluehz 08-15-2002 10:52 AM

pmccann - actually its running inthe system crontab - which does require root capabilities

mervTormel - very interesting! What should I ascertain from that info?

mervTormel 08-15-2002 10:55 AM

Quote:

Originally posted by bluehz
pmccann - actually its running inthe system crontab - which does require root capabilities

mervTormel - very interesting! What should I ascertain from that info?

too many tabs in your /etc/crontab line?

mervTormel 08-15-2002 10:58 AM

try this to examine the tabulation of your crontab file...
Code:

$ cat -vt /etc/crontab

#      $NetBSD: crontab,v 1.13 1997/10/26 13:36:31 lukem Exp $
#
# /etc/crontab - root's crontab
#
SHELL=/bin/sh
PATH=/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log

#min^Ihour^Imday^Imonth^Iwday^Iuser^Icommand

# Disabled by default, since it causes disk access every 10 minutes,
# which is useless unless you use at(1).  Enable as needed.
#*/10^I*^I*^I*^I*^Iroot^I/usr/libexec/atrun
...

^I = tab

bluehz 08-15-2002 11:00 AM

SHELL=/bin/sh
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin
HOME=/var/log
19^I10^I*^I*^I*^Iroot^Iperiodic daily
0^I3^I*^I*^I6^Iroot^Iperiodic weekly
0^I3^I1^I*^I*^Iroot^Iperiodic monthly
15^I3^I*^I*^I*^Iroot^I/usr/local/bin/psync -d /Volumes/HD1 /Volumes/HD2

mervTormel 08-15-2002 11:12 AM

hmm, why is etc in the path? where is periodic?

what happens if you remove etc: from path and put full path to periodic for the commands

bluehz 08-15-2002 11:13 AM

Not sure why etc is in path.

Already tried including full path to periodic: /usr/sbin/periodic - still no go.

Removing /etc from path has no effect.

mervTormel 08-15-2002 11:37 AM

hmm, still haven't seen evidence that the cron entries you are showing us are from your /etc/crontab file.

is that the case? or are you showing us the results of % sudo crontab -u root -l ?

bluehz 08-15-2002 11:40 AM

straight out of pico /etc/crontab

pmccann 08-15-2002 11:48 AM

OK, so what's periodic? : I don't have no stinkin' periodic.

I guess it's just an "all-in-one" that takes daily/weekly/monthly as a parameter, but where's it from and what's in it. The 'root' aspect might be a red herring; could it be that periodic can't be found/executed? Is there maybe a #! line at the start of periodic that's inappropriate for osx?

Thus endeth this portion of the interrogation.

Cheers,
Paul

mervTormel 08-15-2002 11:54 AM

okay. then what does

% sudo crontab -u root -l

say?

does your psync work at 3:15 am?

where did you get this /etc/crontab? it is not the OSX dist'd one.

where is periodic from? it is not OSX dist'd, and i'm surprised that it's in /usr/sbin/

looks like a FreeBSD dist of periodic ? man, there's a lot of bug reports about that thing.

anyhow...

bluehz 08-15-2002 12:03 PM

Periodic is simply a shell script that looks for a folder of scripts and runs the contents of that folder. Common on Linux setups (although on Linux I believe it is called "run-parts") - so instead of calling the daily maintenace type stuff individually - you just call Periodic - which in turn looks for a specific folder. Place the other scripts you want to run within that folder and they are exe by the Periodic script.

#!/bin/sh -
#
# $FreeBSD: src/usr.sbin/periodic/periodic.sh,v 1.9.2.7 2000/11/26 06:06:18 kris Exp $
#
# Run nightly periodic scripts
#
# usage: periodic { daily | weekly | monthly } - run standard periodic scripts
# periodic /absolute/path/to/directory - run periodic scripts in dir

bluehz 08-15-2002 12:06 PM

Actually this is/will be standard dist OSX crontab - I can say nothing more than that ...

Strangely enough the psync DOES work....and it has the root user listed. But the root problem has plagued me for some time. This is just an example today...I finally got around to posting about it.

Quote:

Originally posted by mervTormel

does your psync work at 3:15 am?

where did you get this /etc/crontab? it is not the OSX dist'd one.

Kemul 08-15-2002 01:02 PM

Umm... these are my guesses:
[list=1][*]Is #!/bin/sh - (with the - sign) valid for a shell script?[*]Is periodic executable? (chmod 755)[/list=1]

bluehz 08-15-2002 01:14 PM

#!/bin/sh -

does work - although I do not know what the - is for.

periodic is set:

-r-xr-xr-x 1 root wheel 2875 Jul 14 16:57 /usr/sbin/periodic

Also - I manually called:

periodic daily (as root) and that worked fine

I also individually ran the two script that periodic daily runs and they both ran fine.

Very odd indeed....I thought for sure this was an easy one....for someone with more knowledge that me in shell scripting.

mervTormel 08-15-2002 01:19 PM

how about pointing us to where you got your periodic from and we could try to duplicate your problem? it is a head scratcher.

sao 08-15-2002 03:01 PM

bluehz,

From what I know, /etc/crontab is not the root users crontab, and thus behaves a little differently than a user's crontab. When using /etc/crontab, you have to specify the user the operation should run as.

It should then honor the variables defined in the config file.

Have you tried to make an entry something like:

51 *9 * * * root /usr/sbin/periodic -u xxx.xxx.x.x


Also, if you do crontab /etc/crontab and you fill roots' mailbox with "not found messages", the thing to do then, would be to login as root and crontab -r should do the trick.


Cheers...

sf_talkative 07-19-2006 09:35 PM

This is an old thread, but for those who may stumble on this:

/etc/crontab is different from user crontabs. Former has a field to specify the username to run command as, latter does not.

Now, even root has a "user" crontab. This is different from /etc/crontab.

It looks like the original poster had accidentally run "crontab /etc/crontab" as root. In other words, made /etc/crontab as root user's crontab. Because latter cannot specify a user, it was treat as command and hence the "sh: root: cannot find command" error message.

The solution is to remove this unwanted crontab entry. It should be in /var/spool/crontab/root (or similar -- check "man cron" to find the directory) and remove the crontab created for root.

And DO NOT run the command "crontab /etc/crontab" -- that'll make a copy of /etc/crontab as the current user crontab, and that'll never work as /etc/crontab uses a different format than user crontab.

~talkative


All times are GMT -5. The time now is 06:12 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.