Gutmans_ch16 Page 518 Thursday, September 23, 2004 3:01 PM in .NET Generation 3 of 9 in .NET Gutmans_ch16 Page 518 Thursday, September 23, 2004 3:01 PM visual .net Code 128 Code Set A

Gutmans_ch16 Page 518 Thursday, September 23, 2004 3:01 PM generate, create none none in none projectscode 128 generation PHP Shell Scripting code 39 Chap. 16 Following is an example none for none that forks and execs an ls command:. < php $child_pid = pc ntl_fork(); if ($child_pid == 0) { // replace php with "ls" command in child pcntl_exec("/bin/ls", array("-la")); } elseif ($child_pid != -1) { // wait for the "ls" process to exit pcntl_waitpid($child_pid, $status, 0); } >. First, a child process i none none s created. Then, in the process where $child_pid was returned as 0 (the child process), the ls command is executed. The output from ls will go to standard output.

The parent process waits for the child to exit before it continues. Here is another example. PHP detaches itself from the terminal and continues running in the background (a technique known as daemonizing):.

< php $pid = pcntl_fo rk(); if ($pid) { exit(0); } // create new session, detach from shell s process group posix_setsid(); // XXX if STD{IN,OUT,ERR} constants become available, these have // to be closed here. while (true) { error_log("heartbeat\n", 3, "/tmp/test.log"); sleep(10); } >.

First, this script forks none for none and creates a second PHP process. The parent process then exits, and the child continues. Then, the child disconnects from the controlling terminal and creates its own session and process group with posix_setsid().

This makes sure that signals sent to the shell are not passed along to the child PHP process.. Gutmans_ch16 Page 519 Thursday, September 23, 2004 3:01 PM 16.2 PHP CLI Shell Scripts 16.2.5.

4 Signals In UNIX , signals are a basic mechanism to pass messages between processes. They enable processes to tell each other that some type of event has just occurred. This type of event is the only information passed to basic UNIX signal handlers.

There is another signal-handling mechanism called sigaction in which signal handlers receive more information, but PHP signals are based on the former, basic form. For example, if the user presses Ctrl-c to stop a command-line program, the program receives an interrupt signal, called SIGINT. In PHP, you can set up a function to handle one or more signals with the pcntl_signal() function, like this:.

< php function sigint none for none _handler($signal) { print "Interrupt!\n"; exit; } pcntl_signal(SIGINT, "sigint_handler"); declare (ticks = 1) { while (sleep(1)); } >. This script sleeps until you terminate it. If you do press Ctrl-c, it prints and exits. You could change this example to ignore Ctrl-c completely by changing the signal-handler function to the prede ned SIG_IGN:.

Interrupt! pcntl_signal( none for none SIGINT, SIG_IGN);. You may change a signal handler anytime, including inside a signalhandling function. To revert to the default signal handler, use SIG_DFL:. pcntl_signal(SIGINT, SIG none none _DFL);. PHP probably supports al l the signals your system supports. Try typing in your shell to see some. Table 16.

3 lists of signals that may be useful from PHP, either catching and handling them, or sending them to (killing) other processes.. kill l Gutmans_ch16 Page 520 Thursday, September 23, 2004 3:01 PM PHP Shell Scripting Chap. 16 Table 16.3 Signal SIGHUP SIGINT SIGABRT SIGKILL SIGUSR1 SIGSEGV Description Hangup. Used none for none to notify when terminal connection is lost. Interrupt.

Send when user hits the interrupt (Ctrl-c) key. Sent by the abort() C function; used by assert(). Non-graceful termination of the process; cannot be caught.

User-de ned signal 1. Segmentation fault; in some operating systems, it s known as General Protection Failure. User-de ned signal 2.

Sent when a pipe the process is reading closes unexpectedly. Sent when an alarm times out. Terminate process normally.

A child process just died or changed status. Continue after stopping with SIGSTOP. Halt process; cannot be caught.

Halt process; may be caught. Process stopped due to tty input. Process stopped due to tty output.

CPU time limit exceeded. File size limit exceeded. Passed when a baby is ready to change diapers, hungry, about to climb something dangerous or doing anything else that requires immediate attention from a parent PHP programmer.

. SIGUSR2 SIGPIPE SIGALRM SIGTERM SIGCHLD SIGCONT SIGSTOP SIGTSTP SIGTTIN SIGTTOU SIGCXPU SIGXFSZ SIGBABY. 16.2.6 Examples Here are none for none some examples of command-line tools written in PHP.


1 PHP Filter Utility This example includes a little tool for ltering line by line from standard input through a PHP function that returns a string:. #!/usr/bin/env php < php if (empty($argv[1])) { die("Usage: phpfilter <function>\n"); } $function = $argv[1];.
Copyright © . All rights reserved.