Jump to content

setting up cronjob from php script


rdadhich

Recommended Posts

Hi,

 

My requirement was to set cronjob automatically from php script , if user clicks on a particular button on the webpage.

Then this cronjob will run another php script in the back ground.

I am clueless how to set cron job from php script with out opening crontab file.

 

Will be very help full if some one guide me on it.

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/
Share on other sites

I'm no linux guru but I can see to ways:

 

#1: Opening the crontab and adding it "manually" (with the php script of course), but this is probably "dirty"

 

#2: Use the shell_exec() function to execute the normal linux terminal way of setting up a cronjob:

 

http://dk.php.net/shell_exec

 

<?php

$output = shell_exec('0 20 * * * /usr/local/bin/php -q /path/to/your/script.php');

?>

 

I hope this gets you kick started :)

I'm no linux guru but I can see to ways:

 

#1: Opening the crontab and adding it "manually" (with the php script of course), but this is probably "dirty"

 

#2: Use the shell_exec() function to execute the normal linux terminal way of setting up a cronjob:

 

http://dk.php.net/shell_exec

 

<?php

$output = shell_exec('0 20 * * * /usr/local/bin/php -q /path/to/your/script.php');

?>

 

I hope this gets you kick started :)

 

That command doesn't mean anything to a shell. Most Linux distros nowadays have a few corn directories within /etc (cron.daily/ cron.hourly/ cron.monthly/) you could (if you have permission) dump your script in the appropriate directory. Of course, even if you don't have permssions to write files to these directories its easy enough to setup your own within your ~/

Okay thank you thorpe, how embarrassing... "0 20 * * * /usr/local/bin/php -q /path/to/your/script.php" is of course what you would put in the crontab file :(

 

Is the only way, besides writing your script to the appropriate folder, to open and edit the crontab?

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.