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

Process management in Linux

2010-06-26 22:48 609 查看
用户管理

1. useradd

-m :建立home目录

-s
:指定shell类型

-u :指定UID

-g :指定初始用户组

-G :其他用户组

添加用户的一些默认属性会存放在/etc/default/useradd中,如默认的shell等

用户home目录的内容会从/etc/skel/中copy。

2. passwd userName

新添的用户必须设置密码才能登录

3. userdel 删除用户

userdel -r fredchen
//连home目录一起删除

4. usermod 修改用户属性

usermod -e "2006-01-01"
userName //密码在2006年第一天失效

usermod -L/U userName
//用户密码被冻结/恢复

5. passwd username

6. su
进入root帐号以后,当前的环境仍为进入前的环境

su - 此时才为root的环境

su还可以变换成普通用户的身份,如su -l fredchen

7.
执行sudo命令需要输入的密码是当前用户的密码,而非root的。

Chapter
17 程序和资源管理

1. 查看进程树pstree

fredchen@fredchen:~$
pstree

init─┬─NetworkManager─┬─dhclient


└─{NetworkManager}

├─acpid

├─atd

├─avahi-daemon───avahi-daemon

├─bluetoothd

├─console-kit-dae───63*[{console-kit-dae}]

├─cron

├─cupsd

├─dbus-daemon

├─dd

├─exim4

├─6*[getty]

├─hald───hald-runner─┬─hald-addon-acpi


├─hald-addon-inpu


└─hald-addon-stor

├─klogd

├─nm-system-setti

├─rlinetd─┬─in.telnetd───login───bash───bash


├─in.telnetd───login───bash


└─in.telnetd───login───bash───pstree

├─syslogd

├─system-tools-ba

├─udevd

└─wpa_supplicant

2.
使用后台执行命令时,如果命令有输出,则一定要将其重定向到文件,否则还是会输出到屏幕

如 tar -zpcvf
file.tar.gz file & 最好改写成

tar -zpcvf
file.tar.gz file > log.txt 2>&1 &

3. ctrl + Z
将当前作业放到后台暂停

fredchen@fredchen:~$ vi ss

//ctrl+Z

[1]+
Stopped
vi ss

fredchen@fredchen:~$ jobs -l

[1]+ 5595
Stopped
vi ss

fredchen@fredchen:~$ fg //back to the background
command

vi ss

+ 表示使用fg默认第一个恢复的作业

fg: foreground fg
%jobnumber

bg: background bg %jobnumber

jobs:
显示当前的job

fredchen@fredchen:~$ find / -perm +7000 > log.txt
2>&1

^Z

[1]+
Stopped
find / -perm +7000 > log.txt 2>&1

fredchen@fredchen:~$ jobs ; bg
%1; jobs

[1]+
Stopped
find / -perm +7000 > log.txt 2>&1

[1]+ find / -perm +7000 >
log.txt 2>&1 &

[1]+
Running
find / -perm +7000 > log.txt 2>&1
&

4. 终止job kill
-signal %jobnumber

kill -9 %jobnumber
//立刻终止并删除该job

kill -15 %jobnumber
//以正常方式终止job

kill -l //show所有signal
如9和15就是其中两个

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

5. ps

ps -l //列出本次登录的PID

fredchen@fredchen:~$ ps -l

F
S UID PID PPID C PRI NI ADDR SZ
WCHAN TTY TIME
CMD

4 S 1000 5495 5395 0 80 0
- 1419 wait pts/2 00:00:00 bash

0 R
1000 5614 5495 4 80 0 - 629
- pts/2 00:00:00
ps

说明:

F:进程flag

S: 进程状态

PID: 进程ID

C:CPU使用资源百分比

PRI:
priority

NI:
nice值

ADDR:进程在内存的地址

SZ:用掉的内存大小

WCHAN:当前进程是否正在运行

TIME:
用掉的CPU时间

ps aux //列出所有正在内存中的进程, 注: aux前没有-

fredchen@fredchen:~$ ps
aux

USER PID %CPU %MEM
VSZ RSS TTY STAT START
TIME COMMAND

root 1
0.0 0.7 3056 1888
? Ss Apr01
0:03 /sbin/init

root 2
0.0 0.0 0 0
? S< Apr01
0:00 [kthreadd]

root 3
0.0 0.0 0 0
? S< Apr01
0:00 [migration/0]

root
4 0.0 0.0 0 0
? S< Apr01
0:00 [ksoftirqd/0]

root
5 0.0 0.0 0 0
? S< Apr01
0:00 [watchdog/0]

root
6 0.0 0.0 0 0
? S< Apr01
0:00 [events/0]

root 7
0.0 0.0 0 0
? S< Apr01
0:00 [khelper]

root 46
0.0 0.0 0 0
? S< Apr01
0:00 [kintegrityd/0]

root 48
0.0 0.0 0 0
? S< Apr01
0:00 [kblockd/0]

root 50
0.0 0.0 0 0
? S< Apr01
0:00 [kacpid]

root 51
0.0 0.0 0 0
? S< Apr01
0:00 [kacpi_notify]

root 109
0.0 0.0 0 0
? S< Apr01
0:00 [cqueue]

root 113 0.0
0.0 0 0
? S< Apr01
0:00 [kseriod]

root 154 0.0
0.0 0 0
? S
Apr01 0:00 [pdflush]

root
155 0.0 0.0 0
0 ? S
Apr01 0:00 [pdflush]

root
156 0.0 0.0 0
0 ? S<
Apr01 0:00 [kswapd0]

root
198 0.0 0.0 0
0 ? S<
Apr01 0:00 [aio/0]

root 1118
0.0 0.0 0 0
? S< Apr01
0:00 [ksuspend_usbd]

root 1121 0.0
0.0 0 0
? S< Apr01
0:00 [khubd]

root 1134 0.0
0.0 0 0
? S< Apr01
0:00 [mpt_poll_0]

root 1139 0.0
0.0 0 0
? S< Apr01
0:05 [ata/0]

root 1141 0.0
0.0 0 0
? S< Apr01
0:00 [ata_aux]

root 1767 0.0
0.0 0 0
? S< Apr01
0:00 [scsi_eh_0]

root 1828 0.0
0.0 0 0
? S< Apr01
0:00 [scsi_eh_1]

root 1833 0.0
0.0 0 0
? S< Apr01
0:00 [scsi_eh_2]

root 1957 0.0
0.0 0 0
? S< Apr01
0:00 [kjournald]

root 2131 0.0
0.4 2528 1020 ?
S<s Apr01 0:04 /sbin/udevd
--daemon

root 2317 0.0
0.0 0 0
? S< Apr01
0:00 [kpsmoused]

root 2578 0.0
0.0 0 0
? S< Apr01
0:00 [kgameportd]

root 4003 0.0
0.2 1780 540 tty4 Ss+
Apr01 0:00 /sbin/getty 38400
tty4

root 4004 0.0 0.2
1780 540 tty5 Ss+ Apr01
0:00 /sbin/getty 38400 tty5

root 4012
0.0 0.2 1780 540 tty2
Ss+ Apr01 0:00 /sbin/getty 38400
tty2

root 4015 0.0 0.2
1780 532 tty3 Ss+ Apr01
0:00 /sbin/getty 38400 tty3

root 4017
0.0 0.2 1780 536 tty6
Ss+ Apr01 0:00 /sbin/getty 38400
tty6

root 4174 0.0 0.4
2308 1228 ? Ss
Apr01 0:00 /usr/sbin/acpid -c /etc/acpi/events -s
/var/run/acpid.socket

root 4203 0.0
0.0 0 0
? S< Apr01
0:00 [kondemand/0]

syslog 4288 0.0
0.2 2012 736 ?
Ss Apr01 0:00 /sbin/syslogd -u
syslog

root 4339 0.0
0.2 1940 544 ?
S Apr01 0:00 /bin/dd bs 1 if /proc/kmsg of
/var/run/klogd/kmsg

klog 4341 0.0
0.8 3380 2228 ?
Ss Apr01 0:00 /sbin/klogd -P
/var/run/klogd/kmsg

108 4364
0.0 0.3 2772 924
? Ss Apr01
0:00 /bin/dbus-daemon --system

avahi 4386
0.0 0.5 2888 1480
? Ss Apr01
0:00 avahi-daemon: running [fredchen.local]

avahi
4387 0.0 0.1 2888 500
? Ss Apr01
0:00 avahi-daemon: chroot helper

root
4430 0.0 0.8 6408 2176
? Ss Apr01
0:00 /usr/sbin/cupsd

113 4689
0.0 0.3 6340 984
? Ss Apr01
0:00 /usr/sbin/exim4 -bd -q30m

root 4746
0.0 0.3 2084 964
? Ss Apr01
0:00 /usr/sbin/rlinetd

111 4778
0.0 1.5 6288 4052
? Ss Apr01
0:00 /usr/sbin/hald

root 4781 0.0
0.9 16388 2540 ? Ssl
Apr01 0:00
/usr/sbin/console-kit-daemon

root 4844
0.0 0.4 3364 1104
? S
Apr01 0:00 hald-runner

root
4864 0.0 0.4 3436 1072
? S
Apr01 0:00 hald-addon-input: Listening on /dev/input/event3
/dev/input/event1

root 4869 0.0
0.4 3440 1064 ?
S Apr01 0:01 hald-addon-storage: polling /dev/scd0
(every 2 sec)

111 4872 0.0
0.3 2296 900 ?
S Apr01 0:00 hald-addon-acpi: listening on acpid
socket /var/run/acpid.socket

root 4917
0.0 0.6 3488 1552
? Ss Apr01
0:00 /usr/sbin/bluetoothd

root 4922
0.0 0.0 0 0
? S< Apr01
0:00 [btaddconn]

root 4924 0.0
0.0 0 0
? S< Apr01
0:00 [btdelconn]

root 4938 0.0
0.0 0 0
? S< Apr01
0:00 [krfcommd]

root 4972 0.0
0.9 14632 2520 ? Ssl
Apr01 0:01
/usr/sbin/NetworkManager

root 4979
0.0 0.5 4240 1336
? S
Apr01 0:00 /sbin/wpa_supplicant -u -f
/var/log/wpa_supplicant.log

root 4982
0.0 1.1 6768 2968
? S
Apr01 0:00 /usr/sbin/nm-system-settings --config
/etc/NetworkManager/nm-system-settings.conf

root
4995 0.0 0.4 4336 1148
? Ss Apr01
0:00 /usr/bin/system-tools-backends

daemon 5032
0.0 0.1 2068 456
? Ss Apr01
0:00 /usr/sbin/atd

root 5060 0.0
0.4 3412 1024 ?
Ss Apr01 0:00
/usr/sbin/cron

root 5147 0.0
0.6 2988 1576 tty1 Ss
Apr01 0:00 /bin/login --

root
5150 0.0 0.3 2252 976
? S
Apr01 0:00 /sbin/dhclient -d -sf
/usr/lib/NetworkManager/nm-dhcp-client.action -pf
/var/run/dhclient-et

telnetd 5394 0.0 0.3
2020 844 ?
S Apr01 0:00 in.telnetd:
::ffff:192.168.0.1

root 5395 0.0
0.6 2988 1576 pts/2 Ss
Apr01 0:00 login -h ::ffff:192.168.0.1 -p

fredchen
5495 0.0 1.2 5676 3080 pts/2
S Apr01 0:00 -bash

fredchen 5672
0.0 1.1 5676 3048 tty1
S+ 00:17 0:00 -bash

fredchen 5756
0.0 0.3 2744 1016 pts/2 R+
00:23 0:00 ps aux

VSZ: 虚拟内存量 kb

RSS: 固定内存量
kb

STAT:

-R: run 该进程当前正在运行,或者可运行

-S: sleep

-T:
该进程正在检测或停止

-Z: ZOMBIE 该进程已经挂了,但父进程却无法终止它,僵尸进程

TIME:
使用CPU运行的时间

BTW

fredchen@fredchen:~$ w //this command show
who is logged on and what are they doing

00:37:29 up 2:01,
4 users, load average: 0.95, 0.49, 0.37

USER
TTY
FROM
LOGIN@ IDLE JCPU PCPU WHAT

fredchen
tty1
-
00:17 1:09m 0.36s 0.32s -bash

fredchen
pts/0 ::ffff:192.168.0 00:37 0.00s
0.32s 0.02s w

fredchen pts/2 ::ffff:192.168.0
23:01 34.00s 0.46s 0.42s -bash

tty
为pts表示是通过远程网络登录的,每个session会有一个pts号

而tty1则是本地终端

6.
TOP动态查看进程信息,和windows任务管理器类似

在top命令中的子命令

P:
将显示的进程按cpu占有率排序

M: 按内存使用量排序

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