Okay, this is going to be a very weird request/question.
There is a very long running PHP script that needs to be launched by the user (admin) who is not very technically adept. When running the script through apache, it throws a timeout (502 or 504 Bad Gateway).
Let's just assume that apache can't be configured to fix the timeout issues.
I want to create a button in the admin panel that sends an AJAX call to a PHP script on the server, that PHP script will act as a proxy of sorts to launch a shell command. The shell command will then execute the long running PHP script with certain arguments... but I don't want it to wait for the long running script to finish. The proxy PHP script can exit and return true/false based on if the shell command actually started (this part is optional).
Essentially, have PHP launch a shell command which launches a PHP script.
How can I pull something like this off?
Have you tried shell_exec. It worked for me...
http://php.net/manual/en/function.shell-exec.php
Related
Now I am trying to execute php websocket script as a background. For it, I used below command on the AWS terminal.
php chat-server.php >> log.txt &
But strangely it is terminated without any errors.
My question is two.
Why the php command is terminated on AWS? Is there any limitation to run a php script continually as a background service
How can I run it permanently on AWS? I understand I can use a linux script to run my web-socket server permantely (maybe linux script will be re-lunch my php script when it will be terminated). But I don't know well linux commands. Who can help me?
Thanks for your advice!
First, enable PHP logs to see if there are any errors.
Then, to run it permanently, you can use screen or Tmux. It's simple to use and lets you detach the process and run it in the background. So you are able to logout from your ssh session, and the process is still running.
Please read:
https://wiki.archlinux.org/index.php/tmux
http://linux.die.net/man/1/screen
I need to use an Apache handler to run a PHP script, rather than running it through CLI. I'm using APC user cache, which stores variables using the Apache process. If I run my PHP script through CLI, then it won't have access to the APC variables.
A possible solution is creating a directory restricted to localhost and putting my scripts in there. Then, I can use a browser to run the PHP scripts. However, I'm not too experienced with Linux and I don't know how to implement this. Here's how I need it to work:
One of the cron job fires.
The cron job opens the PHP script using a web browser.
After the PHP script is finished processing, the web browser closes.
I don't know how to close the browser once the task is finished. Also, multiple PHP scripts will be running simultaneously (called by different cron jobs), I'm not sure how this will work. I'm using the Lynx browser on CentOS.
In Debian/Ubuntu I can run a script using lynx, say
/usr/bin/lynx -source 'url'
For eg:
/usr/bin/lynx -source http://google.com
Once execution is completed, the browser quits default.
I need to keep a php script running and alive on my server, the script runs and checks a DB for record, processes if needed, sleeps for 3 and then loops to the top of the script in an infinite loop. The issue is launching it, if I launch it via terminal (its running on an ubuntu system) using php script.php then if the terminal session is ended the script stops running.
So how can I go about launching the script so that it will remain running in the background.
Furthermore if I set up a cron job that runs once an hour and fires off a different script that check the primary one is still running and if not restarts it, how would I get the this checker script to check that the initial script is still running (even if its in sleep).
Any assistance will be greatly appreciated
If starting the script from the web is an option, then set time limit to "unlimited" (in the script itself):
set_time_limit(0);
and set ignore_user_abort to "true":
ignore_user_abort(true);
Then you can run the script as usual from the web, and it will run forever (until the process is killed or script exits in the usual way).
Of course, you can (and MUST) protect such a starter-script by password, e.g. by using HTTP authentication via .htaccess, so that somebody cannot start many forever-running scripts, which would lay down your server.
On checking whether another process is running, see question1, question2, or try searching "[php] check if process is running" here on StackOverflow. See also http://php.net/manual/en/refs.fileprocess.process.php.
If you want to run it from the terminal and keep it running permanently, look into GNU screen. It's a virtual terminal that keeps running in the background even when you close the terminal.
$ sudo apt-get install screen
With this, you can simply do:
$ screen php myscript.php
The script will load in a screen session which you can disconnect from, and even when you close the terminal it will keep running. To check up on it, simply call:
$ screen -x
Best part is screen is free software and is in the repo of all good distros (and other *NIX's if Linux doesn't float your boat).
Cron Job would be one solution:
More details about Cron job.
Another way to do it is to use Gearman or some other taks managers like in this post
I run PHP script in command line from CRON job. Once it complete a process, sometime PHP process still run on task manager as well as command prompt is active in windows forever, unless i kill manaully process from task manager. I put log file, it shows a code is working properly and end of the script. However, it does not exit from process and consume memory of server. I wonder is this bug of php ?
I run application in windows server 2003 and php is in CLI.
i start a linux console app from my php5 script, it starts ok but then termintates. I've tried using system(), shell_exec and tried starting as background process but to no avail it starts and then quits.
What i am trying to achieve is from a remote browser start a console app using a php5 script and then it should remain running (just as it would if i started it from a bash shell) , i then want to send commands (from a bash shell it would be keyboard strokes) to the console app from another set of php5 scripts. Hope its clear what i am trying to do.
If anyone could give some info on the best way about doing this, as i think i may have something fundamentally wrong.
I have a Debian Lenny box running apache.The console app is just a simple program that prints to stdout and reads from stdin.
How do you expect to send input to this app? Where is it listening for input?
It simply may only support interactive use, and exit as a result of that. Or, even simpler, it may terminate because it sees that is has no input (nothing piped in or nothing from some file) and since it's not connected to an interactive shell, it has nothing to do. There's no point in waiting for input from a user that doesn't have a way to interact w/ the application.
On every request, PHP starts up, compiles your script and executes it. After execution, the script exists. When the script exits, all of the resources it was using, including file handles, database handles, and pipes to other programs are terminated.
You're going to need to find another way to keep your program open and have PHP communicate with it. Otherwise, every request to your script is going to open a new copy of the program, and then both will exit when the PHP script is complete.
Unfortunately without knowing what the program is, it will be hard to offer suggestions on how to go about doing this.