Showing posts with label Php. Show all posts
Showing posts with label Php. Show all posts

Thursday, March 27, 2014

Run PHP As a Background Process

I would like to run a PHP script as a background process on my OSX machine. In the test script, I use file_put_contents method to write the log in a specific file.  When I directly execute the command line PHP script, I can see that the log is written into the file, which means, everything is fine. But, when I use it as a background process, it is not working. I have tried many ways that just failed to run script as a background process. At the end, I solve the problem. I want to record how I make it work at the end.

The commands which I used were failed:

 php /path/to/my/test/script/cli_test.php argument1 &
 php -f /path/to/my/test/script/cli_test.php argument1 & 
 php /path/to/my/test/script/cli_test.php argument1 > output.txt 2>&1 &
 nice -20 php  /path/to/my/test/script/cli_test.php argument1 > output.txt 2>&1 &
 nohup  /path/to/my/test/script/cli_test.php argument1 1>/dev/null 2>/dev/null &
 php /path/to/my/test/script/cli_test.php argument1 > /dev/null 2>&1 & echo $! 

The result I got was always showing a sudden stop like below -

 [1]+  Stopped     nohup php /path/to/my/test/script/cli_test.php argument1 > /dev/null 2> /dev/null

And, the process keeps hanging in the background, never die.

I found the solution in a discussion - http://ubuntuforums.org/showthread.php?t=977332 .
According to the discussion that the discussion says, I tried the following command and it worked very well.


php -q /path/to/my/test/script/cli_test.php argument1 < /dev/null
nohup php -q /path/to/my/test/script/cli_test.php argument1 </dev/null 2> /dev/null & echo $! 

I do not know where the bug sits, is it a PHP command line or OSX bug? Ubuntu users think it is a OS level bug, but it seems to me it is a PHP command line bug. Anyways, as long as it works and I do not have to use other solution like "pcntl_fork".


Thursday, February 20, 2014

PHP MSSQL database driver installation

Install FreeTDS for Linux or MacOS driver. The driver from Microsoft is not working for linux based system.

(1) Download the latest FreeTDS from the official website : http://www.freetds.org

(2) Using compiler is better. Even though there are some binary package that you can download, it is sometimes not working. Compiling the driver often work perfectly.

(2.1) Compile FreeTDS (take version 0.82 as an example)
Extract the package:
tar zxvf freetds-0.82.tar.gz

Go to the directory:
cd freetds-0.82

./configure --prefix=/usr/local/freetds --with-tdsver=8.0
make
make install


- with-tdsver = 8.0 refers to the tds 8.0 version ( if this parameter is not used, according to the default compiler, it compiles to 5.0 and 5.0 connection to the database is 4000 , not SQLServer default port 1433 . )

(2.2) Compile PHP Module
To support for MSSQL, adding compiler parameters -- with-mssql = /usr/local/freetds.

Enter the php source directory, MSSQL module's source directory:
cd /home/wxw/gd/php-5.4.17/ext/mssql/

Generate compile profiles
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-mssql=/usr/local/freetds/
make
make install


Add the mssql.so into php.ini, the path will be prompted after installation
vim /usr/local/php/lib/php.ini
extension = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/mssql.so"

Restart Apache server
/usr/local/apache2/bin/apachectl restart

During the compilation, if we encounter an error of not being able to find FreeTDS, like the following.
configure: error: Directory /usr/local/freetds is not a FreeTDS installation directory

You need to create two emtpy files.
touch /usr/local/freetds/include/tds.h
touch /usr/local/freetds/lib/libtds.a

To build and install the PDO_DBLIB extension, in Terminal, go to the new PDO_DBLIB extension directory (php-5.4.17/ext/pdo_dblib) and run the below commands to build the pdo_dblib extension.

phpize
./configure--with-php-config=/usr/bin/php-config--with-pdo-dblib=/usr/local/freetds/
make
sudo cpmodules/pdo_dblib.so/usr/lib/php/extensions/no-debug-non-zts-20100525/


We need to edit is php.ini so open php.ini in your text editor with root privileges.
Search for the line ; extension_dir = "./"
Replace the above line with extension_dir = "/usr/lib/php/extensions/no-debug-non-zts-20100525/"

Adding the extensions.
extension=pdo_dblib.so
Save the file and exit