Solved

Setting up a crontab file

Posted on 2003-11-02
28
1,519 Views
Last Modified: 2013-12-15
How do I set up a crontab file to run updatedb every day at midnight?  Once a week?  Thanks in advance for your help.
0
Comment
Question by:Gnustome
  • 11
  • 6
  • 6
  • +2
28 Comments
 
LVL 18

Expert Comment

by:liddler
ID: 9665428
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
 
LVL 2

Expert Comment

by:aleshm
ID: 9665462
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
 

Author Comment

by:Gnustome
ID: 9665480
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
 
LVL 2

Expert Comment

by:aleshm
ID: 9665488
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
 
LVL 5

Expert Comment

by:arjanh
ID: 9666022
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
 

Author Comment

by:Gnustome
ID: 9667095
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
 

Author Comment

by:Gnustome
ID: 9667132
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
 
LVL 2

Expert Comment

by:aleshm
ID: 9667176
Type:
crontab -e
hit i letter as in insert
type in 0 0 * * * updatedb
hit esc once
hit :
then type wq

A.
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9667179
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
 

Author Comment

by:Gnustome
ID: 9667186
I also tried saving the manual file to /etc/cron.daily, but that didn't work either (according to executing crontab -l).
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9667188
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
 
LVL 2

Expert Comment

by:aleshm
ID: 9667193
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
 

Author Comment

by:Gnustome
ID: 9667229
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
 
LVL 5

Expert Comment

by:arjanh
ID: 9667233
just do the export as the regular user, just before the crontab -e command
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Gnustome
ID: 9667327
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
 
LVL 5

Expert Comment

by:arjanh
ID: 9667347
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
 

Author Comment

by:Gnustome
ID: 9667439
As root I executed apt-get install pico, but there was an error message:
E: couldn't find package pico
0
 
LVL 38

Accepted Solution

by:
yuzh earned 275 total points
ID: 9667923
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
 

Author Comment

by:Gnustome
ID: 9668746
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
 
LVL 38

Expert Comment

by:yuzh
ID: 9668849
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
 
LVL 38

Expert Comment

by:yuzh
ID: 9668879
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
 

Author Comment

by:Gnustome
ID: 9668907
When I executed the commands in question I was returned:
bash: /usr/spool/cron/crontabs/root: No such file or directory
0
 

Author Comment

by:Gnustome
ID: 9668916
I executed the commands both as root and user, with the same result.  Executing crontab -l as root revealed no crontab for root
0
 
LVL 38

Expert Comment

by:yuzh
ID: 9669042
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
 
LVL 5

Assisted Solution

by:arjanh
arjanh earned 75 total points
ID: 9669174
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
 
LVL 38

Assisted Solution

by:yuzh
yuzh earned 275 total points
ID: 9669410
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
 

Author Comment

by:Gnustome
ID: 9669598
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
 
LVL 38

Expert Comment

by:yuzh
ID: 9669639
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now