您的位置:首页 > 运维架构 > Linux

linux 常用命令汇集

2013-10-09 15:14 417 查看

1. ulimit

ulimint -a 用来显示当前的各种用户进程限制
Linux对于每个用户,系统限制其最大进程数,为提高性能,可以根据设备资源情况,
设置个Linux用户的最大进程数,一些需要设置为无限制:
数据段长度:ulimit -d unlimited
最大内存大小:ulimit -m unlimited
堆栈大小:ulimit -s unlimited

-a     All current limits are reported
-b     The maximum socket buffer size
-c     The maximum size of core files created
-d     The maximum size of a processâs data segment
-e     The maximum scheduling priority ("nice")
-f     The maximum size of files written by the shell and its children
-i     The maximum number of pending signals
-l     The maximum size that may be locked into memory
-m     The maximum resident set size (many systems do not honor this limit)
-n     The maximum number of open file descriptors (most systems do not allow this value to be set)
-p     The pipe size in 512-byte blocks (this may not be set)
-q     The maximum number of bytes in POSIX message queues
-r     The maximum real-time scheduling priority
-s     The maximum stack size
-t     The maximum amount of cpu time in seconds
-u     The maximum number of processes available to a single user
-v     The maximum amount of virtual memory available to the shell
-x     The maximum number of file locks
-T     The maximum number of threads


大部分时候使用它是为了产生core dump文件的,以供gdb调试用。

>ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 28577
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
sonora.peking.corp.yahoo.com: /home/zhishan/workspace/fileSystem_learning
>ulimit -c unlimited
sonora.peking.corp.yahoo.com: /home/zhishan/workspace/fileSystem_learning
>ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 28577
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 1024
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

简单实例

1]在Linux下写程序的时候,如果程序比较大,经常会遇到“段错误”(segmentation fault)这样的问题,这主要就是由于Linux系统初始的堆栈大小(stack size)太小的缘故,一般为10M。我一般把stack size设置成256M,这样就没有段错误了!命令为:

ulimit   -s 262140 

如果要系统自动记住这个配置,就编辑/etc/profile文件,在 “ulimit -S -c 0 > /dev/null 2>&1”行下,添加“ulimit   -s 262140”,保存重启系统就可以了!

在RH8的环境文件/etc/profile中,我们可以看到系统是如何配置ulimit的:

CODE:
#grep ulimit /etc/profile

ulimit -S -c 0 > /dev/null 2>&1
这条语句设置了对软件资源和对core文件大小的设置

2]如果我们想要对由shell创建的文件大小作些限制,如:

CODE:
#ll h

-rw-r--r-- 1 lee lee 150062 7月 22 02:39 h

#ulimit -f 100 #设置创建文件的最大块(一块=512字节)

#cat h>newh

File size limit exceeded

#ll newh

-rw-r--r-- 1 lee lee 51200 11月 8 11:47 newh
文件h的大小是150062字节,而我们设定的创建文件的大小是512字节x100块=51200字节

当然系统就会根据你的设置生成了51200字节的newh文件.

3]可以像实例1]一样,把你要设置的ulimit放在/etc/profile这个环境文件中.

在默认的情况下,很多系统的core文件是生成在你运行程序的目录下,或者你在程序中chdir后的那个目录,然后在core文件的后面加了一个 pid。在实际工作中,这样可能会造成很多目录下产生core文件,不便于管理,实际上,在2.6下,core文件的生成位置和文件名的命名都是可以配置 的。

 

/proc/sys/kernel/core_uses_pid可以控制产生的core文件的文件名中是否添加pid作为扩展,如果添加则文件内容为1,否则为0

proc/sys/kernel/core_pattern可以设置格式化的core文件保存位置或文件名,比如原来文件内容是core-%e

可以这样修改:

echo "/tmp/core-%e-%p" > core_pattern

将会控制所产生的core文件会存放到/corefile目录下,产生的文件名为core-命令名-pid-时间戳

以下是参数列表:

    %p - insert pid into filename 添加pid

    %u - insert current uid into filename 添加当前uid

    %g - insert current gid into filename 添加当前gid

    %s - insert signal that caused the coredump into the filename 添加导致产生core的信号

    %t - insert UNIX time that the coredump occurred into filename 添加core文件生成时的unix时间

    %h - insert hostname where the coredump happened into filename 添加主机名

    %e - insert coredumping executable name into filename 添加命令名

当然,你可以用下列方式来完成

sysctl -w kernel.core_pattern=/tmp/core-%e-%p

 

这些操作一旦计算机重启,则会丢失,如果你想持久化这些操作,可以在 /etc/sysctl.conf文件中增加:

kernel.core_pattern=/tmp/core%p

 

加好后,如果你想不重启看看效果的话,则用下面的命令:

sysctl -p /etc/sysctl.conf

------------------------------------------------------------------------------------------------------------------

高手指教:

    解决的问题:

         现有一程序P 长期在服务器上运行,目前经常是每1天死掉一次(段错误)。

    目前解决方法:

         用SecureCRT开一个终端,并在服务其上设置ulimit -c nulimited,然后启动程序P。用ulimite -a 命令查询结果如下:

         core file size       (blocks, -c) unlimited

         data seg size           (kbytes, -d) unlimited

         file size             (blocks, -f) unlimited

         pending signals                 (-i) 1024

         max locked memory    (kbytes, -l) 32

          ............

         表明core文件可以生成。

         并测试利用kill -6 pid能够core文件。

gdb core 多线程
在linux环境下调试多线程,总觉得不像.NET那么方便。这几天就为找一个死锁的bug折腾好久,介绍一下用过的方法吧。

多线程如果dump,多为段错误,一般都涉及内存非法读写。可以这样处理,使用下面的命令打开系统开关,让其可以在死掉的时候生成core文件。   
ulimit -c unlimited

这样的话死掉的时候就可以在当前目录看到core.pid(pid为进程号)的文件。接着使用gdb:
gdb ./bin ./core.pid 

进去后,使用bt查看死掉时栈的情况,在使用frame命令。

还有就是里面某个线程停住,也没死,这种情况一般就是死锁或者涉及消息接受的超时问题(听人说的,没有遇到过)。遇到这种情况,可以使用:
gcore pid (调试进程的pid号)

手动生成core文件,在使用pstack(linux下好像不好使)查看堆栈的情况。如果都看不出来,就仔细查看代码,看看是不是在 if,return,break,continue这种语句操作是忘记解锁,还有嵌套锁的问题,都需要分析清楚了。

在 /etc/security/limits 文件中设置缺省极限就是设置了系统宽度极限, 而不仅仅是创建用户时用户所需的极限。 

省略 Limit 参数时,将会打印出当前资源极限。除非用户指定 -H 标志,否则打印出软极限。当用户指定一个以上资源时,极限名称和单元在值之前打印。如果未给予选项,则假定带有了 -f 标志。 

由于 ulimit 命令影响当前 shell 环境,所以它将作为 shell 常规内置命令提供。如果在独立的命令执行环境中调用该命令,则不影响调用者环境的文件大小极限。以下示例中正是这种情况: 

nohup ulimit -f 10000

env ulimit 10000 

一旦通过进程减少了硬极限,若无 root 特权则无法增加,即使返回到原值也不可能。

最后,说一句,静心看代码,捶胸顿足是没有用的。

2. gcore

3. fdisk

对分区进行格式化,以及加载;

  先提示一下;用 mkfs.bfs mkfs.ext2 mkfs.jfs mkfs.msdos mkfs.vfatmkfs.cramfs mkfs.ext3 mkfs.minix mkfs.reiserfs mkfs.xfs 等命令来格式化分区,比如我想格式化 sda6为ext3文件系统,则输入;
[root@localhost beinan]# mkfs.ext3 /dev/sda6

  如果我想加载 sda6到目前系统来存取文件,应该有mount 命令,但首先您得建一个挂载目录;比如 /mnt/sda6 ; [root@localhost beinan]# mkdir /mnt/sda6

  [root@localhost beinan]# mount /dev/sda6 /mnt/sda6

  [root@localhost beinan]# df -lh

  

Filesystem

  FileSystem 描述 返回指定的驱动器使用的文件系统的类型。 语法 object.FileSystem object 应为 Drive 对象的名称。 说明 可用的返回类型包括 FAT、NTFS更多>>

容量 已用 可用 已用% 挂载点

  /dev/hda8 11G 8.4G 2.0G 81% /

  /dev/shm 236M 0 236M 0% /dev/shm

  /dev/hda10 16G 6.9G 8.3G 46% /mnt/hda10

  /dev/sda6 191M 5.6M 176M 4% /mnt/sda6

  这样我们就能进入 /mnt/sda6目录,然后存取文件了;具体的权限方法,以及mount 更详细的用法,在以后我会专门写一个帖子;在一帖中放下所有的内容实在有点为难;

FDISK进行硬盘分区 分区从实质上说就是对硬盘的一种格式化。当我们创建分区时,就已经设置好了硬盘的各项物理参数,指定了硬盘主引导记录(即MasterBootRecord,一般简称为MBR)和引导记录备份的存放位置。而对于文件系统以及其他操作系统管理硬盘所需要的信息则是通过之后的高级格式化,即Format命令来实现。用一个形象的比喻,分区就好比在一张白纸上画一个大方框。而格式化好比在方框里打上格子。安装各种软件就好比在格子里写上字。分区和格式化就相当于为安装软件打基础,实际上它们为电脑在硬盘上存储数据起到标记定位的作用。
http://www.baidu.com/s?ie=utf-8&bs=linux%E6%9F%A5%E7%9C%8B%E7%A3%81%E7%9B%98%E7%A9%BA%E9%97%B4&f=8&rsv_bp=1&rsv_spt=3&wd=%E5%90%88%E7%90%86%E8%A7%84%E5%88%92%E6%82%A8%E7%9A%84%E7%A1%AC%E7%9B%98%E5%88%86%E5%8C%BA&rsv_n=2&rsv_sug3=1&inputT=1120

4. du

       du - estimate file space usage

5. df

       df - report file system disk space usage

例如:

Mandatory arguments to long options are mandatory for short options too.

-a, --all
include dummy file systems

-B, --block-size=SIZE
use SIZE-byte blocks

--direct
show statistics for a file instead of mount point

--total
produce a grand total

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

-H, --si
likewise, but use powers of 1000 not 1024

-i, --inodes
list inode information instead of block usage
     -k     like --block-size=1K

       -l, --local
              limit listing to local file systems

       --no-sync
              do not invoke sync before getting usage info (default)

       -P, --portability
              use the POSIX output format

       --sync invoke sync before getting usage info

       -t, --type=TYPE
              limit listing to file systems of type TYPE

       -T, --print-type
              print file system type

       -x, --exclude-type=TYPE
              limit listing to file systems not of type TYPE

       Display  values  are  in units of the first available SIZE from --block-size, and the
       DF_BLOCK_SIZE, BLOCK_SIZE and  BLOCKSIZE  environment  variables.   Otherwise,  units
       default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

       SIZE  may be (or may be an integer optionally followed by) one of following: KB 1000,
       K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.


6. gedit

gedit - text editor for the GNOME Desktop

      While aiming at simplicity and ease of use, gedit is a powerful general

       purpose text editor.  It can be used to create and edit  all  kinds  of

       text files

It will start a GUI to open a file, as the same as the notepad on Windows.

7. md5sum

 md5sum - compute and check MD5 message digest

 Print or check MD5 (128-bit) checksums.

它能够标识文件内容信息。

8 sed原地替换命令

sed -i "s/sourceString/replaceStr/g" fileName


 sed - stream editor for filtering and transforming text

  -i[SUFFIX], --in-place[=SUFFIX]

              edit  files in place (makes backup if extension supplied).  The default operation mode is to break symbolic and hard links.  This can be changed with --follow-sym-

              links and --copy.

-i 选项是直接修改文件。

9,perl原地替换命令

perl -pi -e 's/\[\% NAME_NODE \%\]/hdfs:\/\/localhost:8020/g' propertieFile


>perl -h

Usage: perl [switches] [--] [programfile] [arguments]
-0[octal]       specify record separator (\0, if no argument)
-a              autosplit mode with -n or -p (splits $_ into @F)
-C[number/list] enables the listed Unicode features
-c              check syntax only (runs BEGIN and CHECK blocks)
-d[:debugger]   run program under debugger
-D[number/list] set debugging flags (argument is a bit mask or alphabets)
-e program      one line of program (several -e's allowed, omit programfile)
-F/pattern/     split() pattern for -a switch (//'s are optional)
-i[extension]   edit <> files in place (makes backup if extension supplied)
-Idirectory     specify @INC/#include directory (several -I's allowed)
-l[octal]       enable line ending processing, specifies line terminator
-[mM][-]module  execute `use/no module...' before executing program
-n              assume 'while (<>) { ... }' loop around program
-p              assume loop like -n but print line also, like sed
-P              run program through C preprocessor before compilation
-s              enable rudimentary parsing for switches after programfile
-S              look for programfile using PATH environment variable
-t              enable tainting warnings
-T              enable tainting checks
-u              dump core after parsing program
-U              allow unsafe operations
-v              print version, subversion (includes VERY IMPORTANT perl info)
-V[:variable]   print configuration summary (or a single Config.pm variable)
-w              enable many useful warnings (RECOMMENDED)
-W              enable all warnings
-x[directory]   strip off text before #!perl line and perhaps cd to directory
-X              disable all warnings


使用 perl 命令行参数-p,-i也可以做到原地替换。

10. basename

basename -strip directory and suffix from filenames

Usage
basename NAME [SUFFIX] or basename OPTION .. NAME..

Print NAME with any leading directory components removed. If specified, also remove a trailing SUFFIX

e.g

       -s, --suffix=SUFFIX

              remove a trailing SUFFIX

       -z, --zero

              separate output with NUL rather than newline

basename /usr/bin/sort
-> "sort"

basename include/stdio.h .h
-> "stdio"

basename -s .h include/stdio.h
-> "stdio"

basename -a any/str1 any/str2
-> "str1" followed by "str2"0> basename -s _ch02.xml build_ch02.xml 

build
0> basename build_ch02.xml  _ch02.xml

build
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux gdb shell