-   Linux - Newbie (
-   -   How to create a cron job and run it ,give simpleexample (

ramakrishnankt 12-03-2009 07:11 AM

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


repo 12-03-2009 07:14 AM

A good start would be

donanap 12-03-2009 07:19 AM

Hi Ramakrishnan,

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


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.


# 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...

Good Luck

druuna 12-03-2009 07:19 AM



Script you want to run:
When: daily 12:15

From the terminal:

crontab -e (a editor session is started)

Add: 15 12 * * * /full/path/to/

Save and exit editor.


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

Hope this helps.

ramakrishnankt 12-03-2009 07:36 AM

Thanks to all
when i type crontab -e on terminal it gives

/var/spool/cron/crontabs/node2: Permission denied

druuna 12-03-2009 07:43 AM


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.

ramakrishnankt 12-03-2009 07:49 AM

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?

druuna 12-03-2009 07:55 AM


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.

ramakrishnankt 12-03-2009 07:59 AM


Originally Posted by druuna (Post 3778286)

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

How to create it druuna?

druuna 12-03-2009 08:09 AM


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.

chrism01 12-03-2009 06:56 PM



should be


although it's quicker to do


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.

I REALLY hate the way Ubuntu has messed with the std sudo & su setup that the rest of the *nix world has always used!

ramakrishnankt 12-03-2009 11:20 PM

When i type vi/etc/cron.allow
result is
bash: vi/etc/cron.allow: No such file or directory

chrism01 12-03-2009 11:26 PM

You missed the space between the cmd and the param

vi /etc/cron.allow

ramakrishnankt 12-04-2009 12:59 AM

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" E212: Can't open file for writing

druuna 12-04-2009 02:38 AM


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.

All times are GMT -5. The time now is 11:05 PM.