LAMP On Focus

Icon

PHP, MySQL, Linux practical usage.

Daemons in PHP

A ‘daemon’ in Linux is program that runs continuously in the background without user intervention processing data.
For instance if we need to parse a log file, or process uploaded videos.

So we write a script and `fork` it separately as an individual process, this can be done in PHP using the Process Control Extensions. Getting a good grip on it, will take some studying. Though the time spent on it would be a valuable if you are in for a lot a heavy duty processing. The pcntl extensions are not available in PHP by default, you have to compile the CGI or CLI version of PHP with –enable-pcntl configuration option when compiling PHP to enable Process Control support.

An another way to run a script in background when pcntl functions are not available to you, would be by writing the script in a unconditional while loop. But make sure you have the logic to stop the process on failure within the loop.

while (1) {
//The complete process

  if (condition_to_stop === true) {
    break;
  }

Then execute this from a control script. Start this a separate process redirecting all the out put to the void.

$processId = shell_exec("nohup /usr/bin/php process.php 1>/dev/null 2>&1 &");

Make sure you do not depend on any output printed in the screen by process.php, as in the above command we send all the output including the error’s into the /dev/null black hole.

Run ifconfig as a non-root user

Recently I needed to extract the ip number of the box on which my script was running, and had to decide the behaviour of tool.
Upon digging around, worked out a command snippet that worked, as given below.

ifconfig eth0|sed -n "/inet addr:.*/{s/.*inet addr://; s/ .*//; p}"

But the problem was ifconfig only works as a `root` user if you try it as a normal user you would get a command not found.

$ ifconfig
-bash: ifconfig: command not found

The problem is the `sbin` folder under which the `ifconfig` command resides is not added to the`PATH` environment variable by default.

all you have to do is the following.

$ export PATH=$PATH:/sbin
$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/raja/bin:/sbin

Now as we have added the `sbin` folder to the `PATH` variable, try the command again it will work.

You don’t want to set the path each time you login. You can add it to your `.bashrc` file in your home folder to update the path
automatically each time you login.

Here is how you do it.,

$ vi ~/.bashrc

add the following line at the end of the file.

export PATH=$PATH:/sbin

Save and close the file. From the next time you log-in the `sbin` folder will be available to your `PATH` variable.

In case you need to reload the .bashrc file immediately issue the following command

$ source ~/.bashrc

NOTE : It works only on a readonly mode. Trying to set the interface parameters as a non root user will generate error.

$  ifconfig eth0 192.168.0.1
SIOCSIFADDR: Permission denied
SIOCSIFFLAGS: Permission denied

Calendar

January 2009
M T W T F S S
« Dec   Feb »
 1234
567891011
12131415161718
19202122232425
262728293031  

Bookmarks

  • No bookmarks avaliable.

Friend Connect