Linux下的一些使用经验小结_3

本文最后更新于2015年5月16日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

11个让你吃惊的 Linux 终端命令 – 技术

==

Linux下查看某一进程的绝对路径信息

搜索关键字:
  • linux get the full exe path of a process
参考解答:

Try this:

$ ls -l /proc/8505/exe

Or if you don’t want to parse the output of ls, just do:

$ readlink /proc/8505/exe

or

$ realpath /proc/8505/exe

==

abs_path=$(sudo readlink "/proc/$pid/exe" | sed 's/ (deleted)//')
参考链接:

==

#sed/awk的一些例子

A Line-Up of Characters (sed & awk, Second Edition)

#xargs命令的一些例子

https://jonlabelle.com/snippets/view/shell/xargs-command

#找出本进程运行在哪个用户

exec_user=$(ps -o user --no-headers "$pid")

#过滤掉空行

echo "$tmp" | grep -v '^$'

Linux下相关信息的获取{通过/proc文件系统}

  • 获取某一进程对应的应用程序的绝对路径;
  • 获取某一进程对应的应用程序所在目录;
  • 命令行参数、环境变量信息、CPU、内存等状态信息;
  • ……
Process-Specific Subdirectories

The directory  /proc  contains  (among other things) one subdirectory for each process running on the system, which is named after the process ID (PID).

The link  self  points  to  the  process reading the file system. Each process subdirectory has the entries listed in Table 1-1.

Table 1-1: Process specific entries in /proc

File		Content
clear_refs	Clears page referenced bits shown in smaps output
cmdline		Command line arguments
cpu		Current and last cpu in which it was executed	(2.4)(smp)
cwd		Link to the current working directory
environ		Values of environment variables
exe		Link to the executable of this process
fd		Directory, which contains all file descriptors
maps		Memory maps to executables and library files	(2.4)
mem		Memory held by this process
root		Link to the root directory of this process
stat		Process status
statm		Process memory status information
status		Process status in human readable form
wchan		If CONFIG_KALLSYMS is set, a pre-decoded wchan
pagemap		Page table
stack		Report full stack trace, enable via CONFIG_STACKTRACE
smaps		a extension based on maps, showing the memory consumption of each mapping and flags associated with it
numa_maps	an extension based on maps, showing the memory locality and binding policy as well as mem usage (in pages) of each mapping.

For example, to get the status information of a process, all you have to do is read the file /proc/PID/status.

$ cat /proc/self/status
# 你能够通过上面的命令能够获取和ps命令几乎相同(甚至更丰富)的信息,实际上ps命令也就是通过读取 /proc 文件系统来获取信息的

/proc 文件系统的2大作用:

  1. 收集系统信息;
  2. 修改系统参数。
参考链接:

==

如果目录不存在则创建

搜索关键字:linux bash if dir not exist then make it

result_path="${HOME}/result"
[ -d "$result_path" ] || mkdir -p "$result_path"
参考链接:

==

For循环、条件测试、字符串截取

==

awk的使用学习

搜索关键字:awk match example

参考链接:
$ awk '/gold/' coins.txt
$ awk '/gold/ {print $5,$6,$7,$8}' coins.txt
$ awk '{if ($3 < 1980) print $3, "    ",$5,$6,$7,$8}' coins.txt
$ awk 'END {print NR,"coins"}' coins.txt
$ awk '/gold/{ num_gold++; wt_gold += $2 } END{print num_gold,wt_gold}'
$ awk -F'\x01' '{dict[$2]++}; END{for(x in dict) print dict[x]"\t"x}' access.log | sort -nr

$ ls -l | awk 'NR!=1 {s+=$5} END {print "Average: " s/(NR-1)}'

$ awk '/foo/ { print $2 }' info.log
$ awk '$1 ~ /J/' info.log

$ echo "xxx=a yyy=b zzz=c" | awk '{match($0,"yyy=([^ ]+)",a)}END{print a[0],a[1],RSTART,RLENGTH}'
yyy=b b 7 5
$ echo "xxx=a yyy=b zzz=c" | awk '{match($0,"yyy=([^ ]+)",a)}END{print a[0],a[1]}'
yyy=b b
Linux下如何对压缩过的文件进行字符串查找操作

==

Linux下的日期格式化
搜索关键字:
  • linux convert english time format to year month day
  • linux human readable date format

以如下的日志为例:

May 5 03:28:01 hostname rsyslogd: [origin software=”rsyslogd” swVersion=”6.1.1″ x-pid=”1111″ x-info=”http://www.rsyslog.com”] rsyslogd was HUPed

如何把上面的日期字符串”May 5 03:28:01″转换为”2015-05-05 03:28:01″这样的格式?

解决方法(date命令的-d选项):
date_str=$(echo "$item" | awk '{print $1,$2,$3}')
date=$(date -d "$date_str" +'%Y-%m-%d %H:%M:%S')

# date -d@1234567890
Sat Feb 14 07:31:30 CST 2009
# date -d@1234567890 +'%F %T'
2009-02-14 07:31:30
参考链接:

==

MySQL终端提示符

之前因为某个机会,阅读了MySQL客户端的源码,用于记录通过MySQL客户端交互式执行的命令,其中有个需求就是:记录在执行当前命令时的连接信息(主机名、用户名库名、表名……等信息)。但一般情况下,我们在终端中执行命令其实也是需要这样的提示的——知道自己现在所处的位置,防止误操作。一个简单的解决办法就是——修改MySQL终端提示符(MYSQL_PS1)。

搜索关键字:
  • MySQL prompt
  • MySQL MYSQL_PS1
参考解答:

一、在 ~/.bash_profile 或 ~/.bashrc 中添加如下命令:

export MYSQL_PS1="\u@\h [\d]> "

二、在 my.cnf 中可以通过添加选项 prompt=’\u@\h >’ 实现类似的功能!另一个不错的 my.cnf 的配置:

[mysql]
prompt = \\R:\\m:\\s\_\h:\p\_\\d>
参考链接:

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/2154.html

6 thoughts on “Linux下的一些使用经验小结_3”

  1. 文件 `/proc/$pid/cmdline` 中的内容是以 `\0` 字符进行分隔的,如果需要以一个比较正常的格式进行查看(空格分隔),可以使用下面的方法:
    `
    # cat /proc/$pid/cmdline | strings -1
    # cat /proc/$pid/cmdline | xargs -0 echo
    # cat /proc/$pid/cmdline | tr ‘\000’ ‘ ‘
    # cat /proc/$pid/cmdline | tr ‘\0’ ‘ ‘
    # cat -v /proc/$pid/cmdline
    `

    `
    This holds the complete command line for the process, unless the process is a zombie. In the latter case, there is nothing in this file: that is, a read on this file will return 0 characters. The command-line arguments appear in this file as a set of strings separated by null bytes (‘\0’), with a further null byte after the last string.
    `
    参考链接:
    http://stackoverflow.com/questions/993452/splitting-proc-cmdline-arguments-with-spaces
    http://stackoverflow.com/questions/1585989/how-to-parse-proc-pid-cmdline
    http://unix.stackexchange.com/questions/243142/how-to-show-quoted-command-list
    https://linux.die.net/man/5/proc

发表评论

电子邮件地址不会被公开。 必填项已用*标注