linux 查看进程的运行时间

有些时候我们需要知道某个进程的启动时间! 

显示某PID的启动时间命令: 

ps -p PID -o lstart

举例子

如下面显示httpd的启动时间: 

  1. for pid in $(pgrep httpd); do echo -n "${pid}"; ps -p ${pid} -o lstart | grep -v "START"; done  
写个小脚本,根据传入的参数显示对应进程的启动时间 
比如运行:./show.sh  mysql 显示mysql进程的启动时间: 

Bash代码  
  1. #!/bin/bash                                                                                                                                                                                          
  2. if [ -z $1  ]; then  
  3.     echo "please input ps keyword" && exit 1  
  4. fi  
  5. ps aux |grep $1 |grep -v "grep $1" |grep -v " $1" |while read line  
  6. do  
  7.     linewords=($line)  
  8.     pid="${linewords[1]}"  
  9.     START_TIME=$(ps -p ${pid} -o lstart | grep -v "START")  
  10.     echo "$line $START_TIME"  
  11. done  

你可能感兴趣的:(操作系统linux)