避免相同的程序重复运行

Posted by 4Aiur on 04/06/2010 in Shell with Comments closed |

避免相同的程序重复运行的小函数

逻辑如下:

  1. 检查pid文件是否存在,不存在则直接运行程序
  2. 如果pid文件存在,则检查进程中是否存在这个pid
    1. 进程存在此pid则退出
    2. 进程中不存在这一pid,则删除pid文件,程序继续运行

缺点:没有对进程名称进行对比

# Initialize variables
app_path="$(dirname $0)"
pid="${app_path}/run.pid"
# Function
TestPID () {
  if [ -f ${pid} ] ; then
     if ps -p cat ${pid} >/dev/null ; then
        echo "basename $0 is running!  main process id = $(cat ${pid})"
        ps -p cat ${pid} -o cmd=
        exit 1
     else
        rm -f $pid
        echo $$ > ${pid}
     fi
  else
     echo $$ > ${pid}
  fi
}

Tags:

FreeBSD重启网卡

Posted by 4Aiur on 04/06/2010 in BSD with Comments closed |

FreeBSD重启网卡

# 本地重启:
ifconfig le0 down  #stop网卡
ifconfig le0 up  #start网卡

# 远程重启:
/etc/netstart
sh /etc/rc
/etc/rc.d/netif restart

# 重新获取dhcp地址:
/sbin/dhclient interface

Tags:

Shell使用printf进行进制转换

Posted by 4Aiur on 04/06/2010 in Shell with Comments closed |

Shell使用printf进行进制转换

localhost:~ 4aiur$ printf "%d\n" \0x64
100
localhost:~ 4aiur$ printf "%x\n" 100
64

Tags:

网络上的一些XEN资料

Posted by 4Aiur on 04/06/2010 in SysAdmin with Comments closed |

网络上的一些XEN资料

北南南北-Xen 初学者指南

鳥哥的 Linux 私房菜- 利用 Xen 設計虛擬機器

xen安装及配置笔记

RedHat 5.0配置XEN虚拟机(上)

RedHat 5.0配置XEN虚拟机(下)

Tags: ,

awstats分析windowsmedia日志格式的字段配置

Posted by 4Aiur on 04/06/2010 in SysAdmin with Comments closed |

awstats分析windowsmedia日志格式的字段配置

LogFormat="c-ip date time c-dns cs-uri-stem c-starttime x-duration c-rate c-status c-playerid c-playerversion c-playerlanguage cs(User-Agent) cs(Referer) c-hostexe c-hostexever c-os c-osversion c-cpu filelength filesize avgbandwidth protocol transport audiocodec videocodec channelURL sc-bytes c-bytes s-pkts-sent c-pkts-received c-pkts-lost-client c-pkts-lost-net c-pkts-lost-cont-net c-resendreqs c-pkts-recovered-ECC c-pkts-recovered-resent c-buffercount c-totalbuffertime c-quality s-ip s-dns s-totalclients s-cpu-util cs-user-name s-session-id s-content-path cs-url cs-media-name c-max-bandwidth cs-media-role s-proxied"

Tags:

工程师的5个级别

Posted by 4Aiur on 04/06/2010 in SysAdmin with Comments closed |

工程师的5个级别

  • 不知问题是五流工程师
  • 看到问题是四流工程师
  • 找出问题是三流工程师
  • 解决问题是二流工程师
  • 一流工程师会提前发现问题,阻止问题发生

ls long listing format各个字段的含义

Posted by 4Aiur on 04/06/2010 in Linux with Comments closed |

ls long listing format各个字段的含义

ls -l
total 8
-rw-r--r-- 1 root root    2 Jul 27 13:30 bar
-rw-rw-r-- 1 root root 7056 Jul 27 13:27 foo

For each directory that is listed, preface the files with a line
‘total BLOCKS’, where BLOCKS is the total disk allocation for all
files in that directory. The block size currently defaults to 1024
bytes, but this can be overridden (*note Block size::). The
BLOCKS computed counts each hard link separately; this is arguably
a deficiency.

From the output above we can deduct a following information:

  • -rw-rw-r- permissions
  • 1 : number of linked hard-links
  • root: owner of the file
  • root: to which group this file belongs to
  • 7056: file size
  • Jul 27 13:27 modification date and time
  • foo: file/directory name

To answer your question we will look more closely at the permissions part of ls long listing format output:

-rw-r--r--

The file type is one of the following characters:

  • – regular file
  • b block special file
  • c character special file
  • C high performance ("contiguous data") file
  • d directory
  • D door (Solaris 2.5 and up)
  • l symbolic link
  • M off-line ("migrated") file (Cray DMF)
  • n network special file (HP-UX)
  • p FIFO (named pipe)
  • P port (Solaris 10 and up)
  • s socket
  • ? some other file type

The file mode bits listed are similar to symbolic mode specifications (*note Symbolic Modes::).

But `ls’ combines multiple bits into the third character of each set of permissions as follows:

  • s If the set-user-ID or set-group-ID bit and the corresponding executable bit are both set.
  • S If the set-user-ID or set-group-ID bit is set but the corresponding executable bit is not set.
  • t If the restricted deletion flag or sticky bit, and the other-executable bit, are both set.The restricted deletion flag is another name for the sticky bit. *Note Mode Structure::.
  • T If the restricted deletion flag or sticky bit is set but the other-executable bit is not set.
  • x If the executable bit is set and none of the above apply.
  • – Otherwise.

Tags:

使用python计算md5

Posted by 4Aiur on 04/06/2010 in Python with Comments closed |

使用python计算md5

#!/usr/bin/env python3
import os
import hashlib

# Use hashlib module
file = open("foo", "rb")
content = file.read()
file.close()
result = hashlib.md5()
result.update(content)
md5sum = result.hexdigest()
print(md5sum)

# Use OS command
result = os.popen("md5sum foo").readline()
md5sum = result.split()[0]
print(md5sum)

Tags:

awk应用举例

Posted by 4Aiur on 04/06/2010 in Shell with Comments closed |

打印磁盘INODE最大值

# df -Pi | awk 'BEGIN{OFS="\n";Max=0} {if (NR!=1) {sub(/%/,"",$5); Max=$5>Max?$5:Max}} END{print Max}'

38

打印磁盘空间最大值

df -P | awk 'BEGIN{OFS="\n";Max=0} {if (NR!=1) {sub(/%/,"",$5); Max=$5>Max?$5:Max}} END{print "diskUsedSpacePercent: "Max,"diskSpaceUpdateTime: "systime()}'

diskUsedSpacePercent: 55
diskSpaceUpdateTime: 1252898042

按netstat中ESTABLISHED状态的连接数量进行排序

netstat -an 2>/dev/null | awk /ESTABLISHED/'{print $5}' | awk -F: '{!ip[$(NF-1)]++} END { for (item in ip) print item,ip[item] | "sort -k2 -rn"}' | more

打印系统连接数

echo -e "\\033[1;32m"awk /Tcp/'{print $10}' /proc/net/snmp "\\033[0;39m"

CurrEstab 31

The Split function

#!/usr/bin/awk -f
BEGIN {
# this script breaks up the sentence into words, using 
# a space as the character separating the words
    string="This is a string, is it not?";
    search=" ";
    n=split(string,array,search);
    for (i=1;i

Word[1]=This
Word[2]=is
Word[3]=a
Word[4]=string,
Word[5]=is
Word[6]=it
Word[7]=not?

Tags: ,

Linux学习循序渐进

Posted by 4Aiur on 04/06/2010 in Linux with Comments closed |

Linux学习循序渐进

Tags:

Copyright © 2010-2024 4Aiur All rights reserved.
This site is using the Desk Mess Mirrored theme, v2.5, from BuyNowShop.com.