Press "Enter" to skip to content

月与灯依旧 Posts

Linux shell之seq用法

$ seq 1000   #起始默认是 1,间隔默认也是1
$ seq 2 1000  #间隔默认是1
$ seq 1 3 10    #从1到10,间隔为3,结果是:1 4 7 10
#!/bin/bash
for i in `seq 1 10`
do
  echo eth$i does not have a 1000 card!!
done

Linux循环遍历文件写法:

for file in `ls /etc/sysconfig/network-scripts/ifcfg-*`
do
  echo $file
done

以下是本人写的某脚本:

#!/bin/bash
for file in `ls /etc/sysconfig/network-scripts/ifcfg-*`
do
  i=${file##*-}
  #echo now checking $i
  if [ `ifconfig $i | grep 10000base | wc -l` -ge 1 ]; then
    id = `cat /proc/interrupts | grep $i | grep -v $i- | awk '{print $1}'`
    echo 'ff' > /proc/irq/${id%:*}/smp_affinity
    echo $i optimized.
  fi
done

参考文章:linux shell 动态生成 数组系列 seq使用技巧

Leave a Comment

笔记本建立WIFI热点手机搜索不到的解决办法

人人都知道笔记本可以建立临时的WIFI热点用于网络分享,但是建立的WIFI热点,有时候只能被笔记本搜索到,手机、平板等设备却搜索不到。本文介绍解决方法。

1,开启虚拟网卡,输入命令:

netsh wlan set hostednetwork mode=allow ssid=热点名称 key=热点密码

其中,mode参数表示是否启动虚拟网卡,如果要关闭之,可以输入netsh wlan set hostednetwork mode=disallow

2,必要条件检查。如果没有出现以下两项,请重启笔记本。
打开“设备管理器”,展开“网络适配器”,会出现一个名为“Microsoft Virtual WiFi Miniport Adapter”的设备。
打开“网络连接”,会出现一个“无线网络连接 2”,该连接使用的正是“Microsoft Virtual WiFi Miniport Adapter”。

3,将有线连接开启共享。右击“本地连接”,选择“属性”,点击“共享”选项卡,勾选 “允许其它网络用户通过此计算机的 Internet 连接来连接”,同时在下面选中“无线网络连接 2”(即上面Microsoft Virtual WiFi Miniport Adapter设备对应的连接)。

4,启动虚拟网络(承载网络),输入命令:

netsh wlan start hostednetwork

电脑重启以后,需要重新启动虚拟网络,即执行本文的步骤4。
本文是一篇简要介绍,如果想看更详细的说明,可以参考百度经验的这篇文章

1 Comment

python遍历文件脚本实例

自己写的一个Python遍历文件脚本,对查到的文件进行特定的处理。没啥技术含量,但是也记录一下吧。

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import shutil
dir = "/mnt/Packages"
class Packages:
    def __init__(self,srcdir,desdir):
        self.sdir=srcdir
        self.ddir=desdir
    def check(self):
        print('program start...')
        for dirpath, dirnames, filenames in os.walk(self.sdir):   #遍历文件
            for filename in filenames:
                thefile=os.path.join(dirpath,filename)            #文件的绝对地址
                try:
                    if os.path.splitext(thefile)[1]=='.rpm':      #筛选.rpm格式的文件
                        #print('Fount rpm package: ' + thefile)
                        if 'inspuer' in os.popen('rpm -qpi ' + thefile).read().rstrip():
                            print('Found error package: ' + thefile)
                            shutil.copy(thefile, self.ddir)  #将错误文件复制到desdir目录
                            f = open('list.txt', 'a')    #将错误文件列表写入到list.txt
                            f.write(filename + '\n')
                            f.close()
                except IOError, err:
                    print err
                    sys.exit()

if __name__ == '__main__':
    dir=Packages('/mnt/cdrom','/mnt/erpm')   #源目录为/mnt/cdrom,目标目录为/mnt/erpm
    dir.check()
Leave a Comment

Linux系统查看PCI/USB设备信息

Linux系统查看PCI设备信息,基本是使用lspci命令,如果找不到该命令,那么需要先安装pciutils软件包。

列出系统的PCI设备:

[root@os2 ~]# lspci
00:00.0 Host bridge: Intel Corporation Xeon E5/Core i7 DMI2 (rev 07)
00:01.0 PCI bridge: Intel Corporation Xeon E5/Core i7 IIO PCI Express Root Port 1a (rev 07)
00:03.0 PCI bridge: Intel Corporation Xeon E5/Core i7 IIO PCI Express Root Port 3a in PCI Express Mode (rev 07)
00:05.0 System peripheral: Intel Corporation Xeon E5/Core i7 Address Map, VTd_Misc, System Management (rev 07)
00:05.2 System peripheral: Intel Corporation Xeon E5/Core i7 Control Status and Global Errors (rev 07)
00:05.4 PIC: Intel Corporation Xeon E5/Core i7 I/O APIC (rev 07)
00:11.0 PCI bridge: Intel Corporation C600/X79 series chipset PCI Express Virtual Root Port (rev 06)
00:16.0 Communication controller: Intel Corporation C600/X79 series chipset MEI Controller #1 (rev 05)
00:16.1 Communication controller: Intel Corporation C600/X79 series chipset MEI Controller #2 (rev 05)
00:1a.0 USB controller: Intel Corporation C600/X79 series chipset USB2 Enhanced Host Controller #2 (rev 06)
00:1c.0 PCI bridge: Intel Corporation C600/X79 series chipset PCI Express Root Port 1 (rev b6)
00:1c.7 PCI bridge: Intel Corporation C600/X79 series chipset PCI Express Root Port 8 (rev b6)
00:1d.0 USB controller: Intel Corporation C600/X79 series chipset USB2 Enhanced Host Controller #1 (rev 06)
00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev a6)
00:1f.0 ISA bridge: Intel Corporation C600/X79 series chipset LPC Controller (rev 06)
00:1f.2 IDE interface: Intel Corporation C600/X79 series chipset 4-Port SATA IDE Controller (rev 06)
00:1f.3 SMBus: Intel Corporation C600/X79 series chipset SMBus Host Controller (rev 06)
00:1f.5 IDE interface: Intel Corporation C600/X79 series chipset 2-Port SATA IDE Controller (rev 06)
05:00.0 RAID bus controller: Adaptec Series 6 - 6G SAS/PCIe 2 (rev 01)
06:00.0 Serial Attached SCSI controller: Intel Corporation C602 chipset 4-Port SATA Storage Control Unit (rev 06)
06:00.3 SMBus: Intel Corporation C600/X79 series chipset SMBus Controller 0 (rev 06)
07:00.0 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
09:00.0 PCI bridge: ASPEED Technology, Inc. AST1150 PCI-to-PCI Bridge (rev 02)
0a:00.0 VGA compatible controller: ASPEED Technology, Inc. ASPEED Graphics Family (rev 21)
7f:08.0 System peripheral: Intel Corporation Xeon E5/Core i7 QPI Link 0 (rev 07)
7f:08.3 System peripheral: Intel Corporation Xeon E5/Core i7 QPI Link Reut 0 (rev 07)

注意:如果有显示unknown device,说明/usr/share/hwdata/pci.ids文件太旧,没有及时更新。

Leave a Comment

Apache的三种工作模式( Prefork、Worker和Event)

一,Apache三种MPM介绍

Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM)。在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个不同的MPM可供选择,它们会影响到apache的速度和可伸缩性。这三种MPM记录于conf/extra/httpd-mpm.conf文件中。

Prefork MPM:这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它的工作方式类似于Apache 1.3。它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。

这个MPM具有很强的自我调节能力,只需要很少的配置指令调整。最重要的是将MaxClients设置为一个足够大的数值以处理潜在的请求高峰,同时又不能太大,以致需要使用的内存超出物理内存的大小。

Worker MPM:此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求,所以可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是,它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。

每个进程可以拥有的线程数量是固定的。服务器会根据负载情况增加或减少进程数量。一个单独的控制进程(父进程)负责子进程的建立。每个子进程可以建立ThreadsPerChild数量的服务线程和一个监听线程,该监听线程监听接入请求并将其传递给服务线程处理和应答。

不管是Worker模式或是Prefork 模式,Apache总是试图保持一些备用的(spare)或者是空闲的子进程(空闲的服务线程池)用于迎接即将到来的请求。这样客户端就不需要在得到服务前等候子进程的产生。

Event MPM:以上两种稳定的MPM方式在非常繁忙的服务器应用下都有些不足。尽管HTTP的Keepalive方式能减少TCP连接数量和网络负载,但是 Keepalive需要和服务进程或者线程绑定,这就导致一个繁忙的服务器会耗光所有的线程。 Event MPM是解决这个问题的一种新模型,它把服务进程从连接中分离出来。在服务器处理速度很快,同时具有非常高的点击率时,可用的线程数量就是关键的资源限 制,此时Event MPM方式是最有效的。一个以Worker MPM方式工作的繁忙服务器能够承受每秒好几万次的访问量(例如在大型新闻服务站点的高峰时),而Event MPM可以用来处理更高负载。值得注意的是,Event MPM不能在安全HTTP(HTTPS)访问下工作

Leave a Comment

定制Linux发行版(2):定制自己的软件包格式

redhat系统的软件包名字一般为.el6.rpm,假设我们要打造一款名为K-UX的操作系统,那我们可以使用mock,编译出.kux.rpm形式的软件包。在阅读本文之前,你可能需要知道rpmbuild和mock的原理及相关使用方法,这些本博客以前都介绍过。本文的实验环境为CentOS 6.4 x86_64。

为什么要使用mock?

mock是Linux平台下编译.src.rpm的工具,它是在rpmbuild上封装了一层,与rpmbuild相比,它的好处是可以自动解决安装过程中产生的各种依赖关系。而rpmbuild不能自动解决这些依赖关系。既然依赖关系解决了,那就可以写个简单的脚本,实现批量编译.src.rpm的任务了。

按理说,如果要大量编译.src.rpm包,应该使用Fedora提供的Koji工具,但因为Koji太过庞大,配置起来极其复杂,因此还是使用更为小巧智能的mock比较方便。

一,使用rpmbuild编译.kux.rpm格式的软件包

首先需要知道一点儿系统基础:
%dist一般表示操作系统平台,如el5、el6,我们要修改的.kux也需要修改此值
%arch一般表示硬件平台,i386、i686、ia64、x86_64等
要build出.kux.rpm格式的软件包,首先需要修改/etc/rpm/macros.dist文件

[root@os2 ~]# rpm -qf /etc/rpm/macros.dist 
centos-release-6-4.el6.centos.10.x86_64  #由该软件包释放

[root@os2 ~]# cat /etc/rpm/macros.dist 
# dist macros.

%rhel 6
%centos 6
%centos_ver 6
%dist .kux    #将这里的el6修改为kux
%el6 1

然后,再通过rpmbuild命令就可以编译出.kux.rpm格式的软件包了

Leave a Comment

Linux Server出现大量TIME_WAIT连接

可以先看看如下配置项的状态:

cat /proc/sys/net/ipv4/tcp_syncookies
cat /proc/sys/net/ipv4/tcp_timestamps
cat /proc/sys/net/ipv4/tcp_tw_reuse
cat /proc/sys/net/ipv4/tcp_tw_recycle
cat /proc/sys/net/ipv4/tcp_fin_timeout

编辑/etc/sysctl.conf文件:

net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 20
net.ipv4.tcp_keepalive_time = 1800

开启TCP同步标签(syncookie),防止一个套接字在有过多试图连接到达时引起过载
开启net.ipv4.tcp_tw_reuse可以将处于TIME-WAIT状态的socket(TIME-WAIT的端口)用于新的TCP连接
TIME_WAIT快速回收通过net.ipv4.tcp_tw_recycle启用,由于其根据时间戳来判定,所以必须开启TCP时间戳(tcp_timestamps)才有效

使配置生效:

sysctl -p
Comments closed