Apart from cron

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Apart from cron

Post by oneofthelions »

I need to run a php file every one hour. Is there any other solution apart from cron job?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Apart from cron

Post by requinix »

Could make a daemon for it...

But cron's easier, and if you can't run cron I doubt you'd be allowed a background process.
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: Apart from cron

Post by oneofthelions »

I need to run apart from cron, it is a script...
Someone gave me this solution..

Make a file called "time.db" and CHMOD it as 0777
PHP Code:
<?php
// name of your file
$myFile="time.db";
$time=file($myFile);
if(time()-3600 > $time[0]){
// an hour has elapsed
// do your thing.

// write the new timestamp to file
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, time());
fclose($fh);
}
else{
// it hasn't been an hour yet, so do nothing
}
?>
I am not sure if Godaddy allows me chmod any file... need more clarification.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Apart from cron

Post by requinix »

oneofthelions wrote:I am not sure if Godaddy allows me chmod any file... need more clarification.
If you have FTP or SSH access then you can. If not, you probably can some other way.
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: Apart from cron

Post by oneofthelions »

My script obviously did not run the next time...PLEASE CORRECT ME

Code: Select all

 
require_once("includes/connection.php");
 
    $query = mysql_query("SELECT lastRun FROM time") or die(mysql_error());
    if(mysql_num_rows($query)!=0)
    {
        while($row = mysql_fetch_array($query)) 
        {
        $TimeRun = $row['lastRun'];
        if ($TimeRun + 3600 < time())
            {
            $to_email = "salman_ahad@yahoo.com"; 
            $subject = "test script";
            $message = 
            "Time : Running"; 
 
            mail($to_email, $subject, $message); 
            $TimeRun = time();
            $querypost = mysql_query ("UPDATE time SET lastRun = '$TimeRun'");
            }
        }
    }
    else{
    //nothing
    }
 
OR..could I do?

Code: Select all

 
function first(){
//All my code
 
sleep(3600);
call function second()
}
 
function second(){
//All my same code as above
 
sleep(3600);
call function first()
}
 
 
oneofthelions
Forum Newbie
Posts: 14
Joined: Tue Oct 27, 2009 2:41 am

Re: Apart from cron

Post by oneofthelions »

[quote author=PFMaBiSmAd link=topic=274543.msg1300228#msg1300228 date=1256993285]
.....cron job on your godaddy hosting
[/quote]

any cron job allows us to run a php file at specific time...but this is what I am trying to do

Code: Select all

 
//test.php
 
//users table contains multiple users
 
//message table contains multiple messages
 
$user = mysql_query("SELECT * FROM users") or die(mysql_error());
$message = mysql_query("SELECT * FROM message") or die(mysql_error());
 
//...guide me for rest of the code
 
//Every user should be sent a message every one hour. Also the message which was not sent to any user.
 
//Lets say 
user-1 was sent a message-23 at 01:02:03  // user-1 shld receive the next message in queue after one hour
user-2 was sent a message-40  at 20:22:00  // user-2 shld receive the next message in queue after one hour
...
user-n was sent a message-78 at 12:30:10 // user-n shld receive the next message in queue after one hour
 
 
Can this user specific script be run as cron??
Post Reply