FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Can I add tasks to Windows Scheduled Tasks - Using PHP?





jmlworld
I'm currently working on web-based project and I work on several computers scattered around my home.

I use XAMPP (from http://apachefriends.org) installed on Windows XP machine as my main web server, and I create local domain names such as www.test.lh and www.test2.lh where .lh is localhost. I create these local domain names by opening the Windows hosts file available at C:\Windows\system32\drivers\etc and manually adding main IP address followed by the name of the domain name e.g.

Code:

# add test.lh and alias name www.test.lh
192.168.0.1   test.lh
192.168.0.1   www.test.lh


Then, I go to Apache config file (httpd.conf) and I add virtual host for the domain I created above.

I can write a PHP code that can modify the contents of Windows Hosts file and Apache's httpd.conf, what I need to know is if there's a way PHP can add two quick scheduled tasks in Windows scheduled tasks. What the two tasks will do is described below:

1. Create a scheduled task that executes the file xampp_stop.exe
2. Create another scheduled task that executes the file xampp_restart.exe

The interval between the two tasks above should be 30 seconds, so the two tasks would not collide each other.

---------------------

Now, that's the long story. My question is: how can I add scheduled tasks in Windows scheduled tasks, using PHP?

Thank you in advance.
jmraker
I once had to restart apache several times a day. I did it with a batch file, you could use (restart.bat)
Code:
@echo off
cd c:\xamp\
xamp_stop.exe
xamp_restart.exe

and have the task manager run "start /MIN c:/xamp/restart.bat" so it starts off minimized.

There's also supposedly a way to configure apache to create child webservers that terminate after so many requests are handled but I couldn't get it to work for me.
http://httpd.apache.org/docs/2.0/mod/worker.html
http://httpd.apache.org/docs/2.0/mod/mpm_common.html#maxrequestsperchild

The files that the Windows Scheduled tasks uses are binary files, you'd probably need to find a command line program that creates one or use the AT command
http://technet.microsoft.com/en-us/library/cc738335%28WS.10%29.aspx

If that doesn't work you can try using cygwin's cron program which uses a simpler text file.
http://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows
jmlworld
jmraker wrote:
I once had to restart apache several times a day. I did it with a batch file, you could use (restart.bat)
Code:
@echo off
cd c:\xamp\
xamp_stop.exe
xamp_restart.exe

and have the task manager run "start /MIN c:/xamp/restart.bat" so it starts off minimized.

There's also supposedly a way to configure apache to create child webservers that terminate after so many requests are handled but I couldn't get it to work for me.
http://httpd.apache.org/docs/2.0/mod/worker.html
http://httpd.apache.org/docs/2.0/mod/mpm_common.html#maxrequestsperchild

The files that the Windows Scheduled tasks uses are binary files, you'd probably need to find a command line program that creates one or use the AT command
http://technet.microsoft.com/en-us/library/cc738335%28WS.10%29.aspx

If that doesn't work you can try using cygwin's cron program which uses a simpler text file.
http://stackoverflow.com/questions/707184/how-do-you-run-a-crontab-in-cygwin-on-windows


I was first thinking of using Batch file, then I --somehow-- thought that I would achieve the same result by adding tasks to Windows Scheduled Tasks.

Now, I'll give cygwin's cron a try. The bat file is beyond me, since I wanted to restart apache from a website interface.

If I run a batch file using PHP like this:

Code:

<?php

// if restart button is clicked
if(isset($_POST['submit'])) {

   // execute the apache restart batch form
   echo exec('restart_apache.bat');

   echo "Yay!";

} else {

// nothing is submitted, show the "Restart Button"

?>

<form action="restart_apache.php" method="post">
   <input type="submit" name="submit" value="Restart Apache!">
</form>

<?php
}
?>


I've this code in my bat file:

Code:

@echo off
cd c:\xampp\
call apache_stop.bat
call apache_start.bat


What it does is obvious. 1) Run the apache_stop.bat file which kills the httpd.exe process. 2) Run apache_start.bat file, which starts apache.

The problem here is since PHP is child process of apache's httpd.exe process, PHP can stop the parent process, but it cannot restart it.

Time to give the cron thing a try.
Fire Boar
You could fork off the restart in the batch file, adding a short delay in the forked subprocess so PHP has time to finish before the restart actually happens. I have no idea how you'd achieve this in batch though.
jmlworld
Fire Boar wrote:
You could fork off the restart in the batch file, adding a short delay in the forked subprocess so PHP has time to finish before the restart actually happens. I have no idea how you'd achieve this in batch though.


Fire Boar, I've thought about this, but since I'm not cool in batch, I couldn't achieve this. I also google'd it and all the results say something else, like using "-w" which, somehow, didn't work for me.
rickylau
If you mean pausing for a short time in window batch, try
Code:
start /wait (A dummy command which runs for that length of period)


You may create an exe which terminates itself after that length of period, the /wait switch make next command to wait until that dummy command terminates

If you are not familiar with any programming language other than PHP, you may try (I cant tell whether it works actually, I guess it does)
- Creating PHP script with sleep ();
- Call "path/to/php/installation/php.exe path/to/your/script.php" (You should have a php.exe in your php installation, if not [although I guess you have], download a full PHP copy), your batch command would look like
Code:
REM The command runs before delay
START /WAIT C:\php\php.exe C:\my_php\delay_for_a_while.php
REM The command after delay



Didn't read through the whole thread, wondering if what I am saying is what you need or not
jmraker
When I setup a flash video conversion thing for a client I didn't want the site doing more than video conversion at a time because it could affect the site with all the cpu and io used so I made a job queue program in php where it add to the database the name of the command, directory, priority, etc.

In another program that's started in a cron job it checks the database for jobs every 10 seconds and calls the program and never exits. If the program crashes the cron job would start it up again (because on startup the php program checks to see if it's already running).

Something like that would probably work if it's using the right user's crontab like root's

Code:
CREATE TABLE IF NOT EXISTS `job` (
  `job_id` int(11) NOT NULL auto_increment,
  `job_title` varchar(255) NOT NULL,
  `job_user` int(11) NOT NULL,
  `job_command` varchar(255) NOT NULL,
  `job_directory` varchar(255) NOT NULL,
  `job_created` int(11) NOT NULL,
  `job_priority` smallint(2) NOT NULL,
  `job_status` enum('waiting','running','done','error') NOT NULL,
  `job_delete` enum('yes','no') NOT NULL,
  `job_output` text NOT NULL,
  PRIMARY KEY  (`job_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
jmlworld
rickylau wrote:
If you mean pausing for a short time in window batch, try
Code:
start /wait (A dummy command which runs for that length of period)


You may create an exe which terminates itself after that length of period, the /wait switch make next command to wait until that dummy command terminates

If you are not familiar with any programming language other than PHP, you may try (I cant tell whether it works actually, I guess it does)
- Creating PHP script with sleep ();
- Call "path/to/php/installation/php.exe path/to/your/script.php" (You should have a php.exe in your php installation, if not [although I guess you have], download a full PHP copy), your batch command would look like
Code:
REM The command runs before delay
START /WAIT C:\php\php.exe C:\my_php\delay_for_a_while.php
REM The command after delay



Didn't read through the whole thread, wondering if what I am saying is what you need or not


I'm not currently at home. But it seems that one will work. I'll completely stop using batch script if this last attempt fails.

Thanks for your help mate.
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
how to add my reg domian using directadmin...
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
Access Java 1.6 Web service using php client
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.