Monday, January 31, 2011

Changing Your Shell

Changing Your Shell
The easiest way to change your shell is to use the chsh command. Running chsh will place you into the editor that is in your EDITOR environment variable; if it is not set, you will be placed in vi. Change the “Shell:” line accordingly.

You can also give chsh the -s option; this will set your shell for you, without requiring you to enter an editor. For example, if you wanted to change your shell to bash, the following should do the trick:

% chsh -s /usr/local/bin/bash
Note: The shell that you wish to use must be present in the /etc/shells file. If you have installed a shell from the ports collection, then this should have been done for you already. If you installed the shell by hand, you must do this.

For example, if you installed bash by hand and placed it into /usr/local/bin, you would want to:

# echo "/usr/local/bin/bash" >> /etc/shells
Then rerun chsh.

http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/shells.html

Process Forking with PHP background

Before you can use the PHP process control functions you must compile
the PCNTL extensions into PHP using the --enable-pcntl
configure option (ie ./configure --enable-pcntl along with
all the other configuration options you would like to compile into the PHP
binary). No additional libraries need to be pre-installed. Note that these
process control extensions will not work on non-Unix platforms (ie
Microsoft Windows).


Basic Forking Example


A very basic and commonly used example for forking a process in PHP is
as follows:


$pid = pcntl_fork();

if($pid)
{
  // parent process runs what is here
  print
"parent\n";
}
else {
  // child
process runs what is here
  print
"child\n";
}


Running this will output the following:


child
parent


Child process is a copy of the parent process


What actually happens when you call the pcntl_fork()
function is that a child process is spawned which is exactly the same as
the parent process and continues processing from the line below the
function call. All variables and objects etc are copied into the child
process as-is but these are new copies which belong to the new process.
Modifying them in the child process does not affect the values in the
parent (or any other forked) process.


The parent process will have a value assigned to $pid
whereas the child process will not, hence the if test. Note
that in the above example, both processes would continue running whatever
code is after the if statement, something which is rarely mentioned in
examples of PHP process forking on the web.


To illustrate this, we'll modify the example above slightly to add
additional output as follows:


$pid = pcntl_fork();

print
"start\n";

if($pid) {
  // parent
process runs what is here
  print
"parent\n";
}
else {
  // child
process runs what is here
  print
"child\n";
}

print
"end\n";


Running this will display the following:


start
child
end
start
parent
end


Making the parent process wait until the child has
finished


So ideally you want to let either the child or parent continue
processing the rest of the script and make the other process exit after
the process is forked. If you exit from the parent process, however,
you'll end up with "zombie" processes running which do
not belong to any process. Therefore the parent process needs to wait
until all the child processes have finished running before exiting itself.
You can do this using the pcntl_waitpid() function, which
will cause the parent process to wait until the child process has
completed. You can then either just let the parent process exit, or do any
tidy up code that is required.


An example of doing this is as follows:


$pid = pcntl_fork();

if($pid)
{
  // this is the parent process
  // wait until
the child has finished processing then end the script
 
pcntl_waitpid($pid, $status, WUNTRACED);
  exit;
}

// the child process runs its stuff here and then ends

...



The exit call in the parent process ensures that
processing stops at that point and the parent does not execute any of the
code intended for the child. Another way of doing the same thing without
the exit code would be as follows:


$pid = pcntl_fork();

if($pid)
{
  // this is the parent process
  // wait until
the child has finished processing then end the script
 
pcntl_waitpid($pid, $status, WUNTRACED);
}
else {
 
// the child process runs its stuff here
}



Exit codes from the child process


You could optionally have an exit call at the end of the
child part of the if statement.


The $status parameter passed to
pcntl_waitpid() stores the return value from the child
process. If the child process returns 0 (ie success) then it will also be
zero. On my Linux desktop the value returned as $status would
be the value returned from the exit call multipled by 256. So if the child
process ended with exit(2) my system returned 512 as the
$status value. Whether this is the same across all Unix
systems I do not know.


Getting the parent process to wait until the child process has
completed is useful for then doing something else based on the return
value of the child process as shown in the following example:


$pid = pcntl_fork();

if($pid)
{
  // this is the parent process
  // wait until
the child has finished processing then end the script
 
pcntl_waitpid($pid, $status, WUNTRACED);
  if($status >
0) {
    // an error occurred so do some processing to
deal with it
  }
}
else {
  // the child
process runs its stuff here
  ...
  if(...successful
condition...) {
    exit(0); // this indicates success
  }
  else {
    exit(1); // this
indicates failure
  }
}



Forking multiple child processes


This final example illustrates how you could fork several children from
the parent process with PHP. The loop runs three times and forks a child
process for each loop, storing the child pids into an array. After running
the stuff for the child process the child then exits. A second loop runs
in the parent after the first to ensure all child processes have finished
running before resuming its own process.


Note it is very important in this sort of process that the child
explicitly exits in its section of the script, otherwise each child will
continue running through the first, and then second, loop.


$pids = array();

for($i = 0; $i
< 3; $i++) {

  $pids[$i] = pcntl_fork();

  if(!$pids[$i]) {
    // child process

    ...
    exit();
  }

}

for($i = 0; $i < 3; $i++) {
 
pcntl_waitpid($pids[$i], $status, WUNTRACED);
}

//
complete parent processing now all children have finished
...


The PHP manual pages for process control functions can be found at www.php.net/manual/en/ref.pcntl.php. There are a number of
user contributed notes for each of the functions which should also help
with your understanding of process forking in PHP.


http://www.electrictoolbox.com/article/php/process-forking/

Saturday, January 29, 2011

To check if a file contains UTF-8 BOM at header

To check if a file contains UTF-8 BOM at header:

# hexdump -n 3 -C 2.txt
00000000 ef bb bf

ef bb bf // YES

====================

ISO8859-1 is almost identical to -15 where -15 replaces one encoding
with the Euro symbol and includes a few more french symbols. The only
way to tell them apart would be to look at the symbols in context.

UTF-8 is identical to ISO8859 for the first 128 ASCII characters which
include all the standard keyboard characters. After that, characters
are encoded as a multi-byte sequence.

Unicode is usually encoded in UTF-16. If you're lucky, there might be
a BOM (Byte Order Mark) of 0xFFFE or 0xFEFF as the first two characters
in the file. Otherwise, look for a 0x00 (Null character) as every
other character if the text file contains basic 7-bit ASCII characters.

http://www.xpheads.com/forums/microsoft-public-windowsxp-help_and_support/164700-how-detect-if-text-file-iso8859-1-iso8859-15-utf-8-unicode-encoded.html

Friday, January 21, 2011

Taskbar Shuffle - rearrange switch tasks taskbar in windows XP

Taskbar Shuffle - rearrange switch tasks taskbar in windows XP

Taskbar Shuffle is a simple, small, free utility that lets you drag and drop your Windows taskbar buttons to rearrange them. Here’s a full feature list:

Full 32-bit and 64-bit support
Reorder your taskbar buttons by dragging and dropping them
Reorder your tray icons in the same way
Reorder tasks in a grouped button's popup menu in the same way
Middle-click to close programs on your taskbar
Works with UltraMon (version 3+ only) taskbars
Tweak taskbar button grouping

If you run more than a few applications at a time, you'll definitely find Taskbar Shuffle useful, so give it a try!

http://nerdcave.webs.com/

Wednesday, January 19, 2011

How to Change Timezone in FreeBSD

How to Change Timezone in FreeBSD

By default, FreeBSD is configured with UTC timezone. In order to localize your timezone, follow the steps below.

1. cd /usr/share/zoneinfo

2. cp America/Vancouver /etc/localtime
Now the local time zone has been configure, lets get the updated time and date from ntp server.

1. ntpdate time.nist.gov

http://feedproxy.google.com/~r/takizo/~3/G6ebm5L1aus/

Thursday, January 13, 2011

could not listen on UDP socket: permission denied

could not listen on UDP socket: permission denied
creating IPv4 interface re0 failed interface ignored

[] mac_portacl_load="YES"
http://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/mac-portacl.html

[] FreeBSD Bind DNS chroot jail

Wednesday, January 12, 2011

ifconfig says that the status of re0 is "no carrier"

ifconfig says that the status of re0 is "no carrier"

re0: flags=8802 metric 0 mtu 1500
 options=389b
 ether 00:16:36:7f:6e:b6
 media: Ethernet autoselect (10baseT/UTP )
 status: no carrier

[] We have to use that special parameters with ifconfig because our switch does not support autoselect.

[] You can add those mediaopt options to ifconfig_re0 in rc.conf:

# iv /etc/rc.conf
ifconfig_re0="inet 192.168.1.5 netmask 255.255.255.0 media 1000baseTX mediaopt full-duplex"

[] Other than that, 'no carrier' is usually 'no cable' or a dead/downed switchport.

[] Which could happen if you get lots of duplex and speed mismatches, a switch might decide to block off that port. On proper equipment I'd set both on both sides, never auto negotiate. Most unmanaged switches are a bit tricky though. And realtek isn't the best either. I know my realtek cards and home el-cheapo sweex switch will throw a fit if I don't use auto/auto.

[] restart freebsd networking service:

# /etc/rc.d/netif restart && /etc/rc.d/routing restart

# /etc/rc.d/netif forcerestart && /etc/rc.d/routing forcerestart

[] pciconf -lv
re0@pci0:3:0:0: class=0x020000 card=0x2aa9103c chip=0x813610ec rev=0x05 hdr=0x00
    vendor     = 'Realtek Semiconductor'
    device     = 'Realtek 10/100/1000 PCI-E NIC Family all in one NDIS Driver v5.728.0604.2009 06/04/2009 (Rtl8023)'
    class      = network
    subclass   = ethernet

http://gala4th.blogspot.com/2011/01/freebsd-set-speed-duplex-for-my-network.html