Solved

How to run cron hourly?

Posted on 2000-05-05
9
548 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
  • 5
  • 4
9 Comments
 
LVL 16

Expert Comment

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

Author Comment

by:ceenamat
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
COOOOLLL!!

Glad to know that your problem was fixed.

Regards.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now