I'm running a php script in my terminal, here's the code :
php -S localhost:8080 script.php
To echo a data, I have to open a web browser with the url localhost:8080 and wait for the script to finish.
I want to know if it is possible to log data into the terminal instead of the web browser? And also, if it is possible to log the data live and not to wait for the script to finish.
Supposing you are running in a Linux Console: php -f /path/to/file/script.php should do the trick.
Related
I want a program (written in php) to run on a remote server even if after I logout. The program is very simple: it does nothing but sleep for 10s (just for a test), like the following:
function index()
{
while(true)
{
sleep(10);
}
}
So I connect to the remote server via SSH. And then launch the program like this:
nohup php -f index.php &
I want it to run in the background on the server after I log out. But I find that everytime after I close the terminal the program can only run for around 10 minutes and then stop, though it does not stop immediately after terminal closed. If I do not close the terminal, it can keep running forever (as expected). Could anyone tell me what is the reason? And how to solve the problem? I have also tried using "disown" as is suggested in this post but got the same problem: How to make a programme continue to run after log out from ssh?
By the way, I am using shared remote host, so it could be due to some server settings but it's strange because it works fine with terminal open.
You can try using disown right after your nohup command.
If it still doesn't work, consider using screen instead. It is a useful utility allowing "virtual terminals" to run, even after logout.
Create a screen using screen -dmS someName. (e.g. screen -dmS myPhpScript)
Enter your screen using screen -r, your window will be cleared.
Execute your command (php -f index.php, no &!)
Exit the screen, by doing [Ctrl]+[A] (which seems to do nothing), and then pressing [D]. The screen will stay in background. You'll come back to the previous prompt (just before step 2), with a message indicating [detached from XXXXX.someName].
You can get back to the screen using screen -r or screen -x someName.
It turns out to be a server issue. A2hosting does not allow a process to run in the background for their shared hosting. So the process will be killed some time (not immediately) after logout.
I'm trying to execute a PHP script from my apache webserver. I tried a Hello World page and it worked, but when I run this findmyiphone script, it shows 500 error from the webserver. What's wrong?
I tried to execute the example.php in terminal by doing php -f example.php, how would I do that from a browser in my web server
Answer is I need to restart the server
I have a python script that picks an URL as parameter and it fetch some data (normally it takes 30 seconds to finish) and creates a file with it. What I want is to be able to call the script from a web (I thought about doing it with PHP but i don't mind) and just get the file path (which is printed at the begining of the script process) and leave the script running in background.
how can i do this? which is the best way?
Note: I'm using a raspberry pi as a web server and the python file is located in /var/www/
php can execute system commands using the command system().
To execute a python script, you can use this command:
system('/path/to/python /path/to/test.py');
To leave it running in the background, use &:
system('/path/to/python /path/to/test.py &');
If you want to start the script in the background but still get the first lines of output, I would redirect the output to a file:
system('/path/to/python /path/to/test.py >/var/www/unique_file.txt &');
sleep(500); // Script has printed the file path now
$output = file_get_contents("unique_file.txt");
I am trying to send keypresses to a Raspberry Pi running Raspdbian from from a web page hosted on an Apache server at /var/www.
I'm using xdotool, which can send keypresses using commands like this:
xdotool key C
I made a python script xdotool.py that sends a few keypresses like that:
#!/usr/bin/env python
import os
print 'sending HI'
os.system("export DISPLAY=:0")
os.system("xdotool key H")
os.system("xdotool key I")
I am SSH'd into the Raspberry as user Pi, and running this script from terminal works fine and sends the keystrokes "HI" to the Raspberry.
However, I want to be able to do this through a web page. I've made a php-script that runs python scripts and displays the terminal output in the browser, but it does not work with the xdotool.py script. I have given the script permission to be viewed/changed/executed by all users.
I've also changed the Apache settings in /etc/apache2/envvars and changed the user from www-data to pi. The user pi should have all necessary permissions.
The script does run, and "sending HI" is displayed in the browser. But it doesn't send the keystrokes. The error I get from error.log is this:
Error: Can't open display: (null)
Failed creating new xdo instance
Any ideas how I can accomplish this? If there's a way to give everyone permission to do everything, that wouldn't be a problem seeing that this server will only run on a local network with no internet access.
I am trying to run a php script on my remote Virtual Private Server through the command line. The process I follow is:
Log into the server using PuTTY
On the command line prompt, type> php myScript.php
The script runs just fine. BUT THE PROBLEM is that the script stops running as soon as I close the PuTTY console window.
I need the script to keep on running endlessly. How can I do that? I am running Debian on the server.
Thanks in advance.
I believe that Ben has the correct answer, namely use the nohup command. nohup stands for nohangup and means that your program should ignore a hangup signal, generated when you're putty session is disconnected either by you logging out or because you have been timed out.
You need to be aware that the output of your command will be appended to a file in the current directory named nohup.out (or $HOME/nohup.out if permissions prevent you from creating nohup.out in the current directory). If your program generates a lot of output then this file can get very large, alternatively you can use shell redirection to redirect the output of the script to another file.
nohup php myscript.php >myscript.output 2>&1 &
This command will run your script and send all output (both standard and error) to the file myscript.output which will be created anew each time you run the program.
The final & causes the script to run in the background so you can do other things whilst it is running or logout.
An easy way is to run it though nohup:
nohup php myScript.php &
If you run the php command in a screen, detach the screen, then it won't terminate when you close your console.
Screen is a terminal multiplexer that allows you to manage many processes through one physical terminal. Each process gets its own virtual window, and you can bounce between virtual windows interacting with each process. The processes managed by screen continue to run when their window is not active.