避免相同的程序重复运行
避免相同的程序重复运行的小函数
逻辑如下:
- 检查pid文件是否存在,不存在则直接运行程序
- 如果pid文件存在,则检查进程中是否存在这个pid
- 进程存在此pid则退出
- 进程中不存在这一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
}
FreeBSD重启网卡
FreeBSD重启网卡
# 本地重启:
ifconfig le0 down #stop网卡
ifconfig le0 up #start网卡
# 远程重启:
/etc/netstart
sh /etc/rc
/etc/rc.d/netif restart
# 重新获取dhcp地址:
/sbin/dhclient interface
Shell使用printf进行进制转换
Shell使用printf进行进制转换
localhost:~ 4aiur$ printf "%d\n" \0x64
100
localhost:~ 4aiur$ printf "%x\n" 100
64
网络上的一些XEN资料
网络上的一些XEN资料
awstats分析windowsmedia日志格式的字段配置
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"
ls long listing format各个字段的含义
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.
使用python计算md5
使用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)
awk应用举例
打印磁盘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?