1,查看用户下进程
ps -ef | grep $USER
2,查看指定进程
ps -ef | grep xxx.log | grep -v grep
3,查看指定进程数
ps -ef | grep xxx.log | grep -v grep | wc -l
4,查看指定进程号
ps -ef | grep xxx.log | grep -v grep | awk ‘{print $2}’
5,kill掉指定进程号
kill -9 ps -ef | grep xxx.log | grep -v grep | awk '{print $2}'
( ps前 和 最后的 }’ 后 都有一个字符 ` ,这个字符就是键盘数字1左边的符号,不加这个符号会报错,这个建议在代码中使用)
6,shell脚本中循环kill多个指定进程
#!/bin/bash
for i in ${ps -ef | grep xxx_log | grep -v grep | awk '{print $2}' }
do
kill -9 $i
done
7,java代码中执行命令
int pid = 0;
Process process = Runtime.getRuntime().exec(new String[]{“sh”,"-c","kill -9 ps -ef | grep xxx.log | grep -v grep | awk '{print $2}'"});
InputStream in = process.getInputStream();
BufferedReader bs = new BufferedReader(new InputStreamReader(in) );
pid = Integer.parseInt(bs.readLine);
8,shell脚本创建带时间的文件名
#!/bin/bash
#backup log-backup-年月日时分秒
newFileName = "xxx-backup-`date +%Y%m%d%H%d`"
echo $newFileName