Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

awk

cron dashboard
Friday, 28 October 2011 05:58
// log cron entries to central server


#!/bin/sh

host='localhost'
username='root'
password='root@123'
port='3306'

myhost=`hostname`
myip=`/sbin/ifconfig | grep -A2 'eth0 ' | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`

for eachfile in `find /var/spool/cron/ -type f`
do
myfile=`basename $eachfile`

grep -v '^#' $eachfile | grep -v '^MAILTO' | sed '/^$/d' | while read -r myline
do
first=`echo "$myline" | awk '{print $1}'`
second=`echo "$myline" | awk '{print $2}'`
third=`echo "$myline" | awk '{print $3}'`
forth=`echo "$myline" | awk '{print $4}'`
fifth=`echo "$myline" | awk '{print $5}'`
sixth=`echo "$myline" | cut -d' ' --complement -f1-5 | sed 's/\(['"'"'\]\)/\\&/g'`
seventh=`echo "$myline" | awk -F'>|>>' '{print $2}' | sed 's/2$//'`
eighth=`echo "$myline" | awk -F'2>|2>>' '{print $2}' `

mysql -h$host -u$username -p$password -P$port -e" replace into test.mycron values ('$myhost', '$myip', '$myfile', '$first', '$second', '$third', '$forth', '$fifth', '$sixth', '$seventh', '$eighth', now())"

done
done

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/1DPv99fggn4/13821

 
Search for running processes
Tuesday, 05 October 2010 13:44
Search for all running processes using a regular expression pattern and return PIDs for all processes matching it ...

Quick version ... there is probably a better way to do this ...


module Process
class << self
def search(pattern)
result = Dir['/proc/[0-9]*/cmdline'].inject({}) do |h, file|
if (process = File.read(file).split(/\000|\s+/).first)
process = File.basename(process).gsub(/\W/, '')
(h[process] ||= []).push(file.match(/\d+/)[0].to_i)
end
h
end.map { |k, v| v if k.match(pattern) }.compact.flatten
result if result.any?
end
end
end


Simple use-case:


irb(main):184:0> `ps x -o cmd | awk '{ print $1 }' | grep -i bash | wc -l`
=> "8\n"
irb(main):185:0> Process.search(/bash/).size
=> 8
irb(main):186:0> `ps x -o cmd | awk '{ print $1 }' | grep -i sh | wc -l`
=> "17\n"
irb(main):187:0> Process.search(/sh/).size
=> 17
irb(main):188:0>

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/edTiEXl7sUE/12355

 


Taxonomy by Zaragoza Online