Link to home
Start Free TrialLog in
Avatar of msknr
msknr

asked on

Run Script in Background

I have an mailing list script that emails a list of emails when I pass it a subject and a body via POST.

The script takes quite a while to complete.  I am wondering if there is a way I can execute the script and then allow it to run alone on the server so I don't have to keep my browser window open.

If I wanted to do this in shell, I would just type <command> &
then I could forget about it.

Is there an easy way to do this in php?  Or will I have to execute a shell command from my php script?  If so, how should I go about doing that?
Avatar of rstorey2079
rstorey2079

You can use PHP at the command line in a shell.  

Do "whereis php" to find out where your php executable is on your system.

Then, you should be able to do

/path/to/php scriptname.php > output.txt

if you want to save any output generated by your script...it would be in output.txt
Avatar of msknr

ASKER

My plan is to allow a few people access to this script, but I dont want to give them shell access.  That is why I was hoping to perform all executions via the web browser.  As far as output goes, I am having results emailed to the admin after the script completes the process.

Avatar of msknr

ASKER

to get an idea of how slow the execution is, check out the url:

http://www.troysbucket.com/sendmail

feel free to test it out, the tests will only be emailed to me.

Mike
ASKER CERTIFIED SOLUTION
Avatar of shmert
shmert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msknr

ASKER

shmert,

I am using the mail() function.  How can I make use of the queue mechanism?
Use a cron job.

"lynx --dump http://www.ohashi.us/script.php"

And just setup the time to whatever you want.  This will execute the file at a certain time every (whatever you specify).  Then you can just have your script load that info into a database and have the script the cron job runs load from that database.


user a-->  adds two emails.
user b-->  adds 3 emails
cron job runs-->  loads a database of 5 emails and sends them and finishes.

Would that work?


hope that helps,
-kohashi
I've made a comment here: https://www.experts-exchange.com/questions/20784451/php-code-architecture-thoughts-and-suggestions.html#9659961 , that might be of use.

It would mainly involve writing a fork() function, or a fork-wrapper for PHP.
Avatar of msknr

ASKER

I ended up using the system() command.

my sendmail scrip adds the email to a database that looks like this

id    subject    body    sent
1    hello        test      0

then I call
system(php send.php);

send.php then looks in the db for any row that has sent=0
Next it sends out all the emails to members that exist in the members table.
when it is finished, it emails me any output.  (ie.  #of emails sent, errors, execution time)

as long as send.php does not print ANY output, when you use system() it'll run on the server by it self and you can close your browser or navigate to a new page.

thanks for all your help!
also take a look at shell_exec and/or backtick operators (`'s)
hello, msknr.

It sounds like the process still isn't quite running in the background.  Try this syntax to get the php call to system() to return _immediately_

system('/usr/local/bin/long_script > /tmp/php_out &');

This redirect output to the /tmp/php_out file, and the ampersand tells it to run in the background.  Both of these steps are essential.
Just posted this in the other thread, too...

Try this syntax to get the php call to system() to return _immediately_

system('/usr/local/bin/long_script > /tmp/php_out &');

This redirect output to the /tmp/php_out file, and the ampersand tells it to run in the background.  Both of these steps are essential.
Avatar of msknr

ASKER

cool,
it seems to be working, thanks for the help.

Mike