避免相同的程序重复运行

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

避免相同的程序重复运行的小函数 逻辑如下: 检查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 […]

Tags:

Shell使用printf进行进制转换

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

Shell使用printf进行进制转换 localhost:~ 4aiur$ printf “%d\n” \0x64 100 localhost:~ 4aiur$ printf “%x\n” 100 64

Tags:

awk应用举例

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

打印磁盘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 […]

Tags: ,

shell递归一例

Posted by 4Aiur on 03/31/2010 in Shell |

用递归写的一个光标旋转的小脚本 [root@maint-app-108 recurse]# cat cursor.sh cursor () { #{{{ message=”$1″ count=”$2″ for item in “|” “/” “-” “\\” do printf “\r${item} ${message} $count” usleep 50000 done cursor waiting $((count+1)) } cursor waiting 1 [root@maint-app-108 cursor]# 用递归做这种无限循环最后脚本会退出,下面这个while :不会退出。 [root@maint-app-108 cursor]# cat cursor.sh cursor () { #{{{ message=”$1″ for item in “|” “/” “-” “\\” do printf […]

Tags:

数字的格式化打印

Posted by 4Aiur on 03/31/2010 in SysAdmin |

数字的格式化打印 有人问我一个产生"01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20"这样一种字符串的方法,当时我随意的写了一个 for ((x=1;x 我当时觉得有点土,之后人家问了如果打印的结果是从0到100怎么办呢? 因为最近买了本python的书,在复习pyhon的基础知识,恰好在看格式胡print的时候看到print%0是用0替换空格来占位,当时看书的时候感到有点怪,不知道数字前面加0有什么用,所以还有点印象。 所以像下面的python语句那么写好看多了。 Python 2.6.2 (r262:71600, Jul 2 2009, 16:30:16) [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> for x in range(20): […]

Tags:

排除Apache access log乱序日志

Posted by 4Aiur on 03/31/2010 in Shell |

排除Apache access log乱序日志 由于Apache的访问日志时间记录的是访问开始时间,所以会有时间不是顺序排列的情况产生。 由于有一个特殊需求,需要把乱序的日志排除掉,今天写了个小脚本处理了一下。 转换Apache accesslog时间为时间戳,进行处理 把乱序日志打印到了badlog文件中 [root@4Aiur ~]# cat foo 58.59.23.18 – – [26/Nov/2008:11:04:05 +0800] “GET /test.html HTTP/1.1” 200 8228 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)” 58.59.23.18 – – [26/Nov/2008:11:03:05 +0800] “GET /test.html HTTP/1.1” 200 8228 “-” “Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; […]

Tags: ,

带特殊字符文件的删除方法

Posted by 4Aiur on 03/31/2010 in Linux |

带特殊字符文件的删除方法 list文件列表的时候发现有个"?"文件,直接删除?是删不掉的。 ~]# ll total 75040 -rw-r–r– 1 root root 0 Nov 7 14:46 ? # 使用cat -A 查看这个文件是带有特殊字符的文件 ~]# ll | cat -A total 75040$ -rw-r–r– 1 root root 0 Nov 7 14:46 M-

Tags: ,

自动修改crontab配置

Posted by 4Aiur on 03/31/2010 in Shell |

自动修改crontab配置 方法1:使用crontab -l把crontab内容导出到文件中,使用编辑器或脚本修改导出的文件,之后使用新的配置文件覆盖掉现有的配置。 [4Aur@4Aiur ~]$ crontab -l > cron[4Aur@4Aiur ~]$ sed -i ‘s/ls/dir/’ cron [4Aur@4Aiur ~]$ crontab cron 方法2:使用here文档的方式更新crontab的配置。 [4Aur@4Aiur ~]$ crontab -l0 * * * * ls [4Aur@4Aiur ~]$ sh cron_modify.sh [4Aur@4Aiur ~]$ crontab -l 0 * * * * dir [4Aur@4Aiur ~]$ cat cron_modify.sh crontab -e /dev/null :%s/ls/dir/g :wq EOF [4Aur@4Aiur ~]$

Tags: ,

被用于保留(reserved meanings)的退出状态码

Posted by 4Aiur on 03/31/2010 in Shell |

  Exit Codes With Special Meanings Reserved Exit Codes Exit Code Number Meaning Example Comments 1 Catchall for general errors let “var1 = 1/0” Miscellaneous errors, such as “divide by zero” 2 Misuse of shell builtins (according to Bash documentation) ? Seldom seen, usually defaults to exit code 1 126 Command invoked cannot execute ? […]

Tags:

使用shell进行大小写转换

Posted by 4Aiur on 03/31/2010 in Shell |

使用shell进行大小写转换 大写转小写 for f in *; do rename $f echo $f | tr "[:upper:]" "[:lower:]" $f; done 小写转大写 for f in *; do rename $f echo $f | tr "[:lower:]" "[:upper:]" $f; done

Tags:

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