How to run cron hourly?

Posted on 2000-05-05
Last Modified: 2006-11-17
How do I run a perl program as a cron hourly?
I placed this perl pgm in /path/cgi-bin/cronTest.cgi


$time = localtime ();
open (FFILE, ">>/path/file/cronTest");
print FFILE "[ $time ] [Cron Test Run] \n";
close (FFILE);


In my cron.hourly I have a file cronMail, which goes like this:


perl /path/cgi-bin/cronTest.cgi


When I executed cronTest.cgi from the browser, it wrote to the file, /path/file/cronTest. But when I placed it in cron.hourly, it didn't write.

What can be the problem? What permissions shd I give cronMail?
I have given 755 permissions to both cronMail & cronTest.cgi. cronMail has owner as 'root' & cronTest.cgi has owner as 'admin'. All these are same for other cron programs which are running well.

Question by:ceenamat
  • 5
  • 4
LVL 16

Expert Comment

ID: 2780792
couple of Questions.

dont you use the crontab command to run your cron files??

is the method that you are using the normal way you use crons??

Also i would suggest that you add full path to perl in your file

to find out full path to perl, type the following at the command prompt

which perl

lets say you get


then change your cron.hourly file as follows.....

/usr/local/bin/perl /path/cgi-bin/cronTest.cgi  2> /tmp/error

now, if any error occurs the output is trapped in error file in the /tmp directory. That way you will have a better idea of the error.

Author Comment

ID: 2781657
Actually this is my first attempt at cron programs. So I don't know what all I am supposed to do. I only placed these files in the locations I mentioned & changed their permissions.

There are other cron pgms running in cron.hourly (which others have written), which do not have the entire path of perl given (/usr/bin/perl)

I will try out the error printing part you mentioned, but what is "2>" for ?

And what's "the crontab command" ?

LVL 16

Expert Comment

ID: 2782410
Let me answer the 2nd Q first.

"And what's "the crontab command" ? "

A cron job is a command run by the cron daemon at regularly scheduled intervals.

The crontab command submits, edits, lists, or removes cron jobs. The crontab command invokes an editing session that allows you to create a crontab file.

You create entries for each cron job in this file. Each entry must be in a form acceptable to the cron daemon.

When you finish creating entries and exit the file, the crontab command copies  it into the /var/spool/cron/crontabs directory and places it in a file named for your current user name. If a file with your name already exists in the crontabs directory, the crontab command overwrites it.

To list the contents of your crontab file, specify the crontab command with the -l command. (crontab -l)

To remove an existing file, use the -r flag. (crontab -r)

The crontab File Entry Format

A crontab file contains entries for each cron job. Entries are separated by
newline characters. Each crontab file entry contains six fields separated by
spaces or tabs in the following form:

minute  hour  day_of_month  month  weekday  command

These fields accept the following values:

minute 0 through 59

hour 0 through 23

day_of_month 1 through 31

month 1 through 12

weekday 0 through 6 for Sunday through Saturday


To run the calendar command at 6:30 a.m. every Monday, Wednesday, and
Friday, enter:

     30 6 * * 1,3,5 /usr/bin/calendar

To run a script called maintenance every day at midnight in August, enter
the following:
     0 0 * 8 * /u/harry/bin/maintenance
"I will try out the error printing part you mentioned, but what is "2>" for ? "

2> means "send any errors to .."

so basically i am saying that if any errors occur trap then and store them in a file called error in the /tmp directory.
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 2787367
It appears the problem is elsewhere, I am working on it. There is an error from the cron daemon, which says "No such file or directory". I am trying to figure out which file/directory he is talking abt!

I changed the cron pgm to :


date >> /path/file/cronTest

Any ideas?

LVL 16

Accepted Solution

maneshr earned 50 total points
ID: 2788380
check a couple of things..

1) is the path to sh correct??
2) is the path to date correct
3) does the path /path/file exist??

you can check 1 & 2 as follows..

which sh

which date

the above will give you full path to the corresponding files.
use the full path in your cron.

Also check if you have read/write access to /path/file directory.

Hope that helps.

Author Comment

ID: 2791176
which sh gave "/bin/sh"
which date gave "/bin/date"

/path/file exists. I changed the path twice, changed permissions, changed owner...


Author Comment

ID: 2791179
I believe I found the problem. But I can't be sure till the cron hourly executes again. U won't believe it! I uploaded the pgm as binary using FTP. So there was a character in the first line

like this! Maybe that was the problem. Since I viewed it from Linux, I didn't see it. When I viewed it thro WS_FTP, it showed.

I will get back after the cron executes once more, let's see if that's the problem.

Author Comment

ID: 2791403
maneshr: That was the problem. The cron executed correctly the last time. I will award you the points for ur help.
LVL 16

Expert Comment

ID: 2792412

Glad to know that your problem was fixed.


Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

809 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