Executing a shell command in the background
Moderator: General Moderators
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
Executing a shell command in the background
I need to delay execution of a command for several minutes after a user enters a page (ex--user enters page, 5 minutes later command executes). The problem with using sleep() is that the page will not load until the delay is over. The unix sleep command has similar problems. So what I need is to have sleep and the following command be executed directly on the server, so my page will load. Ideas?
- DesignerSMS
- Forum Newbie
- Posts: 17
- Joined: Tue Aug 06, 2002 12:16 am
- Location: Gold Coast, Australia
Yes, this will work.
In your PHP script for generating the page use something like this:
NB: The output of the script must be redirected to something other than STDOUT
And then in scriptname.php:
If this is a popular site this type of execution method could potentially use up a lot of resources. If you get even say 10 requests for this page a second, after 5 minutes there would be 3,000 PHP processes sleeping.
In your PHP script for generating the page use something like this:
Code: Select all
<?php
exec("php -q scriptname.php > /dev/null &");
?>And then in scriptname.php:
Code: Select all
<?php
sleep(60 * 5); // 5 minutes
/* Execute script contents here */
?>