Sponsored Content
Top Forums Shell Programming and Scripting Perl script to kill the process ID of a command after a certain period Post 302375633 by Pouchie1 on Saturday 28th of November 2009 02:53:05 PM
Old 11-28-2009
Thanks all for the feedback.

Scrutinizer,

Your post really scrutinize the problems and give me all kind of possible solutions. It's really appreciated. I cannot ask for more.

The only thing is that it looks like there is no simple way we can have perl do the same thing. is that a fair statement?
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

to kill a process in perl

Hi friends, I have a perl script which needs to kill all java processes running on both windows and unix. currently I'm getting the OS and killing the process by using system. my code is: if ($os eq MSWin32) system("taskkill java"); else system("kill -9 java") Is there any way... (2 Replies)
Discussion started by: gurukottur
2 Replies

2. Shell Programming and Scripting

Perl : Kill process within 5 min

From a perl script , How can I monitor a PS which I activated and kill it within 5 minutes in case it didn't complete its tasks.:confused: (2 Replies)
Discussion started by: Alalush
2 Replies

3. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

4. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

5. Shell Programming and Scripting

Script to kill process...

hello Bros, I need to write some script that i can put it on crontab which checks for a process X if running. If the process X is ruuning then take the PID and kill it or display message that says process X is not running. I am using AIX 5.3 Thanks guys.:b: (2 Replies)
Discussion started by: malcomex999
2 Replies

6. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

7. Shell Programming and Scripting

Script to Kill a Process by Name...

Hello all... new to these forums and a bit of a newbie with linux aswell. I need to figure out how to write a shell script to kill a process by name as given to the script as an argument. I've got that part working OK, but i need to make sure that the script does not allow processes that are... (6 Replies)
Discussion started by: cannon1707
6 Replies

8. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

9. Shell Programming and Scripting

perl check and kill process

Hi All How can i in perl check for a running process and if it is running kill it the issue might be that there will be more than one of the same process and i want to kill all of them the process is below root 1944 1 0 16:28 ? 00:00:01 x11vnc -display :0... (3 Replies)
Discussion started by: ab52
3 Replies

10. Shell Programming and Scripting

Command to know all the Current running process and how to kill

All, 1.What is the unix comand used for all current running process (Including All current running processes Parent ->child->subchild process) 2.If child and subchild processes are running then what is the unix command to kill parent and its all child subchild processes in UNIX. Kindly... (7 Replies)
Discussion started by: skp
7 Replies
HTML::Element::traverse(3pm)				User Contributed Perl Documentation			      HTML::Element::traverse(3pm)

NAME
HTML::Element::traverse - discussion of HTML::Element's traverse method VERSION
This document describes version 5.02 of HTML::Element::traverse, released June 27, 2012 as part of HTML-Tree. SYNOPSIS
# $element->traverse is unnecessary and obscure. # Don't use it in new code. DESCRIPTION
"HTML::Element" provides a method "traverse" that traverses the tree and calls user-specified callbacks for each node, in pre- or post- order. However, use of the method is quite superfluous: if you want to recursively visit every node in the tree, it's almost always simpler to write a subroutine does just that, than it is to bundle up the pre- and/or post-order code in callbacks for the "traverse" method. EXAMPLES
Suppose you want to traverse at/under a node $tree and give elements an 'id' attribute unless they already have one. You can use the "traverse" method: { my $counter = 'x0000'; $start_node->traverse( [ # Callbacks; # pre-order callback: sub { my $x = $_[0]; $x->attr('id', $counter++) unless defined $x->attr('id'); return HTML::Element::OK; # keep traversing }, # post-order callback: undef ], 1, # don't call the callbacks for text nodes ); } or you can just be simple and clear (and not have to understand the calling format for "traverse") by writing a sub that traverses the tree by just calling itself: { my $counter = 'x0000'; sub give_id { my $x = $_[0]; $x->attr('id', $counter++) unless defined $x->attr('id'); foreach my $c ($x->content_list) { give_id($c) if ref $c; # ignore text nodes } }; give_id($start_node); } See, isn't that nice and clear? But, if you really need to know: THE TRAVERSE METHOD
The "traverse()" method is a general object-method for traversing a tree or subtree and calling user-specified callbacks. It accepts the following syntaxes: $h->traverse(&callback) or $h->traverse(&callback, $ignore_text) or $h->traverse( [&pre_callback,&post_callback] , $ignore_text) These all mean to traverse the element and all of its children. That is, this method starts at node $h, "pre-order visits" $h, traverses its children, and then will "post-order visit" $h. "Visiting" means that the callback routine is called, with these arguments: $_[0] : the node (element or text segment), $_[1] : a startflag, and $_[2] : the depth If the $ignore_text parameter is given and true, then the pre-order call will not be happen for text content. The startflag is 1 when we enter a node (i.e., in pre-order calls) and 0 when we leave the node (in post-order calls). Note, however, that post-order calls don't happen for nodes that are text segments or are elements that are prototypically empty (like "br", "hr", etc.). If we visit text nodes (i.e., unless $ignore_text is given and true), then when text nodes are visited, we will also pass two extra arguments to the callback: $_[3] : the element that's the parent of this text node $_[4] : the index of this text node in its parent's content list Note that you can specify that the pre-order routine can be a different routine from the post-order one: $h->traverse( [&pre_callback,&post_callback], ...); You can also specify that no post-order calls are to be made, by providing a false value as the post-order routine: $h->traverse([ &pre_callback,0 ], ...); And similarly for suppressing pre-order callbacks: $h->traverse([ 0,&post_callback ], ...); Note that these two syntaxes specify the same operation: $h->traverse([&foo,&foo], ...); $h->traverse( &foo , ...); The return values from calls to your pre- or post-order routines are significant, and are used to control recursion into the tree. These are the values you can return, listed in descending order of my estimation of their usefulness: HTML::Element::OK, 1, or any other true value ...to keep on traversing. Note that "HTML::Element::OK" et al are constants. So if you're running under "use strict" (as I hope you are), and you say: "return HTML::Element::PRUEN" the compiler will flag this as an error (an unallowable bareword, specifically), whereas if you spell PRUNE correctly, the compiler will not complain. undef, 0, '0', '', or HTML::Element::PRUNE ...to block traversing under the current element's content. (This is ignored if received from a post-order callback, since by then the recursion has already happened.) If this is returned by a pre-order callback, no post-order callback for the current node will happen. (Recall that if your callback exits with just "return;", it is returning undef -- at least in scalar context, and "traverse" always calls your callbacks in scalar context.) HTML::Element::ABORT ...to abort the whole traversal immediately. This is often useful when you're looking for just the first node in the tree that meets some criterion of yours. HTML::Element::PRUNE_UP ...to abort continued traversal into this node and its parent node. No post-order callback for the current or parent node will happen. HTML::Element::PRUNE_SOFTLY Like PRUNE, except that the post-order call for the current node is not blocked. Almost every task to do with extracting information from a tree can be expressed in terms of traverse operations (usually in only one pass, and usually paying attention to only pre-order, or to only post-order), or operations based on traversing. (In fact, many of the other methods in this class are basically calls to traverse() with particular arguments.) The source code for HTML::Element and HTML::TreeBuilder contain several examples of the use of the "traverse" method to gather information about the content of trees and subtrees. (Note: you should not change the structure of a tree while you are traversing it.) [End of documentation for the "traverse()" method] Traversing with Recursive Anonymous Routines Now, if you've been reading Structure and Interpretation of Computer Programs too much, maybe you even want a recursive lambda. Go ahead: { my $counter = 'x0000'; my $give_id; $give_id = sub { my $x = $_[0]; $x->attr('id', $counter++) unless defined $x->attr('id'); foreach my $c ($x->content_list) { $give_id->($c) if ref $c; # ignore text nodes } }; $give_id->($start_node); undef $give_id; } It's a bit nutty, and it's still more concise than a call to the "traverse" method! It is left as an exercise to the reader to figure out how to do the same thing without using a $give_id symbol at all. It is also left as an exercise to the reader to figure out why I undefine $give_id, above; and why I could achieved the same effect with any of: $give_id = 'I like pie!'; # or... $give_id = []; # or even; $give_id = sub { print "Mmmm pie! " }; But not: $give_id = sub { print "I'm $give_id and I like pie! " }; # nor... $give_id = $give_id; # nor... $give_id = { 'pie' => $give_id, 'mode' => 'a la' }; Doing Recursive Things Iteratively Note that you may at times see an iterative implementation of pre-order traversal, like so: { my @to_do = ($tree); # start-node while(@to_do) { my $this = shift @to_do; # "Visit" the node: $this->attr('id', $counter++) unless defined $this->attr('id'); unshift @to_do, grep ref $_, $this->content_list; # Put children on the stack -- they'll be visited next } } This can under certain circumstances be more efficient than just a normal recursive routine, but at the cost of being rather obscure. It gains efficiency by avoiding the overhead of function-calling, but since there are several method dispatches however you do it (to "attr" and "content_list"), the overhead for a simple function call is insignificant. Pruning and Whatnot The "traverse" method does have the fairly neat features of the "ABORT", "PRUNE_UP" and "PRUNE_SOFTLY" signals. None of these can be implemented totally straightforwardly with recursive routines, but it is quite possible. "ABORT"-like behavior can be implemented either with using non-local returning with "eval"/"die": my $died_on; # if you need to know where... sub thing { ... visits $_[0]... ... maybe set $died_on to $_[0] and die "ABORT_TRAV" ... ... else call thing($child) for each child... ...any post-order visiting $_[0]... } eval { thing($node) }; if($@) { if($@ =~ m<^ABORT_TRAV>) { ...it died (aborted) on $died_on... } else { die $@; # some REAL error happened } } or you can just do it with flags: my($abort_flag, $died_on); sub thing { ... visits $_[0]... ... maybe set $abort_flag = 1; $died_on = $_[0]; return; foreach my $c ($_[0]->content_list) { thing($c); return if $abort_flag; } ...any post-order visiting $_[0]... return; } $abort_flag = $died_on = undef; thing($node); ...if defined $abort_flag, it died on $died_on SEE ALSO
HTML::Element AUTHOR
Current maintainers: o Christopher J. Madsen "<perl AT cjmweb.net>" o Jeff Fearn "<jfearn AT cpan.org>" Original HTML-Tree author: o Gisle Aas Former maintainers: o Sean M. Burke o Andy Lester o Pete Krawczyk "<petek AT cpan.org>" You can follow or contribute to HTML-Tree's development at http://github.com/madsen/HTML-Tree <http://github.com/madsen/HTML-Tree>. COPYRIGHT
Copyright 2000,2001 Sean M. Burke perl v5.14.2 2012-06-30 HTML::Element::traverse(3pm)
All times are GMT -4. The time now is 08:33 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy