本节内容:
newlisp获取cpu信息
之前我们介绍过 Linux CPU 负载度量公式,今天使用newlisp写了一个获取本机cpu信息的小程序,每次都会调用REST API将数据发送给linux系统下的web server.
例子:
(load "config.lsp")
(define (add-log msg)
(append-file "cpu.log" (append "\n" (string (now 480)) " "))
(append-file "cpu.log" (append ": " msg))
)
;; return a list
;; which contains total_jiffies and work_jiffies
(define (check-cpu)
(set 'in-file (open "/proc/stat" "read"))
(set 'line (read-line in-file))
(set 'r (parse line))
(close in-file)
(set 'total_jiffies 0)
(println r)
(set 'i 1)
(do-while (< i 8)
(set 'total_jiffies (+ total_jiffies (int (nth i r))))
(inc i)
)
(set 'work_jiffies 0)
(set 'i 1)
(do-while (< i 3)
(set 'work_jiffies (+ work_jiffies (int (nth i r))))
(inc i)
)
(list total_jiffies work_jiffies)
)
(set 'r2 (check-cpu))
(set 'r3 (post-url "http://localhost/wind_tunnel/api/post/cpu"
(format "ip=%s&hostName=%s&epoch=%lld&totalJiffies=%lld&workJiffies=%lld" ip host_name 123456789 (nth 0 r2) (nth 1 r2))))
(add-log r3)
(exit)
config.lsp文件,两行配置:
(set 'ip "192.168.1.101")
本节内容:
ps命令常用参数
一直都用ps命令,但是很少去琢磨其中参数的含义,今天分享一些ps命令的常用参数,供大家参考。
我的常用命令:
-d Select all processes except session leaders.
什么是session leader。
参考该解释:http://www.win.tue.nl/~aeb/linux/lk/lk-10.html#ss10.3
Every session may have a controlling tty, that then also is called the controlling tty of each of its member processes. A file descriptor for the controlling tty is obtained by opening /dev/tty. (And when that fails, there was no controlling tty.) Given a file descriptor for the controlling tty, one may obtain the SID using tcgetsid(fd).
A session is often set up by a login process. The terminal on which one is logged in then becomes the controlling tty of the session. All processes that are descendants of the login process will in general be members of the session.
进程是按照进程组管理的,进程组又属于session。
关系如下:
每个session拥有一个或者多个进程组,每个进程组拥有一个或多个进程。
第一个属于某个session的进程id就是这个session的 leader, session id就用它的进程id。
和进程相关的id有几种,进程id, 父进程id, 进程组id 和 session id.
参考:http://unix.stackexchange.com/questions/18166/what-are-session-leaders-in-ps
-e 参数等同于 -A,
[plain] view plaincopyprint?
-e Select all processes. Identical to -A.
-f 参数 显示完整格式
[plain] view plaincopyprint?
-f Do full-format listing. This option can be combined with many other UNIX-style options to add additional columns. It also causes the command arguments to be
printed. When used with -L, the NLWP (number of threads) and LWP (thread ID) columns will be added. See the c option, the format keyword args, and the
format keyword comm.
不过还有个常用的方式:
ps axu
不同于ps -def 用标准风格,这是BSD风格(不用-作为引导参数), BSD风格会在另一篇博客中介绍。
-a 参数
-a Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
-x 参数 和tty有关,tty会在另一篇文章中解释
x Lift the BSD-style "must have a tty" restriction, which is imposed upon the set of all processes when some BSD-style (without "-") options are used or when
the ps personality setting is BSD-like. The set of processes selected in this manner is in addition to the set of processes selected by other means. An
alternate description is that this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the
a option.
-u 参数
-u userlist
Select by effective user ID (EUID) or name. This selects the processes whose effective user name or ID is in userlist.
The effective user ID describes the user whose file access permissions are used by the process (see geteuid(2)). Identical to U and --user.
还有一种,通过直接输入命令查找,用-C参数,比如:
PID TTY TIME CMD
1258 ? 00:00:00 nginx
1259 ? 00:00:00 nginx
CHN\shu6889@sloop2:~$ ps aux | grep nginx
root 1258 0.0 0.0 31188 1028 ? Ss 09:24 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root 1259 0.0 0.0 47312 2708 ? S 09:24 0:00 nginx: worker process
70780027 4336 0.0 0.0 13652 976 pts/1 S+ 10:40 0:00 grep --color=auto nginx
注意,前面需要输入UNIX95=。
相关阅读:linux命令实例教程,大家可以深入学习下常用的linux命令。
本节内容:
linux系统中命令行弹出光驱
例如:使用eject命令弹出光驱
查看光驱就用检查pci的命令:
00:00.0 Host bridge: Advanced Micro Devices, Inc. [AMD/ATI] RD780 Host Bridge
00:02.0 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] RX780/RD790 PCI to PCI bridge (external gfx0 port A)
00:04.0 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] RD790 PCI to PCI bridge (PCI express gpp port A)
00:0a.0 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] RD790 PCI to PCI bridge (PCI express gpp port F)
00:11.0 SATA controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 SATA Controller [IDE mode]
00:12.0 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller
00:12.1 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller
00:12.2 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller
00:13.0 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI0 Controller
00:13.1 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0 USB OHCI1 Controller
00:13.2 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB EHCI Controller
00:14.0 SMBus: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 SMBus Controller (rev 3c)
00:14.1 IDE interface: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 IDE Controller
00:14.2 Audio device: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 Azalia (Intel HDA)
00:14.3 ISA bridge: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 LPC host controller
00:14.4 PCI bridge: Advanced Micro Devices, Inc. [AMD/ATI] SBx00 PCI to PCI Bridge
00:14.5 USB controller: Advanced Micro Devices, Inc. [AMD/ATI] SB7x0/SB8x0/SB9x0 USB OHCI2 Controller
00:18.0 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 10h Processor HyperTransport Configuration
00:18.1 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 10h Processor Address Map
00:18.2 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 10h Processor DRAM Controller
00:18.3 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 10h Processor Miscellaneous Control
00:18.4 Host bridge: Advanced Micro Devices, Inc. [AMD] Family 10h Processor Link Control
01:00.0 VGA compatible controller: Advanced Micro Devices, Inc. [AMD/ATI] Juniper XT [Radeon HD 5770]
01:00.1 Audio device: Advanced Micro Devices, Inc. [AMD/ATI] Juniper HDMI Audio [Radeon HD 5700 Series]
02:00.0 SATA controller: JMicron Technology Corp. JMB363 SATA/IDE Controller (rev 02)
02:00.1 IDE interface: JMicron Technology Corp. JMB363 SATA/IDE Controller (rev 02)
03:00.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller (rev 02)
04:0e.0 FireWire (IEEE 1394): Texas Instruments TSB43AB23 IEEE-1394a-2000 Controller (PHY/Link)
以上输出中的那个IDE接口,就是光驱。
命令行运行是ok,不过报错,无法弹出光驱。
原来是机械故障,:). 用针捅那个眼,有个东西被顶开,然后就搞定了,光驱哇地就弹出了,呵呵。