4WebHelp: Using cron to automate tasks

Ever needed to run a particular Unix command, PHP or Perl script regularly? You obviously can't be bothered to run it manually every so often (especially if it has to be run daily!). So what are you going to do? Use cron - your saviour!

Cron is a Unix scheduling utility. The syntax is a bit cryptic, but it can be figured out. You'll need to either have telnet (shell) access to your server, or some other way to interface with it (like 34SP's cron front-end). You'll also have to have a host which allows you to run cron jobs.

For example, I have the following two entries in my crontab (cron table):

42 2 * * 0 /usr/bin/zip -q access-log.`/bin/date +\%m\%d`.zip access-log
59 23 * * * /home/server_name/other_path/batch/snapshot.sh

I've changed the names to protect the innocent. Reading from left to right, the first few symbols (numbers or *) refer to when a job will run. For example, the second entry in my crontab runs at 23:59 (that's 11:59 pm). Since the next three items are *, it runs on every day on every month, no matter which day of the week it is. The first job zips up the access logs for my web site, at 2:42 AM, but only on Sunday. It doesn't matter which day of the month it occurs on, it just happens every Sunday.

After that cryptic beginning, the rest of the line is just the command to run. So suppose you wanted something to run on the 15th at noon, but only if it was a Saturday. Then your cron entry would look something like:

0 12 15 * 6 your_job_here

When cron looks for jobs to run, this job will only run at noon (12:00) on the 15th (15) of any month (*) when that day is a Saturday (6). In other words, not very often.

The five fields, in order, are minute (valid values: 0 - 59), hour (valid values: 0 - 23), day of month (valid values: 1 - 31), month (valid values: 1 - 12, or use names), and day (valid values: 0-7, Sunday is 0 or 7, or use names). Any of these fields can be a *, which means match all values. You can also use a comma-seperated list of values, like this: 1,2,3.

I've been using the terms "cron" and "crontab" as if they were the same, however, they're not. Cron is the Unix program which wakes up every minute and looks for jobs to run. Crontab is the database or listing of jobs to consider. I don't own my host, so I don't have access to run cron. But I can use the crontab command (-r to remove, -e to edit, or -l to list) to set up cron jobs which will be run on a schedule that I set up. The listing of jobs above was generated with crontab -l.

Because cron runs as root, you should fully qualify your path names to any scripts that will be run. For example, you shouldn't use:

/batch/snapshot.sh

You should use the full path:

/home/path/batch/snapshot.sh

So, to sum up, here is the crontab syntax:

[minute] [hour] [day of month] [month] [day of week] [command to run]

If you wish to receive errors by email, add the following line to the top of your crontab:

MAILTO="your_email_address@domain.com"

© 4WebHelp and Dave

Prepared for publication by Daniel.

Latest comments on this tutorial
Das
If you have any problem with the cron job settings, you may try http://www.easycron.com.
Mahmad Faruk
Hi one n all..

My question is
In unix cron, i want to run a script in such a way that it should find the files ( including the maximum and minimum size) and after finding it, it should compress on daily basis.

Please help me out.
Thanks in advance
gasper
hi,
a am wondering if i can execute php script via telnet in cron?
in this way (symbolic:):
*/1 * * * *  sudo IP -f php/path script/path

i'm monitoring remote machines in one specific env.
Smile

thx in advance
VINCENT
HI THERE CAN ANYONE TELL ME WHERE TO GET A SCRIPT TO EXTRACT NELY ADDED EMAILS TO MY SITE DAILY.

THANX
willemijns
http://www.willemijns.com/cronjob.htm list every online cronjob competitors
Tom Guider
Try this web based cron hosting: www.setcronjob.com
affordable and reliable.
Jordan
An online tool to automate web based scripts execution , schedule scripts execution's days, hours etc is

<a href="http://getcron.com">GetCron.com</a>

It's possible to set a test account there as well.
prabakar
Hi i want to know the exact syntax to run PHP script in CRON
i am using PHP5 ,Apache,..if anyone know the solution tell me
i want run that script daily basis at particular time
Bridget
Lets say i want to modify a directory that all newly created users will automatically receive a README file in their home directory. Also, providing each user with an alias called la that provides a long listing of all files, including hidden.

How would i be able to do that?
mustafa
how can i write php code to run afile that has an xml sitemap
hsharma
Hi every one...
I have one perl script on unix environment.
I want it to run as cron job every 6 hour of the day. It would be of great help if someone can help me from the begining phase.
Thanks!
Greg
HI

How do i get cron to run for:

1. every 15 mins;
2. every 12 hours;
3. Every 60 mins.

Much appreciated
Nigel I
Hi,
Find great many questions unanswered here. I've one simple requirement. I need to parse and XML file uploaded by users every minute.I've written parser in PHP. I need  to run parser.php and the delete the files or move to another folder.

Any suggessions welcome.
mitch
Thanks for the tutorial. Seemed clear to me.
kumar
really cron are pretty useful in automating some tasks that one have to do on regular basis and this also ensure the accuracy...
Thomas Porter
Cron can't run PHP scripts without using another program. Search "cron php" on Google, you'll find some tutorials.

Cron has afew folders cron.daily, cron.weekly and cron.monthly. Simply do a locate command from a shell eg. "locate cron.daily" and place your script into that folder It will run daily, weekly or monthly, depending on what folder you placed it in.

Its easy for backing up automatically. For example if you open notepad and type "\cp -r /home/ /backups/" and save it as backup.cron and place it into cron.daily dir. Your server will make a backup daily (at 12.00 am [depending on the OS]) into /backups/.

Now running a PHP script to run every 15 min.. I'm not telling, learn what i've just wrote first. What you want to do is more advanced, and sometimes not possable.
grace
Hi,  the tutorial is helpful, but I'm not sure about where to put those commands.

I have a host with cron, and I wonder where I am supposed to put the command 'min hr day ** command' to a file, and how to name the file so cron will pick it up etc.  

Basically, from the tutorial I know how to write a cron command, but I dont know how to actually set up a cron job.

thanks.
papayiya
If you are from the UK and don't have cron support, then you can use http://www.webbasedcron.co.uk

papayiya
phil
If you want to send an email to your own address that's very easy: log into http://www.cronjobs.org with your email address, create a new task with any URL and 72 hours delay, then click on notification: the content of the web page will be sent to you every 72 hours (it should not cost more than 10 cents).
If you want to send specific content to different people, then create your own script that send the emails and call it with cronjobs every 72 hours (this should be free).
naveen sharma
hi, how are you
can you exaplain to me cron jobs with example, i want to send email after 72 hours
herwig
Very good web based cron-jobs at http://www.cronjobs.org
and only advance features are not free !
Daniel, 4WebHelp Team Member
Imran: Do you have access to cron?

If not you can use the services of http://www.webcron.org/ (in French but easily translated I think).
Imran
Hi,
Can u please help me with this.
How can i run " www.abc.com/mail.php "
daily.
plz help me in this regard
thanx
Bye
Charlie Sears
Thank you, this was a terific tutorial, i used to use at, but it is much better to use cron i have herad, and it is easyer, and not needed daily lol. Thanks
George
If your looking for an cron server to host your cron jobs use http://www.webbasedcron.com -- This is great if your current host does not allow cron jobs.

Good luck,
George
mrd
If you want a free cron / task scheduler for Windows, then look at VisualCron at it's homepage:  http://www.visualcron.com
barzlah
hi,
i found an useful cron job manager written in php, it have good syntax (like unix-style).don't required any external things, only php and apache!

http://www.bitfolge.de/pseudocron-en.html
how do i do this???
cron.php  --  every 15 minutes

cron_bounce.php  --  every 12 hrs

cron_responder.php  --  every 60 minutes

thnx
Daniel, 4WebHelp Team Member
Carol: Try it out - it shouldn't.
Carol
0 12 15 * 6 your_job_here

When cron looks for jobs to run, this job will only run at noon (12:00) on the 15th (15) of any month (*) when that day is a Saturday (6). In other words, not very often.

I don't think this will work this way in cron.  From what I have read, it will run if either condition is true, either it is the 15th of the month or it is a Sat.
Daniel, 4WebHelp Team Member
Hello dlsodders,

The */10**** part is not part of the command - it tells cron when to run the script. I don't know if you're using a graphical interface to cron (a Perl/PHP script, or cPanel's editor), but if you are the command is separate from the scheduling. Also there should be spaces between the stars.
Cron and PHP
Hello,
I have just started to use cron and im a little confused.

I have tried several different commands to get this php script to run but no dice.

When I run the command */10**** /home/username/public_html/services/scheduler/tools/send_reminders.php

I recieve an output email that says
Quote:
/bin/sh: line 1: */10****: No such file or directory

In the subject line of the email it actually has the url to the script.  If I click on the url I get this in the browser.
Quote:
/bin/local/bin/php -q
And the script runs.

Know what I have done wrong here?





Daniel, 4WebHelp Team Member
rajiv: Yes, it will, unless your system has trouble keeping its clock on time Smile.
rajiv
will it work after the system reboots ?
John
Works for me.  Thanks!
Daniel, 4WebHelp Team Member
Quote:
helpful.... but i still cant get mine to work... how do i zip a directory and then have that zip file emailed to me?

Please post in our forums for help - the comments system is not designed for support.
marc
helpful.... but i still cant get mine to work... how do i zip a directory and then have that zip file emailed to me?
Alexis
Very nice tutorial, short but useful. Thanks!
Sajid Khan
Thank you, this tutorial has been very helpful.

Add a new comment


Page URL: http://www.4webhelp.net/tutorials/misc/cron.php
Back to the "pretty" page!

© 2017, 4WebHelp Team.