mds1256 Posted December 9, 2012 Share Posted December 9, 2012 (edited) Hi The scenario is that on my website I want to be able to run processes in the background so a user doesn't have to wait until it is finished. Real world example would be a Job site where you submit your CV and based on your criteria it then sends an email to all the people who are interested in each of your skills, and potentially there could be thousands. Now, I dont want the user to have to sit and wait for the SQL to be ran and the emails to be sent before they receive a confirmation. What are my options? I am guessing I could run a cron job every 5 minutes but how would I then get it to send some emails etc? Edited December 9, 2012 by mds1256 Quote Link to comment https://forums.phpfreaks.com/topic/271792-make-php-run-tasks-in-the-background/ Share on other sites More sharing options...
MDCode Posted December 9, 2012 Share Posted December 9, 2012 Set a cron job to run a php file that checks every 5 minutes and sends out emails Quote Link to comment https://forums.phpfreaks.com/topic/271792-make-php-run-tasks-in-the-background/#findComment-1398429 Share on other sites More sharing options...
kicken Posted December 9, 2012 Share Posted December 9, 2012 You would store your tasks that need done into a database table, then have your cronscript select any pending tasks and run through them. So for the email example you'd store some task indicating that a new CV was submitted, and what the ID number is for that new CV. The cronscript could then use that information to find find the email addresses to send to, compose the email, and send it out. Quote Link to comment https://forums.phpfreaks.com/topic/271792-make-php-run-tasks-in-the-background/#findComment-1398433 Share on other sites More sharing options...
gizmola Posted December 9, 2012 Share Posted December 9, 2012 In case it's not clear, "php" has several flavors. There is the php that most people think of as being part of the webserver, and then there is the "Command line interface (CLI)" php that is a seperate executable program. In a shell you can typically run the CLI by typing php -h or php.exe -h and you should see something like this: [david@penny ~]$ php -h Usage: php [options] [-f] <file> [--] [args...] php [options] -r <code> [--] [args...] php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...] php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...] php [options] -- [args...] php [options] -a -a Run as interactive shell -c <path>|<file> Look for php.ini file in this directory -n No php.ini file will be used -d foo[=bar] Define INI entry foo with value 'bar' -e Generate extended information for debugger/profiler -f <file> Parse and execute <file>. -h This help -i PHP information -l Syntax check only (lint) -m Show compiled in modules -r <code> Run PHP <code> without using script tags <?..?> -B <begin_code> Run PHP <begin_code> before processing input lines -R <code> Run PHP <code> for every input line -F <file> Parse and execute <file> for every input line -E <end_code> Run PHP <end_code> after processing all input lines -H Hide any passed arguments from external tools. -s Output HTML syntax highlighted source. -v Version number -w Output source with stripped comments and whitespace. -z <file> Load Zend extension <file>. args... Arguments passed to script. Use -- args when first argument starts with - or script is read from stdin --ini Show configuration file names --rf <name> Show information about function <name>. --rc <name> Show information about class <name>. --re <name> Show information about extension <name>. --ri <name> Show configuration for extension <name>. You can write a php script that does all the work needed as described by Kicken and Socialcloud, and since there is no web server needed, it can be run by the CLI using php -f scriptname.php Combine this with the windows scheduler or cron, so that the program runs periodically, and you have a simple functional offline system. I personally would set it up to run every minute, as 5 minutes is a long time to wait. If you do set it up in linux with cron, you can utilize http://linux.die.net/man/1/flock to insure that only one of your cron jobs is running at a particular time, which can help you safely decrease the periodicity with which you run a script. With cron, you can run scripts every minute if you want. Quote Link to comment https://forums.phpfreaks.com/topic/271792-make-php-run-tasks-in-the-background/#findComment-1398440 Share on other sites More sharing options...
mds1256 Posted December 10, 2012 Author Share Posted December 10, 2012 Hi Thanks for above responses. Looks like cron jobs are the way to go. Quote Link to comment https://forums.phpfreaks.com/topic/271792-make-php-run-tasks-in-the-background/#findComment-1398512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.