I am currently following a tutorial that teaches how to create a queue in php. An infinite loop was created in a php script. I simplified the code in order to focus on the question at hand:
while(1) {
echo 'no jobs to do - waiting...', PHP_EOL;
sleep(10);
}
I use PuTTy (with an SSH connection) to connect to the linux terminal in my shared hosting account (godaddy). If I run php queuefile.php, I know it will run with no problems (already tested the code with a finite for loop instead of the infinite while loop).
QUESTION: How could I exit out of the infinite loop once it has started? I have already read online the option of creating code that "checks" if it should continue looping with something like the following code:
$bool = TRUE;
while ($bool)
{
if(!file_exists(allow.txt)){$bool = FALSE}
//... the rest of the code
though I am curious if there might be a command I can type in the terminal, or a set of keys I can push that will cause the script to terminate. If there is any way of terminating the script, or if there is a better way to make the previous "check", I would love your feedback!
Pushing Ctrl+C should stop the running program that is running in the foreground.
You could also kill it when you login in another session an do some ps aux | grep my-php-script.php and see if it is your program, then you can use pkill -f my-php-script.php to kill this process.
I understand so that you want to make cron in your server. Therefore you should log in your server via putty and create cron job.
For example:
After logging...
crontab -e
Then add
1 2 3 4 5 /path/to/command arg1 arg2
Related
I have a file named /root/folder/myfile.php that will handle incoming packets from a specific port by a GPS device.
When I use [root#main ~] php /root/folder/myfile.php, everything works fine.
I need this file run every second to listen.
I researched for a while and figured out that using php cli is a solution, so I tried above command but as long as the shell is open (I'm using PUTTY), file is executing and when I close the shell, process will be killed.
How can I (where can I) add a command that will run this file every second, or may be in realtime?
I'm using linux centOS 6.5.
Thanks in advance
nohup php myscript.php &
the & puts your process in the background.
The solution from Run php script as daemon process
To kill it:
1) display all running proceses with: ps aux | less or top command
2) find pid(process id) and kill with: kill pid
You would want to use the cron functionality of your server.
Similar to this maybe:
running a script from cron every second
I have centos VPS hosting and installed WHM/cPanel . I want to run a php script using command line for unlimited time.
My script is look like:
<?php
set_time_limit(0);
while(true)
{
//code to send me email
sleep(600);
}
?>
I know that this script should be run for unlimited time.
I have used these commands:
php myfile.php &
nohup php myfile.php &
I found these commands on stackoverflow. And these are running fine. But after one hours, It stop automatically.
I think, i am doing right. But i do not know, which is killing that process.
If not,
i want to know that How to run this script for unlimited time.
What you are doing is correct. It should run. I have PHP scripts that run for much longer than an hour. They run for days on end. I also have programs that are not PHP that should run forever, but don't. It is because they die due to a bug in the program. For example, xscreensaver dies on me about once a week. To keep it running, I use this shell script (which you can use to keep your PHP running):
while:
do
xscreensaver &
wait
done
Now, running that shell script will start the program again if it ever dies for any reason.
If you have console access try using cronjob to run this file.
see : https://www.centos.org/docs/5/html/Deployment_Guide-en-US/ch-autotasks.html
also : http://alvinalexander.com/linux/linux-crontab-file-format-example
Make sure you use php-command in cron-job
Note : You'll require admin rights to edit/work with cron-jobs
I want to run service in PHP which to be run in the background. I have tried by using exec() function in PHP but service is being run in infinite loop and control is not returning back over PHP file. I have searched more over the internet but I can't find the solution. Please give me some idea or reference to achieve this task.
This is code for reference what I want to do:-
echo"hello";
exec("raintree.frm");
echo"hello1";
raintree.frm is a service which I want to execute. Here PHP script prints "hello" over browser but that is not coming on "hello1" because control gets stuck on exec() function.
If you'd like to have your service running in a separate process, as the title states, you need to create the new process and then run the service in it. In PHP you can create a new process with pcntl_fork() and start the service in the child process. Something like this
echo "hello";
$pid = pcntl_fork();
switch($pid){
case -1: // pcntl_fork() failed
die('could not fork');
case 0: // you're in the new (child) process
exec("raintree.frm");
// controll goes further down ONLY if exec() fails
echo 'exec() failed';
default: // you're in the main (parent) process in which the script is running
echo "hello1";
}
For more clarification read the manual (the link above to pcntl_fork()) as well as look at some C/Unix tutorials on the topics (or rather syscalls) fork() and exec().
You can make use of atd for running arbitrary commands in a separate process:
shell_exec('echo code you want to run | at -m now');
For this, atd should be installed and running, of course.
The exec() function is waiting to receive the output of the externally executed command.
To prevent this from happening, you can redirect the output elsewhere:
<?php
echo 'hello1';
exec('raintree.frm > /dev/null &');
echo 'hello2';
This example will output "hello1hello2" without waiting for raintree.frm > /dev/null & to finish.
(this probably only works with Unix-like operating systems)
My project calls for 3 php scripts that are run with if-else conditions. The first script is loaded on the index page of the site to check if a condition is set, and if it is, it calls for the second script. The second script check to see if other conditions are set and it finally calls for the last script if everything is good.
Now I could do this by just including the scripts in the if statement, but since the final result is a resource hogging MySQL dump, i need it to be run independently of the original trigger page.
Also those scripts should continue doing their things once triggered, regardless of the user actions on the index page.
One last thing: it should be able to run on win and nix.
How would you do this?
Does the following code make any sense?
if ($blah != $blah-size){
shell_exec ('php first-script.php > /dev/null 2>/dev/null &');
}
//If the size matches, die
else {
}
Thanks a million in advance.
UPDATE: just in case someone else is going through the same deal.
There seem to be a bug in php when running scripts as cgi but command line in Apache works with all the versions I've tested.
See the bug https://bugs.php.net/bug.php?id=11430
so instead i call the script like this:
exec("php-cli mybigfile.php > /dev/null 2>/dev/null &");
Or you could call it as shell. It works on nix systems but my local windows is hopeless so if anyone run it on windows and it works, please update this.
I would not do this by shell exec because you'd have no control over how many of these resource-hogging processes would be running at any one time. Thus, a user could go click-click-click-click and essentially halt your machine.
Instead, I'd build a work queue. Instead of running the dump directly, the script would submit a record to some sort of FIFO queue (could be a database table or a text file in a dir somewhere) and then immediately return. Next you'd have a cron script that runs at regular intervals and checks the queue to see if there's any work to do. If so, it picks the oldest thing, and runs it. This way, you're assured that you're only ever running one dump at a time.
The easiest way I can think is that you can do
exec("screen -d -m php long-running-script.php");
and then it will return immediately and run in the background. screen will allow you to connect to it and see what's happening.
You can also do what you're doing with 'nohup php long-running-script.php', or by writing a simple C app that does daemonize() and then execs your script.
I've created a php script that allows me to click a button to restart a PHP script. However, I'm not sure the best way to do it. Here's a snapshot of it: http://i51.tinypic.com/2niz32o.png
I currently have this:
if(isset($_POST['login_restart']))
{
$command = exec("/usr/bin/php /var/www/html/login_server.php >/dev/null &");
$pid = exec("nohup $command > /dev/null 2>&1 & echo $!");
$info = "Login server started...PID: $pid";
}
However, that doesn't seem to work. I need it so when the "Restart" button is pressed, it starts the login server, and keeps it running. I've been using the screen function in SSH, however, I don't want to have to keep logging into SSH to restart the login server. I want to somehow use a process ID so I can check to see if the script is running, and if it's not, it'll allow me to click the "Restart" button.
Thanks.
Is there a particular reason that you want to do this manually and not automatically? Is it not the case that the server should always be restarted?
My advice would be to automate this, either by using cron to check the status of your script at regular intervals, or bash infinite loop script immortality.
First create a launcher script to invoke your PHP for convenience, and call it run_login_server.sh (don't forget to chmod +x it so it can be executed):
#!/bin/bash
/usr/bin/php /var/www/html_login_server.php > /dev/null
Then create login_server_daemon.sh to run your script in an infinite loop (again, chmod +x it to make it executable):
#!/bin/bash
while :
do
./run_login_server.sh # or any command line to be executed forever
done
N.B. I have not backgrounded the php process in the above bash script. It works, because the bash loop will call php each time, and the loop will only iterate again once php has died. Just execute login_server_daemon.sh to start the loop (either through an init service or in a detached screen session like you are using now).
If your PHP scripts hang, or you want to reload them because you have updated your code, you can simply kill the looped process–run_login_server.sh and the bash loop will respawn it.
It's as simple as killall run_login_server.sh, which you could do via php's exec. Note that you need to be careful about the user permissions of who has executed what: if you execute login_server_daemon.sh as your_username but php runs as php_username then php will not have permission to killall your process.
Finally, if you can't choose between cron and the script approaches, here are some factors to consider:
The script should live forever, and will only die if 1) explicitly killed, 2) bash somehow trips and dies on a while loop, which I doubt would happen, and 3) a machine-wide catastrophe happens, in which case your little bash script stopping is the least of your worries. A bonus with the script is that restart is immediate after php (or whatever you want to call in the infinite loop) dies.
cron has a the problem that it can only check once a minute at its most frequent setting, if you really care about immediate recovery. It has the additional annoyance that if you decide to stop the script, you also have to remove it from your crontab or it will just come back to life.