Setting up a crontab file

How do I set up a crontab file to run updatedb every day at midnight?  Once a week?  Thanks in advance for your help.
GnustomeAsked:
Who is Participating?
 
yuzhCommented:
If You know how to use vi, then do the following:

EDITOR=vi
export EDITOR

0 0 * * * /path/to/updatedb >/dev/null 2>&1

otherwise, simply do:

echo "0 0 * * * /path/to/updatedb >/dev/null 2>&1"  >> /usr/spool/cron/crontabs/root

PS: you need to use specify the fullpath in the cronatb. if updatedb required to run as
as special user, you need to:

0 0 * * * su - fred -c /path/to/updatedb >/dev/null 2>&1

0
 
liddlerCommented:
Use crontab -e to edit you crontab
0 0 * * * updatedb
to run each day at midnight, or
0 0 * * 0 updatedb
to run just on Sunday night
Type man crontab for more details
0
 
aleshmCommented:
Isn't updatedb set to run as user nobody each night by default? Guessing out of my head tho, but ain't this default on slack?

A.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
GnustomeAuthor Commented:
When I type crontab -e, then 00*** updatedb, then ^O, followed by ^T, then ^X, I'm taken back to the page where I typed the command.  I just go around in circles, can't seem to find any way to exit the editor and have the file stick.  Not only that, but the editor wants to name the file something strange like /tmp/crontab/XXXXAtAul.

aleshm, I don't think it runs by default, because I had once had a problem with deleted files being reported by running slocate.  I solved that by manually running updatedb.
0
 
aleshmCommented:
Try running crontab -l nobody.

Example:
crontab -l nobody
# This updates the database for 'locate' every day:
40 04 * * *       cd / ; updatedb 1> /dev/null 2> /dev/null

This is default on slackware!

You could also manually edit the file, for example on slack it's in /var/spool/cron/crontabs dir. There you have crontabs for all users.

# pico root  

will edit your root crontab and pico is much easier to use the vi (which is default when you typed crontab -e).

A.
0
 
arjanhCommented:
Hi,

The crontab command will use vi, or the editor specified by your VISUAL environment variable...

In vi you should leave edit mode (ESC) and then type :wq to write and quit.

Directly editing the crontab file may result in the updated file not being detected by crontab. The temporary filename is determined by the crontab program, and edits to it WILL be detected.

Cheers,
Arjan
0
 
GnustomeAuthor Commented:
I'm using Debian 2.4.18.
I've increasedthe points to 250.  As user, I type
crontab -e
This starts vi, so I enter
0 0 * * * updatedb
^O
This presents a page identified at the top as File: /temp/crontab.XXX...     Modified  At the bottom I change /temp/crontab.XXX... to /temp/crontab.updatedb.  The options on this page are ^T and ^C.  I type
^T
This presents a page identified at the top as DIR: /temp      Modified
The options on this page are ^Y ^V and ^X
The page lists a number of files and directories, including /temp/crontab.XXX...
I type ^X, and I'm back to the page identified at the top as /temp/crontab.XXX...
On every page I tried punching ESC repeatedly, with no effect.  I read part of man crontab, and it said that pressing ESC repeatedly would eventually exit vi and cause a beep.  That didn't happen, so I'm at a loss as to what to do.

I tried to manually edit crontab for user using kedit in X.  I saved 0 0 * * * updatedb to /var/spool/cron/crontab using the file name updatedb, but when I executed crontab -l both as user and as root the response was: no crontab for user (root).
0
 
GnustomeAuthor Commented:
I forgot to mention that when I tried to save the file using kedit, the top of the kedit window stilled said Untitled [Mod:
0
 
aleshmCommented:
Type:
crontab -e
hit i letter as in insert
type in 0 0 * * * updatedb
hit esc once
hit :
then type wq

A.
0
 
arjanhCommented:
This is not vi then - vi doesn't use CTRL-<char> commands. Looks like it is pico then.
Why don't you try ^X (then Y and enter) after editing - it will ask for confirmation to write your changes to disk...
0
 
GnustomeAuthor Commented:
I also tried saving the manual file to /etc/cron.daily, but that didn't work either (according to executing crontab -l).
0
 
arjanhCommented:
and don't try to change the name - just leave the name and overwrite it. It is just a temporary name generated by the crontab program.
0
 
aleshmCommented:
One other way you could do this is:
export VISUAL=pico
crontab -e

Now you're editing your crontab in pico, which will make it much easier for you.

Hope it helps!
0
 
GnustomeAuthor Commented:
Actually, the editor is nano 1.0.6.  But I executed, as root, export VISUAL=pico.  Then, as user, I typed crontab -e, but the editor that came up is still nano.
0
 
arjanhCommented:
just do the export as the regular user, just before the crontab -e command
0
 
GnustomeAuthor Commented:
As user (steve) I typed export VISUAL=pico, thencrontab -e.  The response was:
no crontab for steve - using an empty one
/bin/sh: pico: command not found
crontab: "pico" exited with status 127
0
 
arjanhCommented:
Then you don't have pico installed. I don't have nano installed, so I can't check it, but have you tried ^X in nano and then save the file?
To get nano back now you have to do export VISUAL=nano before the crontab -e
0
 
GnustomeAuthor Commented:
As root I executed apt-get install pico, but there was an error message:
E: couldn't find package pico
0
 
GnustomeAuthor Commented:
We may be close to a solution, but I'm not sure we're there yet.  Here's what I have ready to go:
echo "0 0 * * * /usr/bin/updatedb >/dev/null 2>&1" >>/usr/spool/cron/crontabs/root

I'm not a programer, but I'm also not sure the latter part of that line is going to fly.  Here's the reason:  Running a KDE Find Files search for crontabs turned up only one instance: /var/spool/cron/crontabs.  But doing dir revealed that crontabs is an empty directory.  Running a KDE Find Files search for spool turned up only /var/spool.

Should I write it the way I have it in this post, or does it need to be altered?  Am I doing anything else wrong?
0
 
yuzhCommented:
login as root (or su as root), type in:

crontab -l

To verify the cron record.

then check if the cron job is running or not:

ps -ef | grep cron

if not you need to start the cron job (or just simply reboot)
0
 
yuzhCommented:
also have a look at the following pages to learn something about cron:

http://www.unixgeeks.org/security/newbie/unix/cron-1.html
http://www.linuxhelp.net/guides/cron/
http://floppix.ccai.com/cron.html

man crontab
0
 
GnustomeAuthor Commented:
When I executed the commands in question I was returned:
bash: /usr/spool/cron/crontabs/root: No such file or directory
0
 
GnustomeAuthor Commented:
I executed the commands both as root and user, with the same result.  Executing crontab -l as root revealed no crontab for root
0
 
yuzhCommented:
If you don't have "crontabs" under "/usr/spool/cron",

echo "0 0 * * * /usr/bin/updatedb >/dev/null 2>&1" >>/usr/spool/cron/crontabs/root

can not create a dir !!!

Now, you can try:

EDITOR=vi
export EDITOR
crontab -e

and put the cron record in (vi mode)

If you still cann't do it, then try to manually create the dirs:
mkdir /usr/spool/cron /usr/spool/cron/crontabs

then use chmod to set the dir permission as the followings:

drwxr-xr-x   4 root     sys          512 Oct 15  1999 cron/
drwxr-xr-x   2 root     sys          512 Jul 23  2002 crontabs

PS: please read the web links in my previuos post, it will help you to understand cron.
0
 
arjanhCommented:
I think you should change
echo "0 0 * * * /usr/bin/updatedb >/dev/null 2>&1" >>/usr/spool/cron/crontabs/root
into
echo "0 0 * * * /usr/bin/updatedb >/dev/null 2>&1" >>/var/spool/cron/crontabs/root

Because appearently that is where your system stores its crontabs.
Then a crontab -l should hopefully finally give you a non-empty file :)

And pico is part of the Pine package (a text mail client), so if you really want to try that (crontab editing SHOULD work with any editor...):
apt-get install pine-tracker
0
 
yuzhCommented:
arjanh make a good point, some of the *nix favour use:
(we should ask for the OS version in the first place !)
/var/spool  instead of /usr/spool

should try:

echo "0 0 * * * /usr/bin/updatedb >/dev/null 2>&1" >>/var/spool/cron/crontabs/root


0
 
GnustomeAuthor Commented:
Using /var/spool/cron/crontabs/root, run as root worked.  Executing crontab -l proves that it was accepted.  As for whether or not it is running, I'm not sure.  I executed ps -ef | grep cron, and here is what the response was:

root   228       1  0 Nov02 ?          00:00:00 /usr/sbin/cron
root   528   525  0 00:45   pts/1   00:00:00 grep cron

I executed the command at 00:45.

I've increased the point value to 350, so I could split it and there would be enough to go around.  You both worked hard to produce a solution.  I already tried apt-get install pico, but that didn't work.  apt-get install pine-tracker didn't work either.  I don't know why.

Thank you both for your hard work.  I look forward to your replies, after which I will split the points and close the question.
0
 
yuzhCommented:
root   228       1  0 Nov02 ?          00:00:00 /usr/sbin/cron

the cronjob is running !!!

(Next time, when you ask a *nix relatived question, please post your OS version,
or the output of "uname -a" command, so that you can get an quicker answer)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.