Exec command in background in php script

This will run two scripts in the background that are not blocking with standard out and error outputting to a file

exec(“php worker_test.php 1 >> /tmp/worker_test.log 2>&1 &”);
exec(“php worker_test.php 2 >> /tmp/worker_test.log 2>&1 &”);

Cannot use LOAD DATA LOCAL INFILE with pdo

SQLSTATE[42000]: Syntax error or access violation: 1148 The used command is not allowed with this MySQL version

1) install the optimized native driver

sudo apt-get install php5-mysqlnd

2) In pdo connect set the local infile flag:

$conn = new PDO(“mysql:host=[host];dbname=[database];port=[port]“, username, password, array(PDO::MYSQL_ATTR_LOCAL_INFILE=>true));

NOTE: do not set local-file in my.cnf. mysql will fail to restart

Prevent Long Running Node Script Timeout

To prevent long running node script running under nginx from timing out need to set setting in two places.

1) Modify /etc/nginx/nginx.conf. Add these lines in the location block.

proxy_connect_timeout 60;
proxy_read_timeout 3600;

2) In node application app.js file where server listener setup.

var server = app.listen(serverPort);
server.timeout = 3600000; // 1 hour in milliseconds

3) Restart nginx

sudo /etc/init.d/nginx restart

Install Redis with PHP

1) install redis server

sudo apt-get update
sudo apt-get install redis-server

2) Make redis use unix socket. modify /etc/redis/redis.conf. Uncomment these lines

unixsocket /var/run/redis/redis.sock
unixsocketperm 777

3) restart redis

sudo /etc/init.d/redis-server restart

2) Install php-dev

sudo apt-get install php-dev
sudo apt-get install php5-dev

3) install from pecl

sudo pecl install redis

4) Add extension to php.ini. Everything in conf.d will be read from php.ini

cd /etc/php5/conf.d
vim /etc/php5/conf.d/redis.ini

Add this to file: extension=redis.so

5) restart php and nginx

/etc/init.d/php5-fpm restart
/etc/init.d/nginx restart

6) Connecting to Redis in php

$redis=new Redis();

7) logic

if(($verifyCheck=$redis->get('somekey'))===FALSE) {
    $verifyCheck = 'someval'.mt_rand();
    // Expire is in secs
    echo 'not cache' . "\n";
} else {
    echo 'from cache' . "\n";
echo $verifyCheck . "\n"

Check if folder is empty in php

1) using glob()

if(count(glob("/tmp/*"))==0) {
    echo 'dir empty';
} else {
    echo 'not empty';

2) using scandir() – will also read hidden files

if(count(scandir("/tmp/*")) == 2) {
   echo 'dir empty';
} else {
   echo 'not empty';

Find and Kill Range of Forked Processes

This will find all process IDs of a forked perl script, loop through them and issue kill command to shutdown all processes that this script runs in. Do as root

ps -ef | grep some.php | grep -v grep | awk '{print $2}' | sudo xargs kill -9 

Override Apache From Envelope Email Address On Linux

If you have upgraded your linux server recently and are unable to send email using PHP mail() you might need to override the envelope From address to a valid email format. The default apache from address is usually apache@local.localdomain. Your server might give you an error saying this is not a valid from address. To overcome this error you must specifically override the envelope From address. This is different from the email message header from address. You do this by passing -f parameter in the optional fifth parameter of mail() function. The from address must be in a valid email format.

$to ="some@yahoooo.com";
$subject = "EMAIL TEST";
$email_content = "Just a test. Do not reply";
$header = "From: apache@test.com";
// This is what makes it work. This parameter is passed to 
// sendmail to override the envelope from address
$params = "-f apache@test.com'";

if(mail( $to, $subject, $email_content, $header, $params)) {
    echo 'email sent';
} else {
    echo 'cannot send';

PHP exec() Error Codes

These are what the exec() error codes mean that are returned in third parameter of exec().

1 – Catchall for general errors.
2 – Misuse of shell builtins.
126 - Command invoked cannot execute. A permission or command not executable problem.
127 - Command not found.
128 - Invalid argument to exit. Exit takes only integer range 0 – 255.
128+n – Fatal error signal “n”.
130 - Script terminated by Control-C.
255* – Exit status out of range. Exit takes only integer range 0 – 255

Installing Sublime Text 2 for Fedora 17 and 18

yum-config-manager –add-repo http://repo.cloudhike.com/sublime2/fedora/sublime2.repo

yum-config-manager –enable

yum install sublime-text

Configure Pidgin to Connect to Google Talk

In the Modify Account dialog

Basic tab settings:

Protocol: XMPP
Username: //is the username portion of your email
Domain: gmail.com
Resource: Home

Advanced tab settings:

Connection security: Use old-style SSL
Connect port: 443
Connect server: talk.google.com
File transfer proxies: proxy.eu.jabber.org