顯示具有 gentoo 標籤的文章。 顯示所有文章
顯示具有 gentoo 標籤的文章。 顯示所有文章

2012年6月13日 星期三

mon process via pid file



#!/usr/bin/python3.2
import os

self_pid_path = '/var/run/monPid.pid'
pid_path_list = ['/var/run/mldonkey.pid', '/var/run/sshd.pid']

# olny a instance run

def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True

pid_file=None

if os.path.exists(self_pid_path):
with open(self_pid_path, 'r') as oldpid_file:
pid = oldpid_file.readline()
if pid.isdigit() and check_pid(int(pid)):
exit()
else:
pid_file = open(self_pid_path, 'w')
else:
pid_file = open(self_pid_path, 'a')

pid_file.write(str(os.getpid()))
pid_file.close()

# olny a instance run

# detect mldonkey alive by pid file

for pid_path in pid_path_list:
pid = 0
program = pid_path[pid_path.rfind('/')+1:-4]
if os.path.exists(pid_path):
with open(pid_path, 'r') as pid_file:
pid = pid_file.readline()[:-1]
if pid.isdigit() and check_pid(int(pid)):
print (program + ': alive')
continue
else:
pid = ''

if pid.isdigit() and check_pid(int(pid)):
print (program + '(' + pid + ')' + 'alive')
else:
print (program + '(' + pid + ')' + 'dead')
os.system('echo `date +"%a %b %d %T"` /etc/init.d/' + program + ' restart >> /var/log/monPidFile')



crontab -e
* * * * * /sbin/monPidFile

2012年5月31日 星期四

burn iso though cui

emerge -av dvd+rw-tools


growisofs -dvd-compat -Z /dev/dvdrw=/root/yourImage.ISO -speed=4

2012年4月23日 星期一

Some install record concerning mod_wsgi on gentoo

Target: Using Virtual Server (I called it prototype :) ).

Request modify the hosts file

/etc/apache2/modules.d/70_mod_wsgi.conf

# WSGISocketPrefix is require 'mkdir /usr/lib/apache2/run'
WSGISocketPrefix
#WSGIPythonOptimize enable output the pac file
WSGIPythonOptimize 1
WSGILazyInitialization Off
#WSGIImportScript path process-group=prototype application-group=prototype
#WSGIPythonHome
#WSGIPythonPath


/etc/apache2/vhosts.d/00_default_vhost.conf

<VirtualHost *:80>
ServerName prototype
ErrorLog /var/log/apache2/prototype_error_log
CustomLog /var/log/apache2/prototype_access_log common

Include /etc/apache2/vhosts.d/prototype.include

<IfModule mpm_peruser_module>
ServerEnvironment apache apache
</IfModule>
</VirtualHost>



/etc/apache2/vhosts.d/prototype.include

ServerAdmin masked@masked
DocumentRoot "/var/www/prototype/htdocs"
<Directory "/var/www/prototype/htdocs">
# Enable AllowOverride in order to use '.htaccess' to modify the access right on the fly
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# Using Alias since mount the DocumentRoot as root "...any static files contained in the DocumentRoot will be hidden and requests against URLs pertaining to the static files will instead be processed by the WSGI application. from:http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide"

Alias /favicon.ico /var/www/prototype/htdocs/favicon.ico
Alias /media/ /var/www/prototype/htdocs/media/

WSGIScriptAlias / /var/www/prototype/htdocs/wsgi_script/runme.py
WSGIDaemonProcess prototype:1 processes=2 threads=15 display-name=%{GROUP} user=prototype group=prototype umask=0022 maximum-requests= 1024 deadlock-timeout=5 shutdown-timeout=3 receive-buffer-size=87380 send-buffer-size=65536
#-WSGIDaemonProcess
WSGIProcessGroup prototype:1
WSGIRestrictProcess prototype:1


/var/www/prototype/htdocs/wsgi_script/runme.py

import sys

def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'

response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)

print >> sys.stderr, 'sys.prefix = %s' % repr(sys.prefix)
print >> sys.stderr, 'sys.path = %s' % repr(sys.path)

return [output]


All dir/.htaccess

AddDefaultCharset utf-8
# Disable directory index
Options -All

Run Home bashrc

all below lines attached the file /etc/bash/bashrc

#enable personally bashrc
if [[ ~/.bashrc ]] ; then
source ~/.bashrc
fi


Example

#!/bin/bash
alias ncat='nano -v'

Enable key pad in termal

/etc/profile.d/term.sh


#!/bin/bash
# This program change term to vt220 for enable num pad on terminal
# The side effect is the page up/down be unavailable in nano editor
# The default value is xterm
# list all available on /etc/terminfo
TERM=vt220


Refer:http://www.linuxquestions.org/questions/*bsd-17/problem-with-remote-terminal-ssh-and-putty-125363/

2012年4月18日 星期三

Save power through dynamic frequency

the program split in 2 part:
a program set something and call the script to manage the frequency

/etc/local.d/powerSave.start

#!/bin/bash

#Runtime Power Management
for i in `find /sys/devices/*/power/control`; do echo auto > $i; done;

#USB Autosuspend
for i in `find /sys/bus/usb/devices/*/power/level`; do echo auto > $i; done;
for i in `find /sys/bus/usb/devices/*/power/autosuspend`; do echo 2 > $i; done;

#Use script to mgmt the frequency
cpufreq-set -r -g userspace
echo `date +"%a %b %d %T"` dynamicFrequency start >> /var/log/dynamicFrequency.log
/sbin/dynamicFrequency.py



/etc/local.d/powerSave.stop

#!/bin/bash
kill $(cat /var/run/dynamicFrequency.pid)
cpufreq-set -r -g performance
echo `date +"%a %b %d %T"` dynamicFrequency Stop >> /var/log/dynamicFrequency.log


/sbin/dynamicFrequency.py

#!/usr/bin/python3.2
"""\
This program select a scaling available frequencies based on loadavg
for example the available frequencies:
350000 700000 1050000 1400000 1750000 2100000 2450000 2800000
the current frequencies is 175000
if load >= 80% the frequencies jump 2 step (245000)
if load >= 60% the frequencies jump 1 step (210000)
if load < 20% the frequencies jump -2 step (1050000)
if load < 40% the frequencies jump -1 step (1400000)


"""

import os

# Write the pid into
pid_path = '/var/run/dynamicFrequency.pid'

pidFile=None
if os.path.exists(pid_path):
pidFile = open(pid_path, 'w')
else:
pidFile = open(pid_path, 'a')

pidFile.write(str(os.getpid()))
pidFile.close()

scaling_available_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies', 'r') as file:
scaling_available_frequencies = file.read().strip().split()

current_frequencies = None
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq', 'r') as file:
raw_current_frequencies = file.read().strip()
if raw_current_frequencies in scaling_available_frequencies:
current_frequencies = scaling_available_frequencies.index(raw_current_frequencies)
else:
current_frequencies = len(scaling_available_frequencies)

from decimal import *
from time import sleep

max = len(scaling_available_frequencies) - 1

while True:
loadavg = None
with open('/proc/loadavg', 'r') as file:
loadavg = Decimal(file.read().strip()[0])

change = 0
if loadavg >= Decimal(0.80):
change = 2
elif loadavg >= Decimal(0.60):
change = 1
elif loadavg < Decimal(0.20):
change = -2
elif loadavg < Decimal(0.40):
change = -1

plan=None
if current_frequencies + change >= max:
plan = max
elif current_frequencies + change < 0:
plan = 0
else:
plan = current_frequencies + change

if plan != current_frequencies:
current_frequencies = plan
with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed', 'w') as file:
file.write(scaling_available_frequencies[current_frequencies])
os.system('echo `date +"%a %b %d %T"` dynamicFrequency' + ' loadavg:' + str(loadavg) + ' change frequency to:' + \
scaling_available_frequencies[plan] + ' >> /var/log/dynamicFrequency.log')

sleep(5)

Reference:

2012年4月13日 星期五

Gentoo Network Configuration

/etc/conf.d/net
# This blank configuration will automatically use DHCP for any net.*
# scripts in /etc/init.d. To create a more complete configuration,
# please review /usr/share/doc/openrc*/net.example* and save your configuration
# in /etc/conf.d/net (this file :]!).
modules="ifconfig"
dns_servers_eth0="192.168.1.254"
config_eth0="192.168.1.234 netmask 255.255.255.0"
routes_eth0="default via 192.168.1.254"


/etc/sysctl.conf

#append this row
kernel.domainname = local


/etc/conf.d/hostname

# Set to the hostname of this machine
hostname="gentoo"

Cannot mount boot partition (ext2)

Solution:
#tune2fs -O has_journal /dev/sda1

lilo: One warning was issued

#/sbin/lilo -v
LILO version 22.8, Copyright (C) 1992-1998 Werner Almesberger
Development beyond version 21 Copyright (C) 1999-2006 John Coffman
Released 19-Feb-2007 and compiled at 09:05:22 on Apr 11 2012

Warning: LBA32 addressing assumed
Reading boot sector from /dev/sda
Using MENU secondary loader
Calling map_insert_data

Boot image: /boot/linux-3.2.1-gentoo-r2
Added gentoo

Boot image: /boot/linux-3.2.2-hardened-r1
Added hardened *
Writing boot sector.
/boot/boot.0800 exists - no boot sector backup copy made.
One warning was issued.

Try
#sbin/lilo -L -S /boot/boot.0800