shellps命令以及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/f
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
grep命令查看进程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命令详解,请参考
⽤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选项表⽰为把每个进程的整个命令⾏全部显⽰完,⽽不是由于命令⾏过长就把它从屏幕上截去。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论