0

I have two php files. In one php file there is simple html form in which I created some drop down for select time and days for cronjob when user set time and day and submit form then all the drop down values stored in database.

Now with the help of this stored values I need to set cronjob and when cron will execute then it run another php file in which I write some code for generate xml file. And this file run every time which time is stored by user in database.

By using Cpanel I can do that but I don't want to use cpanel.

Please give me some proper and working solution, I really need it. Thanks in advance.

j0k
  • 22,600
  • 28
  • 79
  • 90
Indian
  • 645
  • 7
  • 22
  • Without Cpanel or similar, you will have to get a shell to the box and `crontab -e` to edit it directly. – MrCode Nov 09 '12 at 10:51
  • You should not use `cron` like this. The `crontab` file is quite static. Why not when updating the database update the file as well? Seems the best solution and the easiest one to implement. – Ed Heal Nov 09 '12 at 10:52
  • @EdHeal Thanks for your comment but I dont have any Idea how to do this using crontab. Please provide me some example. – Indian Nov 09 '12 at 11:06
  • @paragbhargav - Do not use cron for this. When your code updates the database update the file at the same time. – Ed Heal Nov 09 '12 at 11:13
  • @EdHeal You not understand my question properly. Once user set the time and day then he dont need to set time and day again its automatically run another php file and generate xml file and save it. – Indian Nov 09 '12 at 11:15
  • @paragbhargav - Just get cron to run a script once a minute and do the processing then. Just need to use `crontab -e` once on installation. Get the script to work out what needs to be done. – Ed Heal Nov 09 '12 at 11:18

1 Answers1

0

Use phps system() or exec() function to call the crontab command to replace or modify the existing crontab for the account the web server runs under. You might have to make sure that user is allowed to use the cron system, this depends on the operating system you use.

From within the crontab you can use one of two strategies to run a php script_ - call the php cli interpreter like any normal command, so something like: /usr/bin/php and give it the script to interpret. As an alternative you also can use shebang inside your php script and call it as a simple executable. - use wget to call an url pointing to your webserver (maybe localhost) when you want to execute the script inside your web server.

This might act as a starting point for you to experiment with:

#!/usr/bin/php
<?php
// the time tokens to be fed into the crontab line
$time=array('10','*','*','*','*');

// the actual command to be executed
$command=sprintf("crontab -l | (cat;echo \"%s\t%s\") | crontab", 
                 implode(' ',$time), 
                 '/usr/bin/beep');

// execute command
$result=system($command);

// output result of execution
if ($result)
  echo "Result: $result\n";
else
  echo "FAILURE!\n";
?>

Works for me when called from CLI.

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • I tried to use this exec('echo -e "crontab -l * * * * * /home/username/public_html/myserver.com/client/test/mycron2.php" | crontab -'); But nothing heppen. – Indian Nov 09 '12 at 11:05
  • No, don't use echo, use the command `crontab` itself. Check the man page for the arguments. And eavluate the return value of `exec()` or `system()` as well as the servers error log. – arkascha Nov 09 '12 at 11:19
  • Thanks but........ Still not getting what you want to say. Can you please proved me some example code so that I can understand it better?? – Indian Nov 09 '12 at 11:28
  • I get 'FAILURE!' when I run the above script. And what about '/home/username/public_html/myserver.com/client/test/mycron2.php' file?? – Indian Nov 09 '12 at 12:37
  • I have no idea what that file `mycron2.php` is, so I cannot say anything about it. And for the fact that you get a failure as a result: maybe it is time to start looking yourself what the problem is. It is your system, you should try to understand how things work. You cannot wait what others tell you to type and complain if stuff does not work. How should I know about your setup? If that user account is allowed to use the cron system at all (mentioned that before...) and and and. Start by trying that pipe sequence directly (without php) and understand what it does and what problem exists... – arkascha Nov 09 '12 at 12:41