Monday, October 28, 2013

Cron Job Creation for Windows Server to run a PHP script

Without any manual trigger or interaction, if one wants to run a specific task at the specific time, We can use "Cron Jobs".

In Linux/Unix, they call it as Cron tabs or task. This can be achieved in Linux using crontab functionality. If we use the LAMP, then we can use this cron job/tab to schedule a PHP script to run on linux server. If we use WAMP i.e the server is on Windows OS, then how to achieve this ?.
The solution is very easy. This can be achieved through a windows OS functionality called as "Task Scheduler" .

The procedure to run a PHP script at a scheduled time automatically is as follows:

1. First create a php script of the action. This should contain the code to be executed. For example taking a backup of the sql data.(test.php). Note that this file should be independently executable. i.e there should not be include files. Some times if the include file is in some other directory, it fails to execute.

2. Now create a batch file to run the above action php script. i.e backup.bat.  To know about how to create the batch file to run the php scripts, wait and go through for my next blog

3. Now try to run this batch file from the command line, It should run and generate the output. Else there should be a mistake in the PHP or batch file. Correct it.

4. Now if one can run the batch file, to generate the output, can create and schedule the task.

5. Go to Start->Programs->Accessories->System Tools->Task Scheduler to schedule a task to run it automatically.

6.  You can see the below screen:

7. Now select the second option in the second option: Task Scheduler Library. You can see the below screen. This list all the scheduled jobs

8. Now you can create a new task by selecting the "Create Basic Task" option from the right Panel of the above image.

9. You can see the below options:


10. You can go through all the options like Trigger, action and finish to complete the creation of the schedule task.

11. Here are the simple details of Trigger and action screen which will schedule the task and action.






12. You can Create/Edit the trigger:



13. You can schedule the task as above. You can go through the different options available and select the appropriate. The above trigger is to run the action for every hour of everyday. One can change it based on their requirement

14. Action screen look like this:

15. Can create/edit the action as below:



16. cmd is to run the command prompt from the start menu. And \c C:\wamp\www\Test\admin\backup.bat is to run the batch file of the php script.

17. Other oprions I left as default.

18. Now Enable the script by selecting Enable option in the Select option at the right panel of the main schedule task window.

19. Now just add a batch file at the above location as C:\wamp\www\Test\admin\backup.bat

20. Batch file should contain the below text and save it as backp.bat

@echo off
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\Test\admin\backupcron.php

21. backupcron.php is the action script which contains the code to generate the back up file.

22. Now just try to run the scheduled task manully through "Run" command of the Task manager and check if it works properly.

23. Just see that this task is automatically triggered and executed at the scheduled time .

That's all we need to do to execute a specific tasks at specific intervals using Windows OS.


Thursday, October 17, 2013

Directory Separators Ignored on Inserting into mySQL

Hi, I faced a problem while inserting the file path to mysql database. You can also encounter similar problem when you work with Mysql. So thought of adding this info for your information.

While inserting a file path to my mysql table, I noticed that the slashes in the path are missed once they added to mysql table. This is because the problem is with the backslashes \ , which are often used to escape certain characters. Hence Mysql considers it as a escape character and ignores it.

You can overcome this problem by different methods.

1. You can add double slashes instead of single slash in the directory path.
     Eg: D:\Projects\Test\SQLBackup
           Instead of writing the above path we can write correctly as below:
           D:\\Projects\\Test\\SQLBackup  
        
2. You can just do  quotemeta($rawpath) before the insert statement. This also works.

3. You can also just do addslashes($url).

All the above should work. I just tried the first two and they work perfectly fine. 

So thought of sharing this info with you all. Happy Coding!!!