==
在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)//')
参考链接:
- http://unix.stackexchange.com/questions/8503/is-it-possible-to-find-the-path-of-a-running-program
- http://stackoverflow.com/questions/606041/how-do-i-get-the-path-of-a-process-in-unix-linux
- https://www.kernel.org/doc/Documentation/filesystems/proc.txt
- http://unix.stackexchange.com/questions/197854/how-does-the-proc-pid-exe-symlink-differ-from-ordinary-symlinks
- =
- linux – How to find out from which folder a process is running?
==
#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大作用:
- 收集系统信息;
- 修改系统参数。
参考链接:
- https://www.kernel.org/doc/Documentation/filesystems/proc.txt
- http://tldp.org/LDP/Linux-Filesystem-Hierarchy/html/proc.html
- http://linux.die.net/man/5/proc
==
如果目录不存在则创建
搜索关键字:linux bash if dir not exist then make it
result_path="${HOME}/result" [ -d "$result_path" ] || mkdir -p "$result_path"
参考链接:
- http://stackoverflow.com/questions/793858/how-to-mkdir-only-if-a-dir-does-not-already-exist
- http://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script
- http://stackoverflow.com/questions/6916559/test-multiple-file-conditions-in-one-swoop-bash
- http://tldp.org/LDP/abs/html/fto.html
- http://ixyzero.com/blog/archives/1225.html
==
For循环、条件测试、字符串截取
==
awk的使用学习
搜索关键字:awk match example
参考链接:
- http://stackoverflow.com/questions/5536018/how-to-get-match-regex-pattern-using-awk-from-file #Nice
- http://www.staff.science.uu.nl/~oostr102/docs/nawk/nawk_92.html #Nice2
- http://en.wikibooks.org/wiki/An_Awk_Primer/Search_Patterns_(1)
- http://en.wikibooks.org/wiki/An_Awk_Primer/Search_Patterns_(2)
- AWK Language Programming – Regular Expressions
- http://www.ibm.com/developerworks/library/l-awk3/ #Nice
- http://docstore.mik.ua/orelly/unix3/sedawk/ch03_02.htm
$ 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
参考链接:
- http://stackoverflow.com/questions/6508819/convert-date-formats-in-bash
- http://stackoverflow.com/questions/3249827/convert-from-unixtime-at-command-line
- http://unix.stackexchange.com/questions/2987/how-do-i-convert-an-epoch-timestamp-to-a-human-readable-format-on-the-cli
- http://stackoverflow.com/questions/11021460/getting-human-readable-date-from-epoch-into-variable
- http://unix.stackexchange.com/questions/107750/how-to-parse-iso8601-dates-with-linux-date-command
==
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>
参考链接:
- https://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html
- http://www.thegeekstuff.com/2010/02/mysql_ps1-6-examples-to-make-your-mysql-prompt-like-angelina-jolie/
- =
- http://ronaldbradford.com/blog/correctly-setting-your-mysql-prompt-using-sudo-2014-05-22/
- http://www.dev-garden.org/2011/07/20/put-the-database-hostname-in-your-mysql-prompt/
- http://www.codediesel.com/mysql/changing-mysql-clients-default-prompt/
- =
- https://github.com/romuald/config/blob/master/zsh/zshrc
《 “Linux下的一些使用经验小结_3” 》 有 6 条评论
Linux的一些操作技巧
https://jivoi.github.io/2014/06/22/linux-unix-it-tips-and-tricks-number-1/
https://jivoi.github.io/2015/06/29/linux-unix-it-tips-and-tricks-number-2/
https://jivoi.github.io/2015/07/01/linux-unix-it-tips-and-tricks-number-3/
https://jivoi.github.io/2015/08/17/linux-unix-it-tips-and-tricks-number-4/
文件 `/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
在Linux下如何获取当前进程的名称(How to get current process name in linux?)
http://stackoverflow.com/questions/9097201/how-to-get-current-process-name-in-linux
`
argv[0]
getenv(“_”)
/proc/self/cmdline
#if defined(__APPLE__) || defined(__FreeBSD__)
const char * appname = getprogname();
#elif defined(_GNU_SOURCE)
const char * appname = program_invocation_name;
#else
const char * appname = “?”;
#endif
`
PROC系列之—/proc/pid/status
http://blog.csdn.net/zjl_1026_2001/article/details/2294036
linux查看某个进程内存占用情况以及/proc/pid/status解释
http://www.cnblogs.com/youxin/p/5976194.html
Linux下/proc/[pid]/status 进程参数介绍
http://jungor.me/?p=166
`
Tgid: 线程组号
Pid: 进程ID
PPid: 父进程ID
TracerPid: 接收跟踪该进程信息的进程的ID号
Uid: Uid euid suid fsuid
Gid: Gid egid sgid fsgid
`
30 个有趣的 Linux Shell 命令
https://www.lopezferrando.com/30-interesting-shell-commands/
fzf – UNIX 命令行版本的模糊命令、文件、进程搜索工具
https://github.com/junegunn/fzf