---
title: "手边常用的 linux命令及shell脚本"
author: "fishermen"
date: "2009-08-04T22:28:22Z"
tags: [".net", "C", "Linux", "脚本", "虚拟机"]
slug: "linux-shell"
draft: false
lastmod: "2009-08-04T22:28:52Z"
id: 440256
url: "https://fishermen.iteye.com/blog/440256"
---

```
1 netstat -nltp 开放的端口

2 netstat -an 从源到其他的ip及端口的数字化显示

3 增加路由：
  Root执行:
 /sbin/route add -net 10.10.10.0/24  gw 172.16.65.1

 第一个参数10.10.10.0 是目标机房的网段
 第二个参数 172.16.65.1 是当前机房内网的网关。

 注意双方机器都要执行。

4 tcpdump -XX -s 2048 port 5262 监控tcp收报的具体内容，tcpdump也可用于监听udp的

5 清理swap dmesg | grep swap
  swapoff /dev/sda6

6 查找出某个线程并kill

   ps -efww|grep LOCAL=NO|grep -v grep|cut -c 9-15|xargs kill -9

7 在虚拟机配置虚拟ip，service network restart

8  程序自动获取ip时，需要指定hostname，否则自己寻找到127.0.0.1.
   配置方法： (1) 在 /etc/host   192.168.1.31  IM1
              (2) hostname IM1
   这样就设定本机hostname为IM1，系统获取ip地址时就可获得指定的ip了。

9 nohup:启动命令后，不会被挂断（用户离开，经常容易导致其启动的命令被挂断），打印日志到nohup.log中

10 使用 linux 命令 sort filename | uniq (排序、唯一之后结果)
```

 

 

 

 

 

 

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\** 脚本 \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

1 执行任务，把标准输出及exception都记录到nohup中，且在重启时把nohup重命名

  

```
#!/bin/sh

     date=`date +%y.%m.%d-%H-%M`

     mv nohup.out nohup.out.20$date

     nohup ./run.sh >> nohup.out 2>&1 &

     tail -f nohup.out

```