[codes=c#]
ps -ef |grep hello |awk '{print $2}'|xargs kill -9
[/codes]
这里是输出ps -ef |grep hello 结果的第二列的内容然后通过xargs传递给kill -9,其实第二列内容就是hello的进程号!
附录:
1,awk是一种编程语言,用于在linux/unix下对文本和数据进行处理。数据可以来自标准输入、一个或多个文件,或其它命令的输出。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。它在命令行中使用,但更多是作为脚本来使用。awk的处理文本和数据的方式是这样的,它逐行扫描文件,从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作。如果没有指定处理动作,则把匹配的行显示到标准输出(屏幕),如果没有指定模式,则所有被操作所指定的行都被处理。awk分别代表其作者姓氏的第一个字母。因为它的作者是三个人,分别是Alfred Aho、Brian Kernighan、Peter Weinberger。gawk是awk的GNU版本,它提供了Bell实验室和GNU的一些扩展。
2. xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具。它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理。通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从文件的输出中读取数据。xargs的默认命令是echo,这意味着通过管道传递给xargs的输入将会包含换行和空白,不过通过xargs的处理,换行和空白将被空格取代。xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令.
一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
选定目录 /usr/local/
cd /usr/local/
2.安装PCRE库
cd /usr/local/
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install
3.安装zlib库
cd /usr/local/
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure
make
make install
4.安装ssl
cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
./config
make
make install
5.安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:
cd /usr/local/
wget http://nginx.org/download/nginx-1.2.8.tar.gz
tar -zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8
./configure --prefix=/usr/local/nginx
make
make install
--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。
6.启动
确保系统的 80 端口没被其他程序占用,
/usr/local/nginx/sbin/nginx
检查是否启动成功:
netstat -ano|grep 80 有结果输入说明启动成功
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
7.重启
/usr/local/nginx/sbin/nginx –s reload
8.修改配置文件
cd /usr/local/nginx/conf
vi nginx.conf
9.常用配置
#nginx运行用户和组
user www www;
#启动进程,通常设置成和cpu的数量相等
worker_processes 4;
#全局错误日志及PID文件
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;
#单个后台worker process进程的最大并发链接数
worker_connections 10240;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include mime.types;
default_type application/octet-stream;
error_page 400 403 500 502 503 504 /50x.html;
index index.html index.shtml
autoindex off;
fastcgi_intercept_errors on;
sendfile on;
# These are good default values.
tcp_nopush on;
tcp_nodelay off;
# output compression saves bandwidth
gzip off;
#gzip_static on;
#gzip_min_length 1k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_buffers 4 16k;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;
#gzip_vary on;
server_name_in_redirect off;
#设定负载均衡的服务器列表
upstream portals {
server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;
}
#upstream overflow {
# server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;
# server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;
#}
server {
#侦听8080端口
listen 8080;
server_name 127.0.0.1;
#403、404页面重定向地址
error_page 403 = http://www.e100.cn/ebiz/other/217/403.html;
error_page 404 = http://www.e100.cn/ebiz/other/218/404.html;
proxy_connect_timeout 90;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 128k;
client_header_buffer_size 16k;
large_client_header_buffers 4 64k;
#proxy_send_timeout 3m;
#proxy_read_timeout 3m;
#proxy_buffer_size 4k;
#proxy_buffers 4 32k;
proxy_set_header Host $http_host;
proxy_max_temp_file_size 0;
#proxy_hide_header Set-Cookie;
# if ($host != 'www.e100.cn' ) {
# rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;
# }
location / {
deny all;
}
location ~ ^/resource/res/img/blue/space.gif {
proxy_pass http://tecopera;
}
location = / {
rewrite ^(.*)$ /ebiz/event/517.html last;
}
location = /ebiz/event/517.html {
add_header Vary Accept-Encoding;
root /data/web/html;
expires 10m;
}
location = /check.html {
root /usr/local/nginx/html/;
access_log off;
}
location = /50x.html {
root /usr/local/nginx/html/;
expires 1m;
access_log off;
}
location = /index.html {
add_header Vary Accept-Encoding;
#定义服务器的默认网站根目录位置
root /data/web/html/ebiz;
expires 10m;
}
#定义反向代理访问名称
location ~ ^/ecps-portal/* {
# expires 10m;
#重定向集群名称
proxy_pass http://portals;
#proxy_pass http://172.16.68.134:8082;
}
location ~ ^/fetionLogin/* {
# expires 10m;
proxy_pass http://portals;
#proxy_pass http://172.16.68.134:8082;
}
#location ~ ^/business/* {
# # expires 10m;
# proxy_pass http://172.16.68.132:8088;
# #proxy_pass http://172.16.68.134:8082;
#}
location ~ ^/rsmanager/* {
expires 10m;
root /data/web/;
#proxy_pass http://rsm;
}
#定义nginx处理的页面后缀
location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$ {
root /data/web/html/;
#页面缓存时间为10分钟
expires 10m;
}
#设定查看Nginx状态的地址
location ~* ^/NginxStatus/ {
stub_status on;
access_log off;
allow 10.1.252.126;
allow 10.248.6.49;
allow 127.0.0.1;
deny all;
}
# error_page 405 =200 @405;
# location @405
# {
# proxy_pass http://10.248.6.45:8080;
# }
access_log /data/logs/nginx/access.log combined;
error_log /data/logs/nginx/error.log;
}
server {
listen 8082;
server_name _;
location = /check.html {
root /usr/local/nginx/html/;
access_log off;
}
}
server {
listen 8088;
server_name _;
location ~ ^/* {
root /data/web/b2bhtml/;
access_log off;
}
}
server {
listen 9082;
server_name _;
# location ~ ^/resource/* {
# expires 10m;
# root /data/web/html/;
# }
location / {
root /data/web/html/sysMaintain/;
if (!-f $request_filename) {
rewrite ^/(.*)$ /sysMaintain.html last;
}
}
}
}
1、系统要安装python,并安装与之对应的setuptools,下载地址在此
2、安装:
# sh setuptoolsxxxx.egg
3、安装supervisor,下载地址在此,解压缩后
# python setup.py install
这就ok了,然后执行
# echo_supervisord_conf > /etc/supervisord.conf
修改/etc/supervisord.conf文件,加入你要监控的进程,里面的注释很详细,举个简单的例子:
这是一段要监控的进程的描述信息,添加到这个文件的末尾就好了:
[program:meta.txn.recover.on.error]
command=/cas/bin/meta.txn.recover.on.error ; 被监控的进程路径
numprocs=1 ; 启动几个进程
directory=/cas/bin ; 执行前要不要先cd到目录去,一般不用
autostart=true ; 随着supervisord的启动而启动
autorestart=true ; 自动重启。。当然要选上了
startretries=10 ; 启动失败时的最多重试次数
exitcodes=0 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)
stopsignal=KILL ; 用来杀死进程的信号
stopwaitsecs=10 ; 发送SIGKILL前的等待时间
redirect_stderr=true ; 重定向stderr到stdout
为了节省空间,注释的内容就不贴出来了。
执行
# supervisord -n
能在控制台看到监控进程的输出:
2010-08-17 10:26:07,467 INFO supervisord started with pid 943
2010-08-17 10:26:08,469 INFO spawned: 'meta.txn.recover.on.error' with pid 1009
2010-08-17 10:26:09,876 INFO success: meta.txn.recover.on.error entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
2010-08-17 10:26:48,442 INFO exited: meta.txn.recover.on.error (terminated by SIGKILL; not expected)
2010-08-17 10:26:49,444 INFO spawned: 'meta.txn.recover.on.error' with pid 2427
2010-08-17 10:26:50,487 INFO success: meta.txn.recover.on.error entered RUNNING state, process has stayed up for > than 1 seconds (startsecs)
黑体的地方是我用kill -9杀掉进程后出来的,看到supervisor检测到进程退出后又再次启动了进程。
不带参数运行supervisord是以daemon方式运行。
把supervisord加入到开机启动项里就可以完成监控进程的功能了。
【注意】:当supervisord以非daemon方式运行时,杀掉supervisord后,被监控的进程也退出了。
而以daemon方式运行,杀掉supervisord对被监控进程无影响。
经常会碰到要写一些守护进程,简单做法放入后台:
shell> nohup python xxx.py &
偶尔这么做还可以接受,如果一堆这样的呢?
当然还有一个问题,就是各种服务,对应的命令或者路径都不太一致,比如Apache、MySQL或者其他自行编译的工具。
如果可以统一管理这些应用,是不是很哈皮?
按照惯例现Google一把,不失所望找到一个神奇的利器。supervisor!
supervisor地址:http://supervisord.org,官方标语就是:一个进程管理工具。
安装:
shell> sudo aptitude install supervisor # pip/easy_install
也可以通过其他包管理来安装,比如apt/yum等。
安装好以后,有两个可执行文件和一个配置文件(平台差异,可能路径不一致):
/usr/bin/supervisord -- supervisor服务守护进程 /usr/bin/supervisorctl -- supervisor服务控制程序,比如:status/start/stop/restart xx 等 /etc/supervisor/supervisord.conf -- 配置文件,定义服务名称以及接口等等
下面来一个示例,用web.py写一个hello的程序:
import web urls = ( '/(.*)','hello' ) app = web.application(urls, globals()) class hello: def GET(self, name): return 'hello: ' + name if __name__ == '__main__': app.run()
这个时候可以直接启动这个程序了,下面来配置supervisor,加入管理。修改supervisord.conf,加入如下片段:
[program:hello] command=python /home/smallfish/hello.py autorstart=true stdout_logfile=/home/smallfish/hello.log
上面的意思应该很容易懂,program后面跟服务的名称,command是程序的执行路径,autorstart是表示自动启动,stdout_logfile是捕获标准输出。
到这里,基本搞定了,下面就是启动管理:
shell> sudo /etc/init.d/supervisor start -- 启动supervisor服务 shell> sudo supervisorctl status hello -- 获取hello服务的状态,因为是autorstart,这里已经启动了 hello RUNNING pid 1159, uptime :20:32 shell> sudo supervisorctl stop hello -- 停止hello服务 hello: stopped shell> sudo supervisorctl stop hello -- 再次停止hello,会有错误信息 hello: ERROR (not running) shell> sudo supervisorctl start hello -- 启动hello服务 hello: started
OK,基本的操作就是类似这个了,仔细看supervisord.conf文件里会发现有一段[unix_http_server]的配置,默认是9001端口,可以输入用户名和密码,主要用于Basic Auth认证用的。
填写一下,然后重启supervisor服务,打开浏览器输入:http://localhost:9001,如图:
一般这种情况mail的内容就只是一些正常的系统信息或者是比较重要的错误报告。如果你安装了mutt的话直接用这个命令就可以查看mail的内容(用root登陆先),没有装的话用cat /var/spool/mail/root查看(用root登陆先)。
有时在进入系统的时候经常提示You have new mail in /var/spool/mail/root
你觉得烦人---解决方法:修改系统配置文件/etc/profile,告诉系统不要去检查邮箱.
具体操作:
命令行输入:echo "unset MAILCHECK" >> /etc/profile 【把unset MAILCHECK加到文件/etc/profile 的尾部】然后重新登陆控制台就没有这个讨厌的提示了。
你觉着不烦人---你想把这封信邮件发到自己的邮箱看一下---解决方法:修改/usr/share/logwatch/default.conf/logwatch.conf配置文件
具体操作:
命令行输入:vim /usr/share/logwatch/default.conf/logwatch.conf
MailTo更改为想要输送的邮件人地地址就可以了
收到那一封信,内容分为:
Named(一些不可链接的对象)
pam_unix(一些验证对象,比如sshd)
sendmail
sendmail - largeboxes(大型邮件spool文件)
SSHD(sshd的次数,一下描述信息)
磁盘空间(挂载,空间使用情况)等信息
下面我们一起来看我总结了关于Centos系统关闭You have new mail in /var/spool/mail/root提示的方法,希望文章对各位会有帮助。
昨天搬到阿里云了。
装的系统是Centos 6.3的加固版
今天查看内存的时候 出现一天奇怪的提示
You have new mail in /var/spool/mail/root
有的时候每敲一下回车,就出来You have new mail in /var/spool/mail/root的提示,究竟是为什么呢?
Linux 系统经常会自动发出一些邮件来提醒用户系统中出了哪些问题(收件箱位置:/var/mail/)。可是这些邮件都是发送给 root 用户的。出于系统安全考虑,通常不建议大家直接使用 root 帐户进行日常操作。所以要想点办法来让系统把发给 root 用户的邮件也给自己指定的外部邮箱发一份,或者是直接关闭此项服务。
1、关闭sendmail服务,这里介绍一种不用关闭sendmail服务的方法
代码如下 复制代码
echo “unset MAILCHECK” >> /etc/profilesource /etc/profile
关闭sendmail的功能:
代码如下
chmod 0 /usr/sbin/sendmailmv /usr/sbin/sendmail /usr/sbin/sendmail.bakln -s /var/qmail/bin/sendmail /usr/sbin/sendmail
清空 /var/spool/mail/root日志
代码如下
cat /dev/null > /var/spool/mail/rootcat /dev/null>;/var/spool/mail/root
或者转发到自己的邮箱,下面介绍下怎么转发到自己的邮箱(此方法未经本人亲自验证 来源于网络,有喜欢折腾的请自己研究,成功了 可以跟帖分享经验)
2、root邮件转发到自己的邮箱
方法一:
修改此文件
代码如下
/etc/log.d/logwatch.conf
添加MailTo = root,xxx@xxx.com
方法二
代码如下
/etc/aliases
添加root: xxx@xxx.com
注意:好像如果设置成和主机同域的,好像邮件就发不成,比如本机邮件就是moper.me,那么发这个就没法发,相应的发其他邮箱就可以成功。
关于“/etc/aliases”:
当sendmail收到一个要送给xxx的信时,它会依据/etc/aliases文件中的内容送给另一个使用者。这个功能可以创造一个只有在信件 系统内才有效的使用者。例如mailing list就会用到这个功能,在 mailing list 中,我们可能会创造一个叫 redlinux@link.ece.uci.edu的 mailinglist,但实际上并没有一个叫redlinux的使用者。实际 aliases档的内容是将送给这个使用者的信都收给mailing list处理程式负责分送的工作。
/etc/aliases是一个文本文档,而sendmail需要一个二进位格式的 /etc/aliases.db。newaliases的功能传是将/etc/aliases转换成一个sendmail所能了解的db文件:
代码如下
[root@centos ~]# newaliases
除root外的其它用的邮件可以通过在用户/home/下建立一个.forward文件实现转发:
代码如下
//somebody
other1
other2
文件权限设为600,作用一样,但.forward可以由用户自行维护,而aliases则只有治理员才能修改。
设定~/.forward档案加入转寄目的即可
网上很多教程是你抄我,我抄你,根本就没有验证过的,比如有种方法是修改”/usr/share/logwatch/default.conf/logwatch.conf“配置文件,在centos6中根本就没有这个文件,至于以前的版本有没有就不知道了。
还有很多教程,只有“echo "unset MAILCHECK" >> /etc/profile”,而没有“source /etc/profile”,这也是不对的。
列一下22端口占用的程序
[codes=c#]
[root@leiwan tmp]# netstat -tunlp |grep 22
tcp 0 0 0.0.0.0:42957 0.0.0.0:* LISTEN 2230/rpc.statd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2443/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2292/cupsd
tcp 0 0 :::22 :::* LISTEN 2443/sshd
tcp 0 0 ::1:631 :::* LISTEN 2292/cupsd
tcp 0 0 :::57609 :::* LISTEN 2230/rpc.statd
udp 0 0 0.0.0.0:5353 0.0.0.0:* 2211/avahi-daemon
udp 0 0 0.0.0.0:631 0.0.0.0:* 2292/cupsd
udp 0 0 0.0.0.0:37167 0.0.0.0:* 2230/rpc.statd
udp 0 0 0.0.0.0:52291 0.0.0.0:* 2211/avahi-daemon
udp 0 0 0.0.0.0:68 0.0.0.0:* 2207/dhclient
udp 0 0 0.0.0.0:710 0.0.0.0:* 2230/rpc.statd
udp 0 0 :::39834 :::* 2230/rpc.statd
[/codes]
查看某一端口的占用情况: lsof -i:端口号
[root@www ~]# lsof -i:21
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
pure-ftpd 2651 root 4u IPv4 7047 TCP *:ftp (LISTEN)
pure-ftpd 2651 root 5u IPv6 7048 TCP *:ftp (LISTEN)
这里显示出21号端口正在被pure-ftpd使用,状态是listen。
netstat -anp 显示系统端口使用情况
淘宝 NPM 镜像
这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10分钟 一次以保证尽量与官方服务同步。
- 当前 registry.npm.taobao.org 是从 registry.npmjs.org 进行全量同步的.
- 当前 npm.taobao.org 运行版本是: cnpmjs.org@2.0.0-rc.15
- 本系统运行在 io.js@v1.4.3 上.
- 开源镜像: http://npm.taobao.org/mirrors
- iojs.org/dist 镜像: http://npm.taobao.org/mirrors/iojs
- nodejs.org/dist 镜像: http://npm.taobao.org/mirrors/node
- phantomjs 镜像: http://npm.taobao.org/mirrors/phantomjs
- ChromeDriver 镜像: http://npm.taobao.org/mirrors/chromedriver
- Node.js 文档镜像: http://npm.taobao.org/mirrors/node/latest/docs/api/index.html
- io.js 文档镜像: http://npm.taobao.org/mirrors/iojs/latest/doc/api/index.html
使用说明
你可以使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
或者你直接通过添加 npm 参数 alias 一个新命令:
alias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc" # Or alias it in .bashrc or .zshrc $ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc
安装模块
从 registry.npm.taobao.org 安装所有模块. 当安装的时候发现安装的模块还没有同步过来, 淘宝 NPM 会自动在后台进行同步, 并且会让你从官方 NPMregistry.npmjs.org 进行安装. 下次你再安装这个模块的时候, 就会直接从 淘宝 NPM 安装了.
$ cnpm install [name]
同步模块
直接通过 sync 命令马上同步一个模块, 只有 cnpm 命令行才有此功能:
$ cnpm sync connect
当然, 你可以直接通过 web 方式来同步: /sync/connect
$ open https://npm.taobao.org/sync/connect
其它命令
支持 npm 除了 publish 之外的所有命令, 如:
$ cnpm info connect
1.用途说明
free命令用来显示内存使用情况。display information about free and used memory on the system。
free 命令相对于top 提供了更简洁的查看系统内存使用情况:
其中的相关说明:
Mem:表示物理内存统计
-/+ buffers/cached:表示物理内存的缓存统计
Swap:表示硬盘上交换分区的使用情况(这里我们不去关心)
系统的总物理内存:255268Kb(256M),但系统当前真正可用的内存并不是第一行free 标记的 16936Kb,它仅代表未被分配的内存。
我们使用total1、used1、free1、used2、free2 等名称来代表上面统计数据的各值,1、2 分别代表第一行和第二行的数据。
total1: 表示物理内存总量。
used1: 表示总计分配给缓存(包含buffers 与cache )使用的数量,但其中可能部分缓存并未实际使用。
free1: 未被分配的内存。
shared1: 共享内存,一般系统不会用到,这里也不讨论。
buffers1: 系统分配但未被使用的buffers 数量。
cached1: 系统分配但未被使用的cache 数量。buffer 与cache 的区别见后面。
used2: 实际使用的buffers 与cache 总量,也是实际使用的内存总量。
free2: 未被使用的buffers 与cache 和未被分配的内存之和,这就是系统当前实际可用内存。
2.常用参数
b 以字节为单位显示。
-k 以K字节为单位显示。默认显示。
-m 以兆字节为单位显示。
-s 每隔多少秒重复执行。可编写一个脚本用于监控。
3.数值的单位是m。
Mem开头的行:total是指内存总数。比如这台机器4G内存,实际上已经扣除掉了作为显存的部分。
used是指操作系统已经使用的内存数。这部分既包括操作系统本身使用的部分,也包括应用程序已经使用的部分,还包括缓存的部分。
free是指操作系统还没有使用的内存数。我们通常看到这部分比较小。
shared已经废弃不用,总是显示0。
buffers和cached是指作为缓存的内存数。
-/+ buffers/cache开头的行:(重点看这行)
used列是指应用程序总共使用的内存数。等于Mem.used-Mem.buffers-Mem.cached。
free列是指应用程序还未使用的内存数。这个数据才是我们需要关注的空闲可用内存数。等于Mem.free+Mem.buffers+Mem.cached。如果此行中free列的数据太小,那么就需要优化程序或者增加物理内存了。