LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-03-2009, 07:11 AM   #1
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Rep: Reputation: 15
How to create a cron job and run it ,give simpleexample


Hi,
How create a cron job in terminal,
and how to run it.
Please give a simple example

Thanks
Ramakrishnan
 
Old 12-03-2009, 07:14 AM   #2
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,529

Rep: Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899Reputation: 899
A good start would be
http://www.cyberciti.biz/faq/how-do-...-or-unix-oses/
 
Old 12-03-2009, 07:19 AM   #3
donanap
Member
 
Registered: Sep 2008
Location: Perth, Australia
Distribution: RedHat, Solaris
Posts: 38

Rep: Reputation: 15
Post Example

Hi Ramakrishnan,

firstly when you want to create/edit your crontab file type the following.

Code:
crontab -e
This will open the crontab to edit and will run a check on the cron job for errors when you go to save it.
Here's a simple example.

This line executes the "find" command at 2AM on the 12th of every month that a Sunday or Saturday falls on.

Code:
# Minute   Hour   Day of Month       Month          Day of Week        Command    
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)                
    0        2          12             *               0,6           /usr/bin/find
I stole this example from here: please check it out for more...
http://www.pantz.org/software/cron/croninfo.html

Good Luck
 
Old 12-03-2009, 07:19 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Assumptions:

Script you want to run: foo.sh
When: daily 12:15

From the terminal:

crontab -e (a editor session is started)

Add: 15 12 * * * /full/path/to/foo.sh

Save and exit editor.

Done.

Look at man 5 crontab for the explanation of the 15 12 * * * (minutes, hours, monthday, month, weekday ) notation.

Hope this helps.
 
Old 12-03-2009, 07:36 AM   #5
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
Hi,
Thanks to all
when i type crontab -e on terminal it gives

/var/spool/cron/crontabs/node2: Permission denied
 
Old 12-03-2009, 07:43 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You (the user executing the crontab -e command) must have crontab access.

I'm not sure if this is true for all distro's but have a look at /etc/cron.allow, the user should be added there (you need root access to change that file).

Hope this helps.

Oh, just remembered: The user should not be present in /etc/cron.deny.
 
Old 12-03-2009, 07:49 AM   #7
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
in my ubuntu
/etc/cron.allow is not found
and also when type sudo su in terminal
it shows
sudo: must be setuid root
and i can't install new packages why?
 
Old 12-03-2009, 07:55 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

I'm not an Ubuntu user, but it looks like something was not installed correctly (the setuid thingy). Maybe an Ubuntu user can help you with that one.

You can create the /etc/cron.allow yourself (as root). It expects one user per line.

Hope this helps.
 
Old 12-03-2009, 07:59 AM   #9
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by druuna View Post
Hi,

You can create the /etc/cron.allow yourself (as root). It expects one user per line.
How to create it druuna?
 
Old 12-03-2009, 08:09 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

From the terminal, as root user:

vi /etc/cron.allow

Once it opens press i (for insert) and type the username that needs cron access.
Press esc (escsape, you will taken out of insertion mode)
Press: :wq this will write (w) the file and quit (q), don't forget the leading :

Last thing to do is to change the permissions of this file:
chmod 600 /etc/deny.allow

You can cat the file to see if it is ok: cat /etc/cron.allow. You should see the user that was added.

Hope this helps.

Last edited by druuna; 12-04-2009 at 02:31 AM. Reason: Fixed a typo (g -> q)
 
Old 12-03-2009, 06:56 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
Actually

:wg

should be

:wq

although it's quicker to do

:x

you'd need to be root to edit cron.allow or cron.deny.
In Ubuntu you should be able to do something like

sudo su -

to become root. If that doesn't work, show us the err msgs you get.

/rant
I REALLY hate the way Ubuntu has messed with the std sudo & su setup that the rest of the *nix world has always used!
end_rant/
 
Old 12-03-2009, 11:20 PM   #12
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
Hi,
When i type vi/etc/cron.allow
result is
bash: vi/etc/cron.allow: No such file or directory
 
Old 12-03-2009, 11:26 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,405

Rep: Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783Reputation: 2783
You missed the space between the cmd and the param

vi /etc/cron.allow


http://rute.2038bug.com/index.html.gz
 
Old 12-04-2009, 12:59 AM   #14
ramakrishnankt
Member
 
Registered: Nov 2009
Location: India
Posts: 32

Original Poster
Rep: Reputation: 15
Hi,
when i type vi /etc/cron.allow
and pressed i
then typed root
then esc pressed then i typed :wq
then esc
it shows
"/etc/cron.allow"
"/etc/cron.allow" E212: Can't open file for writing
why
 
Old 12-04-2009, 02:38 AM   #15
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

2 things:

1) You don't need to put the root user in the cron.allow file, root is all powerful. I initially thought you were talking about a normal user.

2) Another way of putting the username into /etc/cron.allow (as root):
echo "username" >> /etc/cron.allow

After that, just in case:
chmod 600 /etc/cron.allow

Hope this helps.
 
  


Reply

Tags
crontab


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create user account with a 'newusers' run from a cron job? tifoso Linux - General 3 11-05-2009 07:19 AM
How to create a cron job to delete a folder's contents? s0n|k Linux - Newbie 1 10-05-2007 08:59 AM
how to run this cron job ashley75 Linux - General 5 05-24-2004 12:20 PM
Did my Cron job run? ryedunn Linux - Newbie 2 02-25-2004 09:59 AM
Cron job does not run brentos Linux - General 6 12-12-2003 03:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration