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
Related
I use takielias Codeigniter websocket in my project for notification purpose, first of all i am noob for websocket handshaking connection and WS protocol, here every thing working as per the takielias github page documentation, in his git hub page after all setup, need to run cmd command for run server client connection command is php index.php welcome index after run this command in the project path websocket connection is switched and every thing is working fine in my local, after uploading on server there i can't run this command manually,
so i need a help with this, run that command and execute the socket connection, then i decide to run that command via php script, but i can't able to execute that command successfully in local, if any other possible way to make a socket connection with client please assist me,
I really Appreciate your help,
As I understood, you need to execute the command via php script. You can do so using shell_exec or exec.
See documentation here:
exec
shell_exec
Example:
shell_exec("/path/to/php /var/www/html/index.php welcome index '".$parmeter1."' '".$parmeter2."' >> /path/to/logs/welcome.log &");
If you want to run the command in background then it is important to put & at the end.
The extra variables surrounded in single quotes after the path to the script are optional. You can omit them if not needed.
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
I finished a program with ZMQ, built a PHP Socket Program, and in order to accept some Client requests. I must be sure that this Server Program run in linux all the time.
I run this Program like this:php /app/server.php.
And, the Terminal shows my output statement like waiting for client connecting..., at this time, I can't use my Terminal to do others things, unless I Ctrl + c to exit this program.
I want to let it automatical run in linux background like a progress. And the Program may die when PHP Error, I have to restart this program manually.
I also want to it can restart self when error happened.
How to do that? Thank U first:)
Take a look at the System_Daemon PEAR package. I've used it several times and find that it works well.
Try using "Screen" program. you can use the following parameters to de-attach it from the current terminal and have your program running inside it.
screen -d -m php /app/server.php
also to have it autostart at boot time, add the above line to
/etc/rc.local
Use the & symbol at the end of the command to run the program in the background, like so:
php /app/server.php &
You can put the above command in /etc/rc.local so that it starts automatically when the system boots.
If you program produces output, you might want send it to a log file instead of STDOUT.
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
At preset we have need to launch apps on a linux box remotely
To do this we have php script that is run at boot via the rc.local file. This php script watches a command file. This has commands written to it.
The php script has trouble running some apps. For instance it can boot X11, but it can't run an app that is meant for X11.
But, if we run the php script from a terminal, them the system works just just fine
Here is the contents of the rc.local file (this fails).
sudo -u jacob /usr/bin/php /home/listener/ListenerThread.php > /var/www/html/out.txt &
The user jacob as sudo root access with no need for passwords
Please help
Most likely if it's an X11 issue the children aren't having DISPLAY set in their environment, but without error messages we can't help you.
One solution I would suggest is to start X11 at boot and put the line that launches your script into your .xinitrc. This way your script will be able to run GUI programs correctly.
If you don't like that solution, then try running your gui apps from within the script like this: env "DISPLAY=:0.0" your_gui_app