shell ps命令 以及 ps命令 进程时间 格式转换

示例

ps -eo pid,etime,cmd | grep "nginx: "|grep -v grep

etime      ELAPSED  elapsed time since the process was started, in the form [[dd-]hh:]mm:ss.

进程运行的总时间

例如

[root@localhost ~]# ps -eo pid,etime,cmd | grep "nginx: "|grep -v grep 
20344    04:25:30 nginx: worker process                                         
 20345    04:25:30 nginx: worker process                                      
 63441  4-21:45:32 nginx: worker process is shutting down                                      
 63443  4-21:45:32 nginx: worker process is shutting down                                        
 66656 32-23:25:22 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf                                                         
114770    01:15:31 nginx: cache manager process  

4-21:45:32 的意思是进程运行了4天21个小时,45分钟,32秒

32-23:25:22 的意思是进程运行了32天23小时,24分钟,22秒

ps -eo lstart,pid,cmd |grep nginx |grep -v grep

ps 的lstart可以显示进程的启动时间,但是时间的格式不是很人性,不利于脚本中的时间比较,

但是可以把ps 命令中的结果转换成ISO时间格式或者 任意格式

[root@lab01 ~]# ps -eo lstart,pid,cmd |grep nginx|grep -v grep
Fri Jun 12 18:25:09 2020 17485 nginx: worker process
Fri Jun 12 18:25:09 2020 17486 nginx: cache manager process
Wed May 27 00:14:49 2020 27921 nginx: master process /usr/local/openresty/nginx/sbin/nginx
[root@lab01 ~]# cat /var/run/nginx.pid
27921
[root@lab01 ~]# date -d "`ps -eo lstart,pid,cmd |grep 27921|grep -v grep|awk '{print $1,$2,$3,$4,$5}'`" "+%Y-%m-%d"
2020-05-27
[root@lab01 ~]# date -d "`ps -eo lstart,pid,cmd |grep 27921|grep -v grep|awk '{print $1,$2,$3,$4,$5}'`" "+%Y-%m-%d:%H:%M:%S"
2020-05-27:00:14:49
[root@lab01 ~]# date -d "`ps -eo lstart,pid,cmd |grep 27921|grep -v grep|awk '{print $1,$2,$3,$4,$5}'`" "+%s"
1590509689
[root@lab01 ~]# 

主要是使用date命令,date命令详解,请参考https://www.cnblogs.com/faberbeta/p/linux-shell002.html

用shell将日期时间与时间戳互转:

date -d "Wed May 27 00:14:49 2020" +%s         输出 1590509689

date -d "Wed May 27 00:14:49 2020" "+%Y-%m-%d:%H:%M:%S"  输出  2020-05-27:00:14:49

date -d "Wed May 27 00:14:49 2020" "+%Y-%m-%d"  输出  2020-05-27

 

时间戳转换为字符串可以这样做:

      date -d @1438617600  "+%Y-%m-%d:%H:%M:%S"    输出:2015-08-04:00:00:00

 ps auxfww

auxww。 a选项显示出所有运行进程的内容, 而不仅仅是您的进程。 u选项显示出进程所归属的用户名字以及内存使用, x 选项显示出后台进程。 而 ww 选项表示为 ps(1) 把每个进程的整个命令行全部显示完, 而不是由于命令行过长就把它从屏幕上截去。

原文地址:https://www.cnblogs.com/faberbeta/p/13152111.html