Solved

How to run cron hourly?

Posted on 2000-05-05
9
563 Views
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

-------------------------------------------------------
#!/usr/bin/perl

$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:

-------------------------------------------------------
#!/bin/sh

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.

Thanks.
Matt.
0
Comment
Question by:ceenamat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

by:maneshr
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

/usr/local/bin/perl


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

Author Comment

by:ceenamat
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" ?

Thanks!
0
 
LVL 16

Expert Comment

by:maneshr
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

Examples

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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ceenamat
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 :

#!/bin/sh

date >> /path/file/cronTest


Any ideas?

Matt.
0
 
LVL 16

Accepted Solution

by:
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.
0
 

Author Comment

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

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

0
 

Author Comment

by:ceenamat
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
 #!/bin/shž

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

Author Comment

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

Expert Comment

by:maneshr
ID: 2792412
COOOOLLL!!

Glad to know that your problem was fixed.

Regards.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

734 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