rdadhich Posted August 11, 2008 Share Posted August 11, 2008 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 More sharing options...
Wuhtzu Posted August 11, 2008 Share Posted August 11, 2008 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 Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613361 Share on other sites More sharing options...
trq Posted August 11, 2008 Share Posted August 11, 2008 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 ~/ Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613376 Share on other sites More sharing options...
Wuhtzu Posted August 11, 2008 Share Posted August 11, 2008 Thank you for correcting me thorpe. But I don't understand your concern (due to lack of linux / unix knowledge), so maybe you would elaborate Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613378 Share on other sites More sharing options...
trq Posted August 11, 2008 Share Posted August 11, 2008 Theres not much to elaberate on. The following.... 0 20 * * * /usr/local/bin/php -q /path/to/your/script.php will simply generate a shell error because your shell will be looking for a command called 0. That syntax meens nothing outside of a crontab file. Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613380 Share on other sites More sharing options...
Wuhtzu Posted August 11, 2008 Share Posted August 11, 2008 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? Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613382 Share on other sites More sharing options...
trq Posted August 11, 2008 Share Posted August 11, 2008 Yeah but its a bot of a job. You need to copy the current crontab to a file, edit that file then load that file back into crontab. Link to comment https://forums.phpfreaks.com/topic/119120-setting-up-cronjob-from-php-script/#findComment-613393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.