示例
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) 把每個進程的整個命令行全部顯示完, 而不是由於命令行過長就把它從屏幕上截去。