Using Cron Job to automate a task or script

By grossiweb9/14/2015
articles
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
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 This e-mail address is being protected from spambots. You need JavaScript enabled to view it

We focus on aspects that matter to your businessThat's what makes our clients happy

Rhia

ATHENS LANGUAGE SCHOOLHOUSE

I am fortunate to have found Grossi Web to construct my website and help me realize my marketing strategy and look! Grossi Web listened to my ideas and created the perfect end result. The process was made easy for me allowing me creative freedom without stress. Grossi Web worked fast, producing a professional and stunning website. I am so pleased with Grossi Web!

Thay

HUMES MCCOY AVIATION

Thanks to the Talent and Professionalism of GrossiWeb, I have turned a dream into a successful aviation company. I continuously receive compliments on the corporate site and AirExpress® brand. GrossiWeb developed a highly targeted marketing campaign that has resulted in international exposure and attaining clients who were previously unreachable!

Bob

DORFMAN CAPITAL

I had been courting a high profile business prospect, but after a few months my prospect was becoming increasingly distant. One morning after I had made some follow up calls, my prospect called me right back stating he had seen a news story about my company on Yahoo news. This is the type of instant gratification and credibility I have experienced working with Grossi Web.

Rhia

ATHENS LANGUAGE SCHOOLHOUSE

I am fortunate to have found Grossi Web to construct my website and help me realize my marketing strategy and look! Grossi Web listened to my ideas and created the perfect end result. The process was made easy for me allowing me creative freedom without stress. Grossi Web worked fast, producing a professional and stunning website. I am so pleased with Grossi Web!

Thay

HUMES MCCOY AVIATION

Thanks to the Talent and Professionalism of GrossiWeb, I have turned a dream into a successful aviation company. I continuously receive compliments on the corporate site and AirExpress® brand. GrossiWeb developed a highly targeted marketing campaign that has resulted in international exposure and attaining clients who were previously unreachable!

Bob

DORFMAN CAPITAL

I had been courting a high profile business prospect, but after a few months my prospect was becoming increasingly distant. One morning after I had made some follow up calls, my prospect called me right back stating he had seen a news story about my company on Yahoo news. This is the type of instant gratification and credibility I have experienced working with Grossi Web.

What happens after you contact us?

15 minutes consultation

We will schedule a quick 15-minute call with you to understand what you need to unlock the full potential of your business.

Project Proposal

After we got every output from you, we will prepare a proposal for you. If you agree with our pricing and timeline, we are all set to take off.