How to stop a Script php running for ever? - php

I recently made the test to know if a PHP script will stop even if I disconnect. The answer was no, the script is still running and flooding my database. It's while(true) script, so do you guys know how to stop it from running?
It's not on a dedicated server that I have access... I only have the ftp, ssh , mysql access
I already tried to rename the name of the file that was executed, but it's still running

Try to get list of all processes via ssh using
ps -ax
and find process of your script. If you find this process you can kill its with
kill %pid
where %pid is the process id from table of processes.

Related

How to stop the execution of a php script in another page?

I've just learned how to setup and start a web-socket server using Ratchet.
I've learned how to start the script using a command from the terminal and the script keeps running forever.
What I want to know now, is how can I start and stop that script (the web-socket server) using php code, not terminal, So that I can have a button to start and stop the web-socket server whenever I want ?
This question might seem naive, but I don't have a clue on how to this, I am new to Ratchet and web-socket.
If you know the pid, then just use command,
exec("kill -9 $pid");
Otherwise, you can use
exec("pkill $progname"); //To kill a any command with name as $progname
Please refer to this for more such options.
How to kill all processes with a given partial name?

How to kill a process in remote Ubuntu server using PHP?

Is there any command that I can run to kill a specific process in remote Ubuntu server using PHP?
Also is there any command to list all the PHP process running in a remote Ubuntu server?
I was using ,
ps aux | grep php
to list all PHP process, after logged into remote machine using ssh. But then, is it possible to get process list from local machine itself?
Note: I am running some set of cron jobs every 15 mins. And keeping process ID for each in DB. There are scenarios that I need to kill certain process ID from my monitoring tool(in another server).
If there is any command, then that I can use in my PHP script to call.
Thanks!
This question could be on askubuntu.
If you have an SSH access to your server you are logged onto it.
Then, you can do sudo killall php to kill all PHP process or sudo kill <idprocess>.
Note that if you have an Apache server running, it can create new processes. Turn Apache off can allow to do not create new processes.
EDIT:
According to this post you can use a package to give password directly without interactivity. Thanks to that, it is possible for your script to sign in on the server, then kill processes, and finally sign out.
You can use the posix_kill() function to send any signal to a process. Depending on your privileges, you may or may not see the results you expect.
The SIGKILL constant will send (as you'd expect) SIGKILL.
You really shouldn't be executing anything involving a combination of exec() and sudo under any circumstances. It implies that your setup is vulnerable to a lot of potential nasty people.

Run PHP script "forever" in a VPS server

I am sure this must be very easy, but I have no experience with unix.
I have a PHP script that is a TCP client and registers received data into a mySQL database.
How can I have that PHP script running forever? I have root access via a console.
From time to time I may need to stop it, change it and restart it again.
Please advice.
Kind regards
If script don't exit itself (have a loop inside), you can start it by php script.php &

ssh2 execution lifetime on the remote side

I am trying to run a bash script using php-ssh2. The script must run forever in the remote machine (the only way to stop it must be with pkill). The problem is that somehow the connection is closed and the bash script is killed.
Nohup, disown and screen... I tried everything and nothing really changed, it simply doesn't keep that script alive.
What can I do?
(I know that this is a security hole (HUGE) but this is just experimental, the main idea is using an HTML button, run a bash script in the server computer, using apache2)
Create a frequent cron job (every minute?) that first checks some kind of a flag (e.g. existence of certain file) before running a job.
In the PHP code, only raise the flag (create the file).
Does the script generate output? If not, check out the keep alive option of ssh.

keep a php script running all the time

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

Categories