Webmaster Key - Discussion Forums


Welcome, Guest. Please login or register.
Did you miss your activation email?
February 09, 2012, 09:24:41 PM

Login with username, password and session length
Welceome to Forums!

Important information for guests and new members:

In order to understand the full benefits of becoming an active member of this forum, please review the following information on guest and new member restrictions. These forum changes have been prompted by an overwhelming and unreasonable amount of bot postings and incoherent guest spam messages. We wish to prevent these events from happening in the future and make our community a more comfortable place for all of our members.

For guests:

Guests are not allowed to open new topics, polls, or posts attachments.
If you wish to open up new discussions on this forum, we encourage you to register.

For new members:

New members with less than five posts are not allowed to modify additional profile information such as avatars, contact information, biographies, and signatures. However, new members are encouraged to post their own topics or reply to topics initiated by other members. Become active on the forums and 5 posts should be an easy task!

We are a diverse community with members from all over the world. We encourage new ideas and interesting conversation. Do not be afraid to post webmaster/computer-related questions or problems, as our active members are always willing to help when they are able. Interested? Join us.

+ Webmaster Key Forums
|-+ General Discussion
| |-+ Tech Corner
| | |-+ HOWTOs Library
| | | |-+ How to schedule the running of scripts
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] Go Down Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print
Author Topic: How to schedule the running of scripts  (Read 7313 times)
Andy
Administrator
Veteran
*****
Posts: 5 752



« on: September 02, 2007, 09:47:59 AM »

Skill level: Intermediate
Requirements: Linux, PHP and Apache hosting (with cPanel) plus capability to CHMOD files (set access permissions). You can do this with FileZilla for example.

We shall be doing this amazing feat with a feature known as Crontab. I access this via cPanel in my web hosting control panel.

What you do is specify how often you want to run a script (for example: everyday at 1AM).

The path to your script will be something like:
php /home/[your username]/public_html/script.php - notice that there is a space after php

Also, you can enter an email address to get reports from the script of success or failure.

There's a tutorial here:
http://www.upstartblogger.com/how-to-create-a-cron-job-in-cpanel

This is a simple script I wrote to test the Crontab out:

Code:
<?php
//save access time of this file
  
$fn "log/log_".date("d-m-y").".txt";
  
$fp fopen($fn"a+"); //Create the file or open it

  
$t date("H:i");
  
fwrite($fp"$t\n");
  
fclose($fp);
?>

Save this as a .php file

This script writes the time that the script was run to a date-stamped file. By doing this you can get confidence that you know how to set up cron jobs. To start with you could try running the script every 5 minutes just ahead of your present time. Don't forget to delete your test crontab. Also you can set up multiple crontabs but be careful to not abuse your server by running them too frequently.

If you save this file to the root directory of your site as script.php it will work with the example crontab path I mentioned in the example.

But you also need to create a log folder for the log data and give this the file permissions of 777 (read/write/execute for everybody). You don't have to use 777 but this makes it possible for you to view the text files of the logs that it saves in your browser i.e. you browse the log file and see the text files listed and can click on them to see the contents.

Test the script by browsing to www.yoursite.com/script.php or whatever you called the script. Actually, it is better to set this up in a test folder rather than working from the root of your site.

So you may have the file structure:

public_html/testcron/script.php
public_html/testcron/log/ - set this to CHMOD 777

Make sure your crontab path has this new path to the script e.g. php /home/site123/public_html/testcron/script.php

Test the script by browsing: www.yoursite.com/testcron/script.php
If you get error 500 then your log file can't be written because of permission problems. I got this problem when trying to write to the same folder that my script was in. The answer was to create a sub-folder.
To check the log file browse: www.yoursite.com/testcron/log/

Check your email for reports from the cron job.

Remember the time values are based on where your server is such as EST (Eastern Standard Time) not your local time. You can get your server time by looking at the time stamps of uploaded files in your FTP program.

Have fun....
« Last Edit: September 02, 2007, 02:48:33 PM by Andy » Report to moderator   Logged

SensoVision
Administrator
Veteran
*****
Posts: 5 857


I'm proud user of Debian GNU/Linux OS


WWW
« Reply #1 on: September 03, 2007, 12:24:19 AM »

Nice tutorial, Andy!
Although I've always had problems with setting cron job through cPanel interface so was really glad when I was able to set job to crontab file directly.
So if you have on hosting SSH access you may try this:
crontab -e
this would open crontab editor. To setup executing of script provided by Andy  for example to be executed on Monday and Friday at 19:30, every month and every day of month you would need to add this string:
Code:
#min   hour   dayofmonth   month   dayofweek         command                                   
30      19     *             *         1,5             php /home/[your username]/public_html/script.php

Don't forget to have empty string at the bottom of crontab as if you don't it may cause problems.

Andy, hope you wouldn't mind  that I stole your topic like this? I just hate cPanel interface when it comes to crontab and thought to show other way as alternative.
Report to moderator   Logged

Denis
Andy
Administrator
Veteran
*****
Posts: 5 752



« Reply #2 on: September 03, 2007, 09:12:50 AM »

Quote
Andy, hope you wouldn't mind  that I stole your topic like this? I just hate cPanel interface when it comes to crontab and thought to show other way as alternative.

No problem, I thought you would add something.  Wink

btw if you click the "Advanced" button in cPanel under Cron Jobs then you get a simple interface to enter data the way you mentioned. Actually I think I would prefer to use this option since it seems easier to understand than the Standard cPanel option where it's not entirely clear what the various selections will end up doing.

One interesting use for a cron job would be to run a script that gets data such as stock prices or lottery results from another site. A bit cheeky though and they could always block you by ip address.

An alternative to a cron job would be a script that is called from a regularly trafficked page. This script could then do some action based on the current time.
Report to moderator   Logged

SensoVision
Administrator
Veteran
*****
Posts: 5 857


I'm proud user of Debian GNU/Linux OS


WWW
« Reply #3 on: September 03, 2007, 10:41:41 AM »

Quote
btw if you click the "Advanced" button in cPanel under Cron Jobs then you get a simple interface to enter data the way you mentioned.
guess that this feature appeared on later versions of cPanel when I already give up on using their interface for this task Tongue
at least I wasn't aware of it's existence.

Quote
One interesting use for a cron job would be to run a script that gets data such as stock prices or lottery results from another site. A bit cheeky though and they could always block you by ip address.

An alternative to a cron job would be a script that is called from a regularly trafficked page. This script could then do some action based on the current time.
As example I can say that use it for recording tv shows at given time, and perform hard disk maintenance(like clearing unnecessary temporary and old backup files), generating regular backups, and scanning for viruses. It let me write schedule which wouldn't interfere with my work on PC and I doesn't experience slowdowns during execution of any of these tasks.

Regarding banning of IP which you mentioned, it could really happened, so it's important to use cron wisely and not perform frequent requests to server, also if downloaded data is considerably big, you may like to check from script if it was changed from last time or not, to avoid downloading of duplicated data. I'm sure that administration would appreciate if webmasters would get data in a such way and wouldn't put excess load on the servers.
Report to moderator   Logged

Denis
sholing
Jr. Member
*
Posts: 26


« Reply #4 on: September 08, 2010, 09:51:49 AM »

PHP codes always giving trouble to me,i must use all tricks while running the code.
Report to moderator   Logged
Pages: [1] Go Up Stumble Upon! Digg It! del.icio.us! Add to Technorati! ReddIt!  Send this topic Print 
+ Webmaster Key Forums
|-+ General Discussion
| |-+ Tech Corner
| | |-+ HOWTOs Library
| | | |-+ How to schedule the running of scripts

Jump to:  
« previous next »


Our Partners
RelmaxTOP Ranking System Web Hosting RelmaxTOP Ranking System
Staff Sites
12Noon[12Noon Gallery] Andy[Urgentclick]
Tamuril[Tamuril's Digital Art Exhibit] Sensovision
Powered by MySQL Powered by PHP We are hosted by Relmax Inc. |Our Privacy Policy | Sitemap
Powered by SMF 1.1.9 | SMF © 2006-2009, Simple Machines LLC
Forum design by Tamuril © 2005.
Valid XHTML 1.0! Valid CSS!