Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mechcozmo

macrumors 603
Original poster
Jul 17, 2004
5,215
2
At least, I think so. My server's system crontab doesn't seem to be happy doing much of anything. It is currently set up to run a shell script, another perl script, a command to perform a chmod, and the 'test' one: make a folder.

The make a folder script runs every minute. The 'who' column says that root is the owner. And the command is a simple 'mkdir WORKALREADY'
But there is no "WORKALREADY" folder being made in my home directory. :mad:

This is not very good, as the shell script bans hackers to my server (runs every 5 minutes), the perl script generates usage logs (Every Saturday), and the chmod command keeps the /Library/Webserver/Documents folder at a state of permissions so that whatever is uploaded to the webserver will be allowed to display (every 5 minutes).

What is wrong with my crontab?:(
 
Mechcozmo said:
The 'who' column says that root is the owner. And the command is a simple 'mkdir WORKALREADY'
But there is no "WORKALREADY" folder being made in my home directory.

Wouldn't the folder be created in root's home directory (unless you are logged in as root)? Try 'mkdir /Users/<user name>/WORKALREADY'.

Or better yet, try 'open /Applications/iCal' or something. Much easer to check to see if it worked.
 
Did you edit the cron file using the crontab command, or by editing the file directly?

If it's the latter, perhaps cron just didn't notice you changed its instructions. You should be able to kickstart cron into noticing by using the "crontab -e" command as root to edit it again (even if you just change a comment), or with the "touch /etc/crontab" command as root, or even by restarting your Mac.
 
For some reason (that I never investigated) cron doesn't seem to actually execute any jobs by default. Stuff that used to be in crontab (I think...) has been moved to /System/Library/LaunchDaemons

The things I wanted to add were weekly and monthly things, so it was easier for me to edit those scripts to do what I wanted.

Sorry I can't be of more help than telling you something you already know...
 
cjc343 said:
For some reason (that I never investigated) cron doesn't seem to actually execute any jobs by default. Stuff that used to be in crontab (I think...) has been moved to /System/Library/LaunchDaemons

The things I wanted to add were weekly and monthly things, so it was easier for me to edit those scripts to do what I wanted.

Sorry I can't be of more help than telling you something you already know...
Apple doesn't use cron anymore. Everything they put in cron now does launch from Launch. But cron is still there. I use it everyday for clean up and shut down at my lab. It am 100% certain it still works.
 
grapes911 said:
Apple doesn't use cron anymore. Everything they put in cron now does launch from Launch. But cron is still there. I use it everyday for clean up and shut down at my lab. It am 100% certain it still works.
It does. I have cron jobs running on my Tiger system right now. I set up stuff with launchd as well using the excellent "Lingon", but I find cron quicker and easier to set up as I'm familiar with it.
 
dr_lha said:
...but I find cron quicker and easier to set up as I'm familiar with it.
That is how I feel. I've tried to quickly learn launchd, but with no success. I just say "screw it, I'll use cron." I am fearful that the next version of OS X will no longer have cron and I'll be forced to use launchd.
 
Doctor Q said:
Did you edit the cron file using the crontab command, or by editing the file directly?

If it's the latter, perhaps cron just didn't notice you changed its instructions. You should be able to kickstart cron into noticing by using the "crontab -e" command as root to edit it again (even if you just change a comment), or with the "touch /etc/crontab" command as root, or even by restarting your Mac.

I edited the file directly. I can't figure out the crontab command, but the file is simple enough. I tried the 'touch /etc/crontab' command but that didn't seem to help at all.

grapes911 said:
Wouldn't the folder be created in root's home directory (unless you are logged in as root)? Try 'mkdir /Users/<user name>/WORKALREADY'.

Or better yet, try 'open /Applications/iCal' or something. Much easer to check to see if it worked.
You're right... I edited the command to make the folder at the root level of the drive. No go.

I'm trying a restart to see if that will help.
 
I changed the make-folder command to the open-iCal command. Nothing.
I restarted. Got Chicken of the VNC a bit mad, but whatever. And after a minute...

Nothing. :mad:

To prove I'm not going insane, I present this:

Code:
# The periodic and atrun jobs have moved to launchd jobs
# See /System/Library/LaunchDaemons
#
# minute        hour    mday    month   wday    who     command
5       *       *       *       *       root    sh /Users/admin/Documents/Scripts/FTPd-autoban.sh
0       0       *       *       6       root    /usr/local/bin/purestats -m -d -f -o /Users/admin/Documents/Logs
5       *       *       *       *       root    sudo chmod -R 664 /Library/Webserver/Documents
1       *       *       *       *       root    open /applications/ical
 
Mechcozmo said:
What is wrong with my crontab?:(

You have incorrectly edited the file using a text editor rather than using the crontab -e command. It is not going to work until you go back and use crontab to edit the file.
 
Mechcozmo said:
I edited the file directly. I can't figure out the crontab command, but the file is simple enough. I tried the 'touch /etc/crontab' command but that didn't seem to help at all.
You should use "crontab -e" and set the environment variable "EDITOR" to whatever text editor you prefer (otherwise it defaults to vi I believe).

Also, /etc/crontab is not the root crontab on Tiger I believe. The root crontab is now stored in /var/cron/tabs/root
 
mrichmon said:
You have incorrectly edited the file using a text editor rather than using the crontab -e command. It is not going to work until you go back and use crontab to edit the file.
Not true. I always use an editor. It always works for me.

dr_lha said:
You should use "crontab -e" and set the environment variable "EDITOR" to whatever text editor you prefer (otherwise it defaults to vi I believe).
Again, you do not have to use crontab -e. I open up crontab in pico and edit it all the time. It always works.

Also, /etc/crontab is not the root crontab on Tiger I believe. The root crontab is now stored in /var/cron/tabs/root
No, /etc/crontab is the correct one. I tested it out, and I don't even have a crontab where you suggested.
 
grapes911 said:
Interesting. I just skimmed it. I'll read it fully later. pico and /etc/crontab has always worked for me and still does. I see no reason for me to change.
Agreed, it should work. Just given that the guy is having trouble trying that, it can't hurt to use a different method.
 
Mechcozmo, I'll chime in.

I encourage you to read the manual on crontab. man crontab

man crontab said:
Crontab is the program used to install, deinstall or list the tables used
to drive the cron(8) daemon in Vixie Cron. Each user can have their own
crontab, and though these are files in /var, they are not intended to be
edited directly.

To make certain your edits worked, simply use crontab -l and/or sudo crontab -u root -l in Terminal. This will list your current crontab file. DO NOT use sudo crontab without a user option

If you really don't want to use crontab -e to modify your crontab, then create your own crontab file and call it whatever you like. Do NOT put this in /var.

Then after you create the file, simply do a sudo crontab -u root /path/to/your/new/file

Follow this with a sudo crontab -u root -l to make certain it accepted your changes. This will overwrite previous crontabs.

PS. I haven't ever encountered where you add a username to your crontab. That part seems weird to me. Since every user has their own crontab, it wouldn't make sense to allow the ability to masquerade as a different user. My examples assume you want these to run as root, which is why most of the commands have the -u root option.

I suggest your the file you create look like this.

Code:
5       *       *       *       *       sh /Users/admin/Documents/Scripts/FTPd-autoban.sh
0       0       *       *       6       /usr/local/bin/purestats -m -d -f -o /Users/admin/Documents/Logs
5       *       *       *       *       chmod -R 664 /Library/Webserver/Documents
1       *       *       *       *       open /applications/ical

Then follow the above instructions.


Finally, if you don't want to hassle with Terminal, there exists some crontab GUIs but I don't have any experience with them to recommend one. Also you may want to consider redirecting output so you can actually see whats going on...

I think thats about it.

PPS. Obviously (or maybe not) - if you are logged in as root, you won't need the sudo.

Feel free to PM me if you need addt'l assistance.
 
A question for kingjr3:

Your example cron file includes the task
sudo chmod -R 664 /Library/Webserver/Documents​
Why would you use sudo in a cron file? If it's root's cron file, I would think it wouldn't be necessary, and if it's a mortal user's cron file, I wouldn't think it would work.
 
Doctor Q said:
A question for kingjr3:

Your example cron file includes the task
sudo chmod -R 664 /Library/Webserver/Documents​
Why would you use sudo in a cron file? If it's root's cron file, I would think it wouldn't be necessary, and if it's a mortal user's cron file, I wouldn't think it would work.

That was Mechcozmo's crontab example.....I didn't really look at it, but yes you are correct. sudo would indeed be redundant if this is root's crontab and impossible in any other user's crontab...good catch Doc. post edited.

:)
 
Code:
Server-G4:~ admin$ sudo crontab -u root -l
Password:
crontab: no crontab for root
Server-G4:~ admin$

That would be a bad thing, wouldn't it?
 
kingjr3 said:
I suggest the file you create look like this.

Code:
5       *       *       *       *       sh /Users/admin/Documents/Scripts/FTPd-autoban.sh
0       0       *       *       6       /usr/local/bin/purestats -m -d -f -o /Users/admin/Documents/Logs
5       *       *       *       *       chmod -R 664 /Library/Webserver/Documents
1       *       *       *       *       open /applications/ical

Then follow the above instructions.

The root column existed in all the entries already so I thought I should keep them there for my additions...
And the crontab command is very confusing. I get a bunch of tildes, odd colors, and no clear explanation. Editing the file ought to work and is more clear for me.

As an addition to my above post; the file exists, but the command says it does not. :confused:
 
Mechcozmo said:
Code:
Server-G4:~ admin$ sudo crontab -u root -l
Password:
crontab: no crontab for root
Server-G4:~ admin$

That would be a bad thing, wouldn't it?
No. It simply means that user root doesn't yet have a cron file. Since you were asking to display it, not edit it, the command tells you there isn't such a file.
 
Doctor Q said:
No. It simply means that user root doesn't yet have a cron file. Since you were asking to display it, not edit it, the command tells you there isn't such a file.

Except that the root user should have /etc/crontab as its cron file. If I remember the man page correctly.

However I used the instructions on that Oreillynet.com site to use the 'crontab -e' in nano. Now, I DO have a root crontab. Or so it says.

Code:
Server-G4:~ admin$ sudo crontab -e
crontab: no crontab for root - using an empty one
crontab: installing new crontab
Server-G4:~ admin$ sudo crontab -l
# The periodic and atrun jobs have moved to launchd jobs
# See /System/Library/LaunchDaemons
#
# minute        hour    mday    month   wday    who     command
5       *       *       *       *       sh /Users/admin/Documents/Scripts/FTPd-autoban.sh
0       0       *       *       6       /usr/local/bin/purestats -m -d -f -o /Users/admin/Documents/Logs
5       *       *       *       *       chmod -R 664 /Library/Webserver/Documents
1       *       *       *       *       open /applications/ical.app

iCal still isn't opening though... which it should be.

EDIT: I take that back... iCal opened but a bit late it would seem...
EDIT v2: VNC, forgot to refresh, I think...
EDIT v3: No, 'top' says iCal isn't here and it has been 3 minutes...
EDIT v4: Yeah, it is running the cron scripts but not at their 'correct' times. iCal should have opened 4 times by now but it hasn't, and top doesn't show it as being open. At least I know the scripts will run belatedly...
 
I'd be surprised if you could use a command like open /applications/ical.app in a cron file because iCal requires a graphic environment and cron runs as a background process (a daemon).

Perhaps it would be better, for testing, to start with a command such as touch /Users/Mechcozmo/Documents/test.txt and then see if that file gets created when the cron task is supposed to run.
 
Doctor Q said:
I'd be surprised if you could use a command like open /applications/ical.app in a cron file because iCal requires a graphic environment and cron runs as a background process (a daemon).
The command did work, but delayed. (Not every minute as it was supposed to)

Doctor Q said:
Perhaps it would be better, for testing, to start with a command such as touch /Users/Mechcozmo/Documents/test.txt and then see if that file gets created when the cron task is supposed to run.
New and improved crontab using 'sudo crontab -e'
Code:
Server-G4:~ admin$ sudo crontab -l
# The periodic and atrun jobs have moved to launchd jobs
# See /System/Library/LaunchDaemons
#
# minute        hour    mday    month   wday    command
5       *       *       *       *       sh /Users/admin/Documents/Scripts/FTPd-autoban.sh
0       0       *       *       6       /usr/local/bin/purestats -m -d -f -o /Users/admin/Documents/Logs
5       *       *       *       *       chmod -R 664 /Library/Webserver/Documents
1       *       *       *       *       touch /text.txt
No 'text.txt' after a minute however; not even after about 5 minutes.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.