I am looking to run 10 php CLI processes (parallel) using one shell script. but, I am trying to find out how to send a process to background once created.
something like this I am trying. any help will be appreciated. (just the flow, not a real shell script)
for loop 1 to 10
php -q /home/xxx/xxx.php & // I want to send this to background one created. because it runs indefintely depending on the task it needs to do.
end for
sorry, I had the hard time with this.
To answer your question directly,
#!/bin/bash
for i in {1..10}
do
php -q /home/xxx/xxx.php &
done
Try to use Supervisord for scripts like this. It's has a simple configuration file and you just need to set script path and processes count.
Related
I am not very good with bash or shell script.
I would like to know if what I want to do is possible.
I have a big request to do with PHP. Only when I launch it my server is in Timout (and I can't extend it).
I had the idea to split the PHP requests in iterations. Every time I reload my page, my script iterates over a JSON file.
I want to know if I can use a script with a CRON to run my PHP file as long as it has iterations to perform. And if I can use a response from my PHP in my bash to stop the script when everything is finished?
I apologize for my English, thank you for your time.
I have searched the web several times, but I can't find an answer to use a php file with a bash script with iterator.
Thanks in advance,
Thank to Andrea Olivato :
If it's just a timeout problem and your script would work, just run it
from command line. php script.php Assuming you have a non-infinite
loop in your script, it will automatically stop once it finishes
everything
I'm having a problem where putty gets regularly disconnected. So, when I run a PHP script from the terminal, it always gets interrupted. The script is supposed to run several hours, so I'm not having any luck with it.
How can I completely run this from the server side? I'm reading about cron jobs, but I'm having a hard time understanding at this time. Is there any alternative to cron for what I need?
I have several script PHP files that need to be run, one by one, or perhaps two at a time. Any ideas?
You don't need to leave it run in a cron job - you can just run the php script inside a screen.
Simply type;
screen php /path/to/myphpscript.php
A screen will continue running even after you disconnect from PuTTY. If you need to check up on it, you can use;
screen -r
To re-attach yourself to this process, and view any output.
You need to prevent the process from terminating when the session disconnects.
Something like this would work:
nohup php myscript.php
You can create a cron job to start the php script periodically based on a list of time tasks. More info. You could also start the task in the background from the console. i.e. php-cgi script.php& this would make the script a background task
Take a look at GNU Screen; it allows you to detach and reattach a session later, which is perfect for long-running scripts. Cron is a good option if you want it to happen in a recurring fashion; one-off batch jobs can be scheduled with something like at. For more intense computing needs, you might want to look into a more full-fledged job scheduling system like TORQUE.
You can run your program in background
php ./yourscript.php &
I was wondering how to make a php daemon script that runs one time at the day?
Do you know any good frameworks with benefits?
or is it just small code?
Thanks
I was wondering how to make a php deamon script that runs one time at
the day?
In order to do this, get familiar with cron jobs. A cron job is a function that gets executed by the server on a time interval. Usually you'd edit your "crontab" by executing crontab -e
Then, once inside, you'd write the interval you want, followed by the command.
Typically it looks like:
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Since its PHP, you can either a) run your php command as a php cli command, OR b) you can make the command get executed when a particular page is run... and just execute that in cron via a curl -X GET 'http://url/' (etc.)
Also, note that you can write all of your stuff in a shell script file and actually run that file as your cron command... that reduces line-item complexity
cron
Sorry I haven't closed this one.
I actually discovered that my host didn't allowed cron jobs running. So I found a relevant homepage that offer a free service to make a request for me when I needed. In my case, I have specified a url link that should be requested to my RESTful API each day.
The link is here and works like a charm :)
is it possible to launch a php script in background on the webserver with js and let it run even if you change page or not visit the site at all and then get the current status if you call the php script in a second moment?
This php script will process data for hours and sleep for X seconds/minutes for each loops. If what I asked before is possible how can I even get "echos" from it if php will only generated an output only when the script ends?
Maybe this is not a job for PHP?
thank you
EDIT: on a windows machine with apache
It certainly is possible - I have several scripts that run 24/7 written in PHP. Check out Creating Daemons in PHP. It has good info on how to 'daemonize' a php script so that it will run like a service, and it also covers signal handling.
To get debugging output you would redirect to a log file. Do a search on "unix redirect output" as there is a lot of info available.
In Windows it's not much different from UNIX.
First of all, you need to create a PHP script with a run loop. For example, take a look at this: http://code.google.com/p/php-apns/ . This is a PHP "daemon": the main script, PushMonitor.php, runs forever, because it has an infinite loop. It polls a queue at regular intervals, then execute the actions and then wait. Really simple, actually!
The problem, in your case, is that you want to launch the "daemon" from a PHP script.
You may want to look at this: http://robert.accettura.com/blog/2006/09/14/asynchronous-processing-with-php/ (first example code) . You will execute something like launchBackgroundProcess('php myscript.php') .
Note that on the code there's the "start /b" command (and the "&" at the end of the command for UNIX). That is important, because otherwise your process would be killed when the PHP script of the web page is terminated (children process die after parent dies!).
Also, remember that the "php" executable (cli) must be in your path (so you can execute "php" from the command line).
Since the PHP script of the page launching the background process is going to terminate, you can't directly catch the "echoes" in a simple way. My suggestion is to write all output to a file (or a database etc), and then read the contents from that source when necessary.
So, instead of "echo", you will use file_put_contents() etc.
i am working on web Payroll project using symfony framework. we have 27000 employees to process every month. when we doing employee payroll process we can not depend on browser request as it is a long time taking process and server time out coming. as a work around we like to execute the php script from the background on linux server. then even browser closed the scripts can excute the background.
what is the best way to do this task please help
More info
we would like to give the pay roll process start button from the web interface when user click on the button pay roll process should start even browser close it should execute until done from the background.
regards
To run in the backgroud do the following steps:
Wrap your php command in a shell script:
**
#!/usr/bin/bash
# set up environment variables, PATH etc. here
php /home/yourapp/yourscript.php
**
Code up a web page/php script to request the start.
In the script you need the line:
system('/home/yourapp/yourscript.sh > scriptlog.txt &'
You probably need an extra link to browse the "scriptlog.txt" file from the web.
There are different ways to handle this problem.
The first way is to execute , startinan shell command in background (and with using nohup if you like) starting from your PHP code. Thats nearly the same think like the first answer in the duplicate question.
The 2nd way is to use PHP PCNTL Feature.
Using PCNTL you can create child processes, which running in background. So it's possible to make an fork and returning to the user: "pay roll process is running - you will get an mail, if the system is ready".