Hi,

Can i use sleep() for 1 hour? I mean is there any expiry time for it or browser or session so on. Purpose is to send 2000 emails every one hour.

Thanks

Recommended Answers

All 12 Replies

Should be able to

sleep(3600);

You may also want to note that if you plan to send 2000 email during exactly an hour then you will want to setup a timer to time how long it took for the script to execute before the sleep function then subtract that time from 3600.

After sending 2000 email (it doesn't matter how long it takes), it is going to sleep 1 hour sleep(1200); . This shouldn't require any configuration, i guess!
Is this statement logical?

... it is going to sleep 1 hour sleep(1200); . This shouldn't require any configuration, i guess!
Is this statement logical?

Well the sleep function would be set to sleep(3600); if you want it to sleep an hour after sending. However, if you want it to take an hour from line 0 to the end of the script then that is when you will need to factor in what time has allready been taken. So judging by your previous post, it sounds like you want the script to take a little over an hour from start to finnish (or at least the loop) which meens you would just use the following:

sleep(3600); //sleep exactly one hour

Hi,

Can i use sleep() for 1 hour? I mean is there any expiry time for it or browser or session so on. Purpose is to send 2000 emails every one hour.

Thanks

For what you're doing the browser should not be in the equation. What you want is to start the PHP script from the command line.

php /path/toscript.php &

You can even use that from within a PHP page.

exec('php /path/to/script.php &');

That way it runs in the background.

Other better options if possible:

Use cron. Write a PHP deamon.

Hi digital,

Is there any examples to run a cron job under ubuntu 8 because, i can't do it, although i have tried a lot of different examples?

thanks

Hi digital,

Is there any examples to run a cron job under ubuntu 8 because, i can't do it, although i have tried a lot of different examples?

thanks

Are you trying to just input this yourself from the command line or want PHP to do it?

see: http://www.unixgeeks.org/security/newbie/unix/cron-1.html

Basically you need to use: crontab -e

This brings up the default editor, and allows you to edit your user's crontab file.

The syntax:

Now for the more complicated second part of a crontab file.
An entry in cron is made up of a series of fields, much like the /etc/passwd
file is, but in the crontab they are separated by a space. There are normally
seven fields in one entry. The fields are:

minute hour dom month dow user cmd

minute This controls what minute of the hour the command will run on,
and is between '0' and '59'
hour This controls what hour the command will run on, and is specified in
the 24 hour clock, values must be between 0 and 23 (0 is midnight)
dom This is the Day of Month, that you want the command run on, e.g. to
run a command on the 19th of each month, the dom would be 19.
month This is the month a specified command will run on, it may be specified
numerically (0-12), or as the name of the month (e.g. May)
dow This is the Day of Week that you want a command to be run on, it can
also be numeric (0-7) or as the name of the day (e.g. sun).
user This is the user who runs the command.
cmd This is the command that you want run. This field may contain
multiple words or spaces.

Usually your hosting account will provide a way to manage cron without having to go through the command line. CPanel, which is a really popular control panel, has a web based interface to add a cron job.

commented: Very good explanation of cron +3

Hi,

I did run this code on my localhost (Firefox) but the forever loop was broken after 15 minutes rather than 1 hour. Is it something to do with browser?

Thanks

$i=1;
while ($i>0) {
do something and sleep 1 hour
sleep(3600);
}

Hi digital,

I have add this line into index.php and run it. This solved my problem.

Thanks

exec('php /var/www/html/public/TEST/SwiftSleep/run.php &');

Hi,

I did run this code on my localhost (Firefox) but the forever loop was broken after 15 minutes rather than 1 hour. Is it something to do with browser?

Thanks

$i=1;
while ($i>0) {
do something and sleep 1 hour
sleep(3600);
}

Yes, the browser will not keep a connection for that long. You can try and ask it to keep the connection persistent by making sure you use HTTP/1.1 or using the keep-alive HTTP header with HTTP1.0.

PHP also has a max_execution_time setting in PHP.ini. You can change this at runtime with:
ini_set('max_execution_time', 0);

Apache also has a default keep-alive timeout setting.
http://httpd.apache.org/docs/2.0/mod/core.html#keepalivetimeout

Intermediate caches will also have timeouts.

So keeping a HTTP connection alive is possible, but not always achievable. That is probably why we have BOSH and Comet, which require 2 HTTP connections instead of 1 to emulate persistent HTTP/TCP.

Ok. I have used exec() to run a php file. I know it curently runs. exec('php /var/www/html/public/TEST/SwiftSleep/run.php &') 1. How do i know that it runs?
2. How do i stop it?

Can i create a php file and run it on my localhost and see?

Thanks

Ok. I have used exec() to run a php file. I know it curently runs. exec('php /var/www/html/public/TEST/SwiftSleep/run.php &') 1. How do i know that it runs?
2. How do i stop it?

Can i create a php file and run it on my localhost and see?

Thanks

What you want is called IPC (Inter Process Communication).
http://en.wikipedia.org/wiki/Inter-process_communication

This is why it is better to write a deamon (start a child process) using PHP itself without a system call (ie: exec()). That way, you can use the IPC provided by the system, that allows parent and child processes to talk to each other.

Since you're using exec(), you'll have to use another medium to communicate. This can either by files, or sockets or anything you can both read and write from.

The file is probably the easiest. Just have your /var/www/html/public/TEST/SwiftSleep/run.php write a file, and have your "parent" php script read from that file to know how the "child" is doing.

btw: starting a child process is quite simple:

You need the pcntl extension however.

eg:

// this forks the current process
// which means a child process is created, that is a clone of the current one
// so everything defined and executed in this process, will be cloned to child
$pid = pcntl_fork();

// the returned PID signifies which process we are in
// note that from now on everything will be run twice
// once in the parent process, and again in the child
// but run in parallel
if ($pid == -1) { // fail
 return false;
} else if ($pid) { // parent
  break;// just continue
} else { // child
  // include the file we need as child process
  include('/var/www/html/public/TEST/SwiftSleep/run.php');
  // we need to exit, or we will run the rest of this script, which is meant for the parent process only
  exit;
}
commented: Your posts are always so informative. +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.