1、收集进程信息
[root@stone ~]# ps
PID TTY TIME CMD
17209 pts/1 00:00:00 bash
17436 pts/1 00:00:00 ps
[root@stone ~]# ps -f
UID PID PPID C STIME TTY TIME CMD
root 17209 17206 0 08:29 pts/1 00:00:00 -bash
root 17439 17209 0 09:24 pts/1 00:00:00 ps -f
#-f选项显示更多信息列
[root@stone ~]# ps -e | head -5
PID TTY TIME CMD
1 ? 00:00:00 init
2 ? 00:00:00 migration/0
3 ? 00:00:00 ksoftirqd/0
4 ? 00:00:00 watchdog/0
#-e选项打印出所有进程
[root@stone ~]# ps -ef | head -5
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 May22 ? 00:00:00 init [5]
root 2 1 0 May22 ? 00:00:00 [migration/0]
root 3 1 0 May22 ? 00:00:00 [ksoftirqd/0]
root 4 1 0 May22 ? 00:00:00 [watchdog/0]
[root@stone ~]# ps -eo comm,pcpu | head -5
COMMAND %CPU
init 0.0
migration/0 0.0
ksoftirqd/0 0.0
watchdog/0 0.0
#-o选项指定需要的列参数
列参数及含义:
参数
|
描述
|
pcpu
|
CPU占用率
|
pid
|
进程ID
|
ppid
|
父进程ID
|
pmen
|
内存使用率
|
comm
|
命令名称
|
cmd |
简单命令 |
user |
启动进程的用户 |
nice |
优先级 |
time |
累积的CPU时间 |
etime |
进程启动后度过的时间 |
tty |
所关联的tty设备 |
euid |
有效用户ID
|
stat |
进程状态 |
[root@stone ~]# ps -eo comm,pcpu --sort -pcpu | head -5
COMMAND %CPU
sshd 0.0
Xorg 0.0
yum-updatesd 0.0
dbus-daemon 0.0
#--sort表示排序,-pcpu表示降序排列,+pcpu表示升序排列
[root@stone ~]# ps -C bash
PID TTY TIME CMD
18508 pts/1 00:00:00 bash
18560 pts/2 00:00:00 bash
#-C选项列出指定命令的进程信息
[root@stone ~]# ps -C bash -o pid=
18508
18560
[root@stone ~]# pgrep bash
18508
18560
[root@stone ~]# pgrep bash -d ":"
18508:18560
#-d选项重新定义分隔符
[root@stone ~]# pgrep -u stone bash
18619
#-u指定用户
[root@stone ~]# pgrep -u stone
18618
18619
[root@stone ~]# pgrep -lu stone
18618 sshd
18619 bash
[root@stone ~]# ps -u root -U root -o user,pcpu,comm | head -5
USER %CPU COMMAND
root 0.0 init
root 0.0 migration/0
root 0.0 ksoftirqd/0
root 0.0 watchdog/0
#-u指定有效用户
#-U指定真实用户
[root@stone ~]# ps -t pts/1,pts/2,pts/3
PID TTY TIME CMD
18508 pts/1 00:00:00 bash
18560 pts/2 00:00:00 bash
18619 pts/3 00:00:00 bash
18762 pts/1 00:00:00 ps
#-t表示终端
[root@stone ~]# ps -eLf | head
UID PID PPID LWP C NLWP STIME TTY TIME CMD
root 1 0 1 0 1 May22 ? 00:00:00 init [5]
root 2 1 2 0 1 May22 ? 00:00:00 [migration/0]
root 3 1 3 0 1 May22 ? 00:00:00 [ksoftirqd/0]
root 4 1 4 0 1 May22 ? 00:00:00 [watchdog/0]
root 5 1 5 0 1 May22 ? 00:00:00 [migration/1]
root 6 1 6 0 1 May22 ? 00:00:00 [ksoftirqd/1]
root 7 1 7 0 1 May22 ? 00:00:00 [watchdog/1]
root 8 1 8 0 1 May22 ? 00:00:00 [events/0]
root 9 1 9 0 1 May22 ? 00:00:00 [events/1]
#-L选项显示线程信息,NLWP是进程的线程数量
[root@stone ~]# ps -eo pid,cmd e | head -2
PID CMD
1 init [5] HOME=/ TERM=linux SELINUX_INIT=YES
#e表示显示环境变量
[root@stone ~]# ps -C bash -o cmd e > ps.e
[root@stone ~]# cat ps.e
CMD
-bash USER=root LOGNAME=root HOME=/root PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin MAIL=/var/mail/root SHELL=/bin/bash SSH_CLIENT=172.16.3.151 3479 22 SSH_CONNECTION=172.16.3.151 3479 172.16.3.54 22 SSH_TTY=/dev/pts/1 TERM=linux
#显示某个进程的环境变量
2、发送信号
[root@stone ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE
9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT
17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU
25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH
29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN
35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4
39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6
59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
#-l选项打印所有信号
常用信号
信号名称
|
信号序号
|
含义
|
SIGHUP
|
1
|
挂起检查
|
SIGINT
|
2
|
Ctrl+C
|
SIGKILL
|
9
|
杀死进程
|
SIGTERM
|
15
|
终止进程
|
SIGTSTP
|
20
|
Ctrl+Z
|
[root@stone ~]# ps -C bash
PID TTY TIME CMD
26826 pts/1 00:00:00 bash
26934 pts/2 00:00:00 bash
27281 pts/3 00:00:00 bash
[root@stone ~]# kill -9 27281
[root@stone ~]# ps -C bash
PID TTY TIME CMD
26826 pts/1 00:00:00 bash
26934 pts/2 00:00:00 bash
[root@stone ~]# ps -C bash
PID TTY TIME CMD
26826 pts/1 00:00:00 bash
26934 pts/2 00:00:00 bash
27244 pts/3 00:00:00 bash
[root@stone ~]# killall -u stone -9 bash
[root@stone ~]# ps -C bash
PID TTY TIME CMD
26826 pts/1 00:00:00 bash
26934 pts/2 00:00:00 bash
#killall直接使用进程名称进行操作
#-u指定用户
[root@stone ~]# cat bin/sighandle.sh
#!/bin/bash
function handler()
{
echo Hey,received signal:SIGINT
}
echo My process ID is $$
trap 'handler' SIGINT
while true;
do
sleep 1
done
#trap捕捉并相应信号,当按下Ctrl+Z产生SIGINT信号时,触发函数“handler”
3、which whereis file whatis
[root@stone ~]# which ls
alias ls='ls --color=tty'
/bin/ls
#which显示命令的具体路径
[root@stone ~]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
#whereis显示命令的具体路径和帮助路径
[root@stone ~]# file /bin/ls
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.9, dynamically linked (uses shared libs), for GNU/Linux 2.6.9, stripped
#file显示文件类型
[root@stone ~]# whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
#whatis显示命令的简短描述
[root@stone ~]# uptime
14:19:58 up 5 days, 20:34, 1 user, load average: 0.00, 0.00, 0.00
#1分钟,5分钟,10分钟的平均负载
4、发送消息
[root@stone ~]# cat num1 | wall
[root@stone ~]# echo this is a message | wall
[root@stone ~]# mesg y
#允许消息写入
[root@stone ~]# mesg n
#禁止消息写入
[root@stone ~]# cat bin/message_user.sh
#!/bin/bash
user=$1
devices=`ls /dev/pts/* -l | awk '{print $3,$10}' | grep $user | awk '{print $2}'`
for dev in $devices;
do
cat /dev/stdin > $dev
done
#指定用户终端发送消息
5、收集系统信息
[root@stone ~]# hostname
stone
[root@stone ~]# uname -n
stone
#主机名
[root@stone ~]# uname -a
Linux stone 2.6.18-164.el5xen #1 SMP Tue Aug 18 16:06:30 EDT 2009 i686 i686 i386 GNU/Linux
#系统详细信息
[root@stone ~]# uname -r
2.6.18-164.el5xen
#内核版本
[root@stone ~]# uname -m
i686
[root@stone ~]# uname
Linux
[root@stone ~]# cat /proc/cpuinfo
[root@stone ~]# cat /proc/meminfo
[root@stone ~]# cat /proc/partitions
6、任务调度cron
[root@stone ~]# crontab << EOF
> 00 02 1 * * df -h
> EOF
[root@stone ~]# crontab -l
00 02 1 * * df -h
[root@stone ~]# crontab -l -u root
00 02 1 * * df -h
[root@stone ~]# crontab -u root -r
[root@stone ~]# crontab -u root -l
no crontab for root