Is it possible to launch a php script at apache startup? - php

As the title says, I want to know if it is be possible to automatically launch a PHP script when a restart of apache is done.
MORE INFO EDIT:
I will try to explain what is the purpose of this, the best I can. We are currently refactoring our application and we'll be stuck with 2 differents configuration file system for the time being, until all of the application flows are refactored (might take more than a year). The old one is using simple flat file in the key=value format (i.e. www.conf), while the new system will use cacheable php files (i.e. www.php). We need to replicate to www.php any config changes made in www.conf.
Since Apache gets restarted whenever there is a config change in www.conf, I thought it might be a good workaround solution to launch a PHP script, that would replicate the www.conf to www.php.

You need to modify you startup script for your apache.
Open your startup script, it should be in /etc/init.d/apache or apache2
Search for the start / restart section and add your cli call for your PHP script.
Example:
restart)
[..]
php -q /tmp/myscript.php &
;;
Where /tmp/myscript.php is your php script that you want to launch.
The "&" at the end will start the script in the background so your startup will not wait until your php script has ended. If you want to wait until it has ended, remove the &.
You should not put such thing into your startup scripts, there might be better solutions. What are trying to achieve?

At the risk of offending people (like myself) who prefer neat clean solutions, is changing the Apache's default start script an option for you? If so, that'd be the simplest solution

Related

PHP background process in safe mode

In my php project i should have some background process , but in safe mode , because I'm running it on a shared host.
For example my background process code is in the file bg.php and I want it to be executed , write at finish of another specific script.( or maybe with some delay )
I searched a lot. some suggested libraries like beanstalkd but i think this library is heavy for my simple background process and also doesn't have good doc for PHP. some others said functions like exec() which is not possible in safe mode.
1- Does anyone have a simple solution for this problem?
2- I were also mentioned by cron jobs existing in cpanel which
prepare tasks to be executed on a specific time. can I use this
option to solve my problem in some way?
I noted I want a simple and lightweight solution. any suggestions appreciated deeply.
It's not possible use a PHP file served by a HTTP Server because all proceses will die after the request end.
So cPanel cron is a good option, you don't need to have the cron entry always enable, just setup once the entry and disable it after the script start to run.
Only you'll need add & at the end of your cron command to make it run as background.

thttpd and php in CGI mode

Is there a way to make PHP and thttpd work together in CGI mode? There are some pieces of information here and there but our naive attempts failed so far as it seems like PHP doesn't understand the environment variables set by thttpd and therefore fails to find the script file.
Building php as thttpd module is not desirable due to custom build configuration (it's an embedded device) - but possible as a last resort. However, I'd like to avoid that using CGI approach if possible.
Wrap
/cgi-bin/php.cgi:
#!/bin/sh
export DOCUMENT_ROOT=/home/alex/thttpd/www
export SCRIPT_NAME=/cgi-bin/test.php
export SCRIPT_FILENAME=/home/alex/thttpd/www/cgi-bin/test.php
exec /usr/bin/php-cgi
or patch thttpd to export a suitable env for php-cgi
This may not be the answer you're hoping for, but there seem to be no development in thttpd. I'd recommend mongoose. MIT-licence, good for embedded and easy php setup,
Runs on windows as well.
php setup: go WindowsUsage and scroll down.
UPDATE: new link
http://cesanta.com/docs/PhpWebsite.shtml

Execute another PHP file without waiting for it to finish its execution

I have StartServer.php file that basically starts a server if it is not already started. Everything works perfect except that the StartServer.php will hang forever waiting for the shell_exec()'d file Server.php to finish its execution that it never does.
Is there a way to execute a PHP file and just forget about it -- and not wait for its execution to finish?
Edit: It has to work on Windows and Linux.
This should help you - Asynchronous shell exec in PHP
Basically, you shell_exec("php Server.php > /dev/null 2>/dev/null &")
You'll need something like the pcntl functions. Only problem is that this is a non-windows extension, and not suitable to run inside a web server. The only other possibility I can think of is to use the exec function and fork the current process manually (as suggested in dekomotes's answer), doing OS detection to figure out the command needed. Note that this approach isn't much different to using the pcntl functions (in Linux, at least) - the ampersand character causes the second script to be run inside different process. Multi-threaded / multi-process programming isn't well supported in PHP, particularly when running inside a web server.
I think it's traditional to let the server detach itself form the parent process, ie to "daemonize" itself, rather than having the script starting the server detach itself. Check the server you're starting to see if it has a daemon-option.
If you've written the server yourself, in PHP, you need to detach it. It looks somehting like this:
posix_setsid(); //Start a new session
if(pcntl_fork()) {exit();} //Fork process and kill the old one
I think this works on Windows too. (Not tested.)

How to achieve single-processing mode running php scripts?

I have cron job - php script which is called one time in 5 minutes. I need to be sure that previously called php script has finished execution - do not want to mix data that's being processed.
There are three approaches I used to apply:
Creation of auxiliary text file which contains running-state flag. Executed script analyzes the contents of the file and breaks if flag is set to true. It's the simplest solution, but every time I create such script, I feel that I invented a bike one more time. Is there any well-known patterns or best-practices which would satisfy most of the needs?
Adding UNIX service. This approach is the best for the cron jobs. But it's more time consuming to develop and test UNIX service: good bash scripting knowledge is required.
Tracking processes using database. Good solution, but sometimes database usage is not encouraged and again - do not want to invent a bike, hope there is a good flexible solution already.
Maybe you have other suggestions how to manage single-processing of php scripts? Would be glad to hear your thoughts about this.
I'd recommend using the file locking mechanism. You create a text file, and you make your process lock it exclusively (see php flock function: http://us3.php.net/flock). If it fails to lock, then you exit because there is another instance running.
The advantage of using file locking is that if your PHP scripts dies unexpectedly or gets killed, it will automatically release the lock. This will not happen if you use plain text files for the status (if the script is set to update this file at the end of execution and it terminates unexpectedly, you will be left with untrue data).
http://php.net/flock with LOCK_EX should be enough in your case.
You could check wether or not your script is currently running using the ps command, helped by the grep command. "man ps" and "man grep" will tell you all about these unix/linux commands if you need informations about these.
Let's assume your script is called 'my_script.php'. This unix command :
ps aux | grep my_script.php
...will tell you if your script is running. You can run this command with shell_exec() at the start of your script, and exit() if it's already running.
The main advantage of this method is that it can't be wrong, where the script could have crashed, leaving your flag file in a state that would let you think it's still running.
I'd stick to version number 1. It's simple and works out. As long as you only wan't to check whether the script has finished or not it should be sufficent. If more complex data is to be remembered I'd go for version 3 in order to be able to 'memorize' the relevant data...
hth
K

How can I execute CGI files from PHP?

I'm trying to make a web app that will manage my Mercurial repositories for me.
I want it so that when I tell it to load repository X:
Connect to a MySQL server and make sure X exists.
Check if the user is allowed to access the repository.
If above is true, get the location of X from a mysql server.
Run a hgweb cgi script (python) containing the path of the repository.
Here is the problem, I want to: take the hgweb script, modify it, and run it.
But I do not want to: take the hgweb script, modify it, write it to a file and redirect there.
I am using Apache to run the httpd process.
Ryan Ballantyne has the right answer posted (I upvoted it). The backtick operator is the way to execute a shell script.
The simplest solution is probably to modify the hgweb script so that it doesn't "contain" the path to the repository, per se. Instead, pass it as a command-line argument. This means you don't have to worry about modifying and writing the hgweb script anywhere. All you'd have to do is:
//do stuff to get location of repository from MySQL into variable $x
//run shell script
$res = `python hgweb.py $x`;
You can run shell scripts from within PHP. There are various ways to do it, and complications with some hosts not providing the proper permissions, all of which are well-documented on php.net. That said, the simplest way is to simply enclose your command in backticks. So, to unzip a file, I could say:
`unzip /path/to/file`
SO, if your python script is such that it can be run from a command-line environment (or you could modify it so to run), this would seem to be the preferred method.
As far as you question, no, you're not likely to get php to execute a modified script without writing it somewhere, whether that's a file on the disk, a virtual file mapped to ram, or something similar.
It sounds like you might be trying to pound a railroad spike with a twig. If you're to the point where you're filtering access based on user permissions stored in MySQL, have you looked at existing HG solutions to make sure there isn't something more applicable than hgweb? It's really built for doing exactly one thing well, and this is a fair bit beyond it's normal realm.
I might suggest looking into apache's native authentication as a more convenient method for controlling access to repositories, then just serve the repo without modifying the script.

Categories