our
$VERSION
=
'1.16.0'
;
@EXPORT
=
qw(kill killall
ps
nice)
;
sub
kill
{
my
(
$process
,
$sig
) =
@_
;
$sig
||=
""
;
i_run(
"kill $sig "
.
$process
,
fail_ok
=> 1 );
if
( $? != 0 ) {
die
(
"Error killing $process"
);
}
}
sub
killall {
my
(
$process
,
$sig
) =
@_
;
$sig
||=
""
;
if
( can_run(
"killall"
) ) {
i_run(
"killall $sig $process"
,
fail_ok
=> 1 );
if
( $? != 0 ) {
die
(
"Error killing $process"
);
}
}
else
{
die
(
"Can't execute killall."
);
}
}
sub
ps {
my
(
@custom
) =
@_
;
my
@list
;
if
(is_openwrt) {
@list
= i_run(
"ps"
,
fail_ok
=> 1 );
my
@ret
= ();
for
my
$line
(
@list
) {
$line
=~ s/^\s*|\s*$//g;
my
(
$pid
,
$user
,
$vsz
,
$stat
,
$command
) =
split
( /\s+/,
$line
, 5 );
push
(
@ret
,
{
user
=>
$user
,
pid
=>
$pid
,
vsz
=>
$vsz
,
stat
=>
$stat
,
command
=>
$command
,
}
);
}
return
@ret
;
}
elsif
( operating_system_is(
"SunOS"
) && operating_system_version() <= 510 ) {
if
(
@custom
) {
@list
=
i_run(
"/usr/ucb/ps awwx -o"
.
join
(
","
,
@custom
),
fail_ok
=> 1 );
}
else
{
@list
= i_run(
"/usr/ucb/ps auwwx"
,
fail_ok
=> 1 );
}
}
else
{
if
(
@custom
) {
@list
= i_run(
"ps awwx -o"
.
join
(
","
,
@custom
),
fail_ok
=> 1 );
}
else
{
@list
= i_run(
"ps auwwx"
,
fail_ok
=> 1 );
}
}
if
( $? != 0 ) {
if
(
@custom
) {
die
(
"Error running ps ax -o"
.
join
(
","
,
@custom
) );
}
else
{
die
(
"Error running ps aux"
);
}
}
shift
@list
;
my
@ret
= ();
if
(
@custom
) {
for
my
$line
(
@list
) {
$line
=~ s/^\s+//;
my
@col_vals
=
split
( /\s+/,
$line
,
scalar
(
@custom
) );
my
%vals
;
@vals
{
@custom
} =
@col_vals
;
push
@ret
, {
%vals
};
}
}
else
{
for
my
$line
(
@list
) {
my
(
$user
,
$pid
,
$cpu
,
$mem
,
$vsz
,
$rss
,
$tty
,
$stat
,
$start
,
$time
,
$command
) =
split
( /\s+/,
$line
, 11 );
push
(
@ret
,
{
user
=>
$user
,
pid
=>
$pid
,
cpu
=>
$cpu
,
mem
=>
$mem
,
vsz
=>
$vsz
,
rss
=>
$rss
,
tty
=>
$tty
,
stat
=>
$stat
,
start
=>
$start
,
time
=>
$time
,
command
=>
$command
,
}
);
}
}
return
@ret
;
}
sub
nice {
my
(
$pid
,
$level
) =
@_
;
i_run
"renice $level $pid"
,
fail_ok
=> 1;
if
( $? != 0 ) {
die
(
"Error renicing $pid"
);
}
}
1;