Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS 

See your most used shell commands

Found here.

history | awk '{print $2}' | awk 'BEGIN {FS="|"}{print $1}' | sort | uniq -c | sort -n | tail | sort -nr

Set CPU affinity for a Ruby script in Linux

require 'rubygems'
require 'inline'

# Oodles of inspiration and examples from
# http://www-128.ibm.com/developerworks/linux/library/l-affinity.html

class LinuxScheduler
  inline do |builder|
    builder.include '<sched.h>'
    builder.include '<stdio.h>'
    builder.c '
      int _set_affinity(int cpu_id) {
        cpu_set_t mask;
        __CPU_ZERO(&mask);
        __CPU_SET(cpu_id, &mask);
        if(sched_setaffinity(getpid(), sizeof(mask), &mask ) == -1) {
          printf("WARNING: Could not set CPU Affinity, continuing as-is\n");
          return 0;
        }
        return 1;
      }
    '
  end

  # cpu_id is 0-based, so for core/cpu 1 = 0, etc..
  def self.set_affinity(cpu_id)
    self.new._set_affinity(cpu_id.to_i)
  end
end


# Set this process's CPU affinity
LinuxScheduler.set_affinity(ARGV.first)

# Hog up all the CPU time on that processor
1000000.times { b = rand(100) ** 100 }

vmstat output and column meaning in Linux

Here's some vmstat output I just got:

procs                      memory      swap          io     system         cpu
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 2  5 375912  19548  17556 477472    0    1     0     0    1     1  1  0  0  1
 0  4 375912  18700  17556 478264    0    0  1044     0  774  1329  8  1  0 91
 0  5 375912  17664  17556 479168    0    0  1160     0  764  1110  8  1  0 91
 1  8 375912  15836  17568 479796    0    0  1144   840  751  1622 16  7  0 78
 0  7 375912  19340  17576 480224    0    0  1224   148  587  1958 17 18  0 65
 2  0 375912  18288  17588 481036    0    0   812     0  845  1732 18  3 21 59
 0  2 375912  15868  17588 481528    0    0  1012     0  588   941  4  1  5 90 


r - processes waiting to run but that are held up by the CPU
b - processes sleeping (usually waiting for IO)
swpd - total swap used (in KB)
free - total free memory
buff - total buffer memory
cache - total disk cache memory usage
si - memory swapped in from disk (in KB/sec)
so - memory swapped out to disk (in KB/sec)
bi - blocks read in from IO devices (blocks per sec)
bo - blocks written to IO devices (blocks per sec)
in - interrupts per second
cs - context switches per second
us - userland CPU usage in %
sy - kernelspace CPU usage in %
id - CPU idle %
wa - CPU IO wait %

How to install MySQL 5.0 on RHEL 4

RedHat Enterprise Linux 4 comes with MySQL 4, so it needed to be upgraded. I did it the following way. I am not responsible for anything that happens if you follow these instructions, so please understand what they do!

Make sure to replace the wget RPM downloads with the correct RPMs for your platform! These ones are for AMD/Intel 64 bit (non Itanium)..

service mysqld stop
cd ~
mkdir mysql
cd mysql
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/MySQL-server-standard-5.0.22-0.rhel4.x86_64.rpm/from/http://mirror.x10.com/mirror/mysql/
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/MySQL-client-standard-5.0.22-0.rhel4.x86_64.rpm/from/http://mirror.x10.com/mirror/mysql/
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/MySQL-shared-standard-5.0.22-0.rhel4.x86_64.rpm/from/http://mirror.x10.com/mirror/mysql/
wget http://dev.mysql.com/get/Downloads/MySQL-5.0/MySQL-devel-standard-5.0.22-0.rhel4.x86_64.rpm/from/http://mirror.x10.com/mirror/mysql/
rpm -Uvh --nodeps MySQL-server-standard-5.0.22-0.rhel4.x86_64.rpm
rpm -Uvh MySQL-client-standard-5.0.22-0.rhel4.x86_64.rpm
rpm -Uvh MySQL-shared-standard-5.0.22-0.rhel4.x86_64.rpm
rpm -Uvh MySQL-devel-standard-5.0.22-0.rhel4.x86_64.rpm
nano -w /etc/my.cnf
# Comment out the base-dir line under [mysqld.server] - RHEL 4 / MySQL bug
adduser mysql
chown -R mysql:mysql /var/run/mysqld
chown -R mysql:mysql /var/lib/mysql
service mysql start
mysql

Search and replace over file(s) with Perl

A quick bit of Perl can come in handy if you have an old site to update that has no CMS, or something similar.

To change 'source' to 'destination' in all HTML files in the current directory:

perl -pi -e 's/source/destination/g' *.html


You could use this to update copyright notices, etc.. but bear in mind you need to stay with Perl/regex syntax, so escape those forward slashes, etc :)

Find all large files on a Linux machine

Finds all files over 20,000KB (roughly 20MB) in size and presents their names and size in a human readable format:

find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }' 

Mail a file as an attachment from the UNIX prompt

uuencode file.txt file.txt | mail email@address.com

Very minimal security of remote file fetching on Linux

Basic stuff, but stops bad users being able to grab stuff they shouldn't.

chmod 750 /usr/bin/rcp 
chmod 750 /usr/bin/wget 
chmod 750 /usr/bin/lynx 
chmod 750 /usr/bin/links 
chmod 750 /usr/bin/scp

Get a list of big files on an Ensim shared hosting server

Go to /home/virtual and run:

find -type l -maxdepth 1 -name '*.*' | xargs -n 1 basename | xargs -i find /home/virtual/{}/var/www -type f -size +8192k -ls

Easy file encryption and decryption from the shell

Works on OS X, Linux, anywhere with OpenSSL installed:

To encrypt a file:
openssl des3 -salt -in infile.txt -out encryptedfile.txt


To decrypt the file:
openssl des3 -d -salt -in encryptedfile.txt -out normalfile.txt


Do not specify the same file as input and output on encryption.. I have noticed weird effects on OS X (it eats the file). Remove the -in * stuff if you want to pipe data into it (e.g. a tarred folder). Omit the -out * stuff if you want it to pipe data out on STDOUT.
« Newer Snippets
Older Snippets »
Showing 1-10 of 15 total  RSS