A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.
Jump to
Showing posts with label process. Show all posts
Showing posts with label process. Show all posts
example gdb command
Use gdb to get a backtrace by attaching to a running process, then detaching immediately like so:
sudo gdb /path/to/apache/bin/httpd 12345 -ex bt -ex detach -ex quit
Where 12345 is a process ID that is misbehaving.
This command should be used with a bit of care as it will pause that process while the backtrace is obtained.
Code works on one environment but not another
What can differ between environments? Check the following:
* Your code (obviously)
* Versions of other dependent packages - both in-house and third-party
* Versions of other installed in-house modules
* Versions of other Perl CPAN modules
* Processes which didn't die when you restarted the app
* Data in the database
* Number of rows in tables, i.e. is some limit being hit?
* Schema of the database
* Browser cache
* Server side web cache
* Files on disk, e.g. cached print documents
(source: a decade's experience building software)
* Your code (obviously)
* Versions of other dependent packages - both in-house and third-party
* Versions of other installed in-house modules
* Versions of other Perl CPAN modules
* Processes which didn't die when you restarted the app
* Data in the database
* Number of rows in tables, i.e. is some limit being hit?
* Schema of the database
* Browser cache
* Server side web cache
* Files on disk, e.g. cached print documents
(source: a decade's experience building software)
Find and kill slow postgres queries
SELECT query_start,datname,procpid,current_query FROM pg_stat_activity;
SELECT NOW();
SELECT pg_cancel_backend($PID);
-- where $PID is the number from the procpid column in the first query
Labels:
databases,
kill,
performance,
postgresql,
process,
query,
slow
Run an external command from Perl, capturing both the output and the exit value
# This is how to run an external command from Perl,
# capturing both the output and the exit value
sub run_external_command {
my ($self, $command) = @_;
$self->logger->debug("Running command: $command");
my $output = '';
$command .= ' 2>&1 | '; # capture all output
open(my $capture, $command);
{
local $/ = undef;
$output = <$capture>;
}
close($capture);
chomp($output);
my $exit_value = ${^CHILD_ERROR_NATIVE} >> 8;
$self->logger->debug("Exit value of command: '$exit_value'");
$self->logger->debug("Output of command: '".($output || '[no output]')."'");
return ($exit_value, $output);
}
This is another way:
use IPC::Open3;
sub run_external_command_2 {
local (*my_in, *my_out, *my_err);
my $command = "some command here that might error";
my $pid = open3(\*my_in, \*my_out, \*my_err, $command) or die($!);
close(my_in);
my $std_out = do { local $/;
my $std_err = do { local $/;
waitpid($pid, 0);
my $exit_code = $? >> 8;
return { exit_code => $exit_code, std_out => $std_out, std_err => $std_err };
}
How to manage problematic application windows in Ubuntu
In Ubuntu:
To find out what application a window belongs to: Type xwininfo in the console, then click on the window.
To kill a window, type xkill in the console, then click on a window to kill it.
To find out what application a window belongs to: Type xwininfo in the console, then click on the window.
To kill a window, type xkill in the console, then click on a window to kill it.
Catch runaway processes on Mac
#!/bin/sh
# kill runaway processes at 100% CPU usage (Mac OSX)
export PNAME=$1
if [[ ! $PNAME ]]
then
echo "usage: runaways.sh [process name]"
exit
fi
export PPERCENT='100.0'
SECONDS_TO_TEST=20
THRESHOLD=15
SECONDS_RUNAWAY=$(top -l $SECONDS_TO_TEST | perl -lne'BEGIN{$c=0};/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && do { $c++ };END{print$c}');
if [[ $SECONDS_RUNAWAY -gt $THRESHOLD ]]
then
echo "killing $PNAME at $PPERCENT"%...
PID=$(top -o cpu -l 2 | perl -lne'/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && print $1')
if [[ $PID ]]
then
fi
else
echo "$PNAME is not runaway"
fi
# kill runaway processes at 100% CPU usage (Mac OSX)
export PNAME=$1
if [[ ! $PNAME ]]
then
echo "usage: runaways.sh [process name]"
exit
fi
export PPERCENT='100.0'
SECONDS_TO_TEST=20
THRESHOLD=15
SECONDS_RUNAWAY=$(top -l $SECONDS_TO_TEST | perl -lne'BEGIN{$c=0};/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && do { $c++ };END{print$c}');
if [[ $SECONDS_RUNAWAY -gt $THRESHOLD ]]
then
echo "killing $PNAME at $PPERCENT"%...
PID=$(top -o cpu -l 2 | perl -lne'/$ENV{PNAME}\s+$ENV{PPERCENT}\%/ && print $1')
if [[ $PID ]]
then
echo "Killing $PNAME"
kill -9 $PID
else
echo "$PNAME does not exceed the threshold for a runaway"fi
else
echo "$PNAME is not runaway"
fi
Simple Perl parallelisation
@waitlist = (10,20,16,12,8,22);
foreach $item (@waitlist) {
pipe *{$item},RETHAND;
unless ($pid = fork()) {
# Child process
sleep $item;
print RETHAND "Completed $item\n";
exit();
}
}
foreach $item (@waitlist) {
$response = <$item>;
$now = localtime();
print "$now - $response";
}
Replace sleep with a more useful command.
debugging technique
When programming C, it can be difficult to track down a segmentation fault.
It is often caused by attempting to read uninitialised memory.
Try skipping parts of the program until you locate the statement causing it.
Then replace each part of the data by hard-coded values, one by one, until you find the uninitialised variable/pointer.
Temporarily suspend a process
At times, you may find it necessary to temporarily suspend a process, and then resume its execution at a later time. The following two commands will suspend a process, and the resume it, respectively:
# kill -STOP 945
# kill -CONT 945
# kill -STOP 945
# kill -CONT 945
Subscribe to:
Posts (Atom)