Kill all processes of a user

To list all processes of myuser:


# list all processes of a user

ps -fu myuser

# grep pid

ps -fu myuser | awk 'NR !=1 {print $2}'

# kill them all

kill -9 $(ps -fu myuser | awk 'NR != 1 {print $2}')

Posted in BASH, Ubuntu | Leave a comment

Rescale ranges

Lets say I have values in a range from 0-1 and I want to rescale them to a range of 1 to 25. Generally speaking this can be resolved by: n.min + (x – o.min) * (n.max – n.min)/(o.max – o.min), where x is the value that is rescaled and n and o stand for new and old. For example to rescale the value 0.5 on our old scale ranging from 0 to 1 to our new scale we use:
1+(0.5-0)*(25-1)/(1-0) = 13

Posted in Uncategorized | Leave a comment

Read zipped file into R

Sometimes I do not want to unzip files before reading them to R. There is a nice way of reading zipped file (via a tmp dir) into R.

 myfile <- read.csv(zip.file.extract("~/files/test.csv", "myzip.zip"))
 

Where the file test.csv is actually located in the: ~/files/myzip.zip/test.csv.

Posted in R, Uncategorized | Tagged | Leave a comment

Allow access from remote host to mysql server

I have a mysql server and wanted to grant access to some remote hosts. The server runs on RedHat Enterprise Linux.

Fir I had to make some changes to the mysql config file:

su
# edit mysql config file
vi /etc/my.cnf
# add the following line
bind-address=SERVER-IP
# make sure there is line that tells mysql which port to use, something like
port=3306
# and that the following line is uncommented, although I did not have this line at all in my my.cnf
# skip-networking

Restart mysql

/etc/init.d/mysqld restart 

Log on to your database an allow access from remote hosts

mysql -u root -p DB -h localhost
GRANT ALL ON MY_DB.* TO 'MY_USER'@'SERVER_IP' IDENTIFIED BY 'MY_PASSWORD';
exit;

Finally check whether or not port 3306 is actually open. If its closed open it with:

su
vi /etc/sysconfig/iptables
# add the following line
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# save and quit and restart iptables
service iptables restart

Links I found useful:

Posted in mysql | Leave a comment

Delete table from PostGIS

A table and all its dependencies in PostGIS can be deleted with:

SELECT DropGeometryTable('myTable');
Posted in Uncategorized | Tagged , | Leave a comment

Reclass ascii grid with awk

I had to reclass a large number of grids (20000+) on a server without any GIS installed. So my choice was awk.


BEGIN {FS=" ";}
 {
 if (NR < 7) print $0;
 else{
 for(i=1;i<=NF;i++)
 {
 if ($i == "-9999") res=-9999;
 else if ($i < a) res=0;
 else res=1;
 printf "%s ", res;
 } printf "\n";
 }
}

The script requires as a parameter a a threshold. Every value below this threshold is classified as 0 and every value above as 1. No data values (-9999) are not affected. Once the script is saved to reclass.awk, it can be calle with:


awk -v a=0.4 -f reclass.awk ingrid.asc > outgrid.asc

Posted in awk, BASH, Computer | Leave a comment

Administrative areas of South America

I needed all first level administrative areas forĀ  South America. That was my way of getting them:

# get admin 1 for south america
mkdir gadm_sa
cd gadm_sa

# download the data
for country in COL ECU BRA BOL ARG PER GUY GUF VEN SUR PRY URY CHL
do
wget http://www.gadm.org/data/shp/${country}_adm.zip
unzip ${country}_adm.zip
done

# merge them to one shapefile
ogr2ogr sa_adm1.shp COL_adm1.shp

for country in ECU BRA BOL ARG PER GUY GUF VEN SUR PRY URY CHL
do
ogr2ogr -update -append sa_adm1.shp ${country}_adm1.shp -nln sa_adm1
done
Posted in BASH, GRASS | Leave a comment