AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 99428
Accepted
Robert
Robert
Asked: 2015-05-03 08:48:14 +0800 CST2015-05-03 08:48:14 +0800 CST 2015-05-03 08:48:14 +0800 CST

如何获得服务器的正常运行时间?

  • 772

我正在使用 postgresql-9.4 并且为了支持和监控任务,我需要知道我的服务器已经运行了多长时间?有什么功能或方法可以知道这一点吗?有没有一种 sql 方法可以知道我的服务器何时启动?

我真的需要知道它开始以来经过的时间吗?

postgresql postgresql-9.4
  • 2 2 个回答
  • 6852 Views

2 个回答

  • Voted
  1. Best Answer
    a_horse_with_no_name
    2015-05-03T10:11:27+08:002015-05-03T10:11:27+08:00

    使用内置函数pg_postmaster_start_time

    select current_timestamp - pg_postmaster_start_time() as uptime
    

    手册中的更多详细信息:http ://www.postgresql.org/docs/current/static/functions-info.html

    如果你想要几秒钟,请使用extract:

    select extract(epoch from current_timestamp - pg_postmaster_start_time()) as uptime
    
    • 13
  2. Vérace
    2015-05-03T09:20:58+08:002015-05-03T09:20:58+08:00

    感谢线程1、2和3。

    以下示例取自运行 postgresql 的实例。

    方法一:(提供开始时间到秒等详细信息)

    `ls -l ./data/postmaster.pid` 
    

    postmaster.pid 文件是在您启动服务器时创建的。

    样品输出:

    [pol@localhost inst]$ ls -l ./data/postmaster.pid
    -rw-------. 1 pol pol 106 May  4 20:53 ./data/postmaster.pid
    [pol@localhost inst]$
    

    方法2:(繁琐,为了完整性而包含在内)

    如果你是一个真正的 bash 大师,你可以使用这样的东西

    - 步骤1

    首先,获取“root”postgres 进程的 pid - 在本例中为 7382。

    [pol@localhost inst]$ `ps -ef | grep post`
    

    样品输出:

    [pol@localhost inst]$ ps -ef | grep post
    pol       7382     1  0 20:53 pts/2    00:00:00 /home/pol/Downloads/software/postgres/inst/bin/postgres -D ./data
    pol       7384  7382  0 20:53 ?        00:00:00 postgres: checkpointer process   
    pol       7385  7382  0 20:53 ?        00:00:00 postgres: writer process   
    pol       7386  7382  0 20:53 ?        00:00:00 postgres: wal writer process   
    pol       7387  7382  0 20:53 ?        00:00:00 postgres: autovacuum launcher process   
    pol       7388  7382  0 20:53 ?        00:00:00 postgres: stats collector process   
    pol       9741  5477  0 23:49 pts/1    00:00:00 grep --color=auto post
    [pol@localhost inst]$ 
    

    - 步骤 2(a)

    然后运行以下命令

    [pol@localhost inst]$ `ps -p 7382 -o lstart`
                     STARTED
    Fri May  1 18:03:44 2015
    [pol@localhost inst]$ 
    

    - 步骤 2(b)

    或(也许更适合脚本)

    [pol@localhost inst]$ `ps -p 7382 -o lstart=`
    Fri May  1 18:03:44 2015
    [pol@localhost inst]$ 
    

    当使用各种 bash 实用程序将输出传递到脚本链时,末尾的 = 会抑制可能会混淆水域的标题。

    方法三:

    如果你想要时间到纳秒!我怀疑最后 3-4 位数字的准确性(很多有趣的谷歌搜索)

    [pol@localhost inst]$ `stat /proc/7382`
      File: /proc/7382
      Size: 0           Blocks: 0          IO Block: 1024   directory
    Device: 3h/3d   Inode: 154004      Links: 9
    Access: (0555/dr-xr-xr-x)  Uid: ( 1000/     pol)   Gid: ( 1000/     pol)
    Context: unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
    Access: 2015-05-01 18:03:55.473814036 +0100
    Modify: 2015-05-01 18:03:55.473814036 +0100
    Change: 2015-05-01 18:03:55.473814036 +0100
     Birth: -
    [pol@localhost inst]$ 
    

    方法四:

    显然,所有这些方法都可以转换为可执行文件,但我认为接下来的两个特别优雅。

    此脚本输出 pid 的名称和编号。它已经运行的秒数。脚本的输入 ($1) 是 pid。

    #file: sincetime
    #!/bin/bash
    init=`stat -t /proc/$1 | awk '{print $14}'`
    curr=`date +%s`
    seconds=`echo $curr - $init| bc`
    name=`cat /proc/$1/cmdline`
    echo $name $seconds
    

    要使其可执行,请将上述内容另存为 sincetime,然后发出命令chmod 755 sincetime并在任何地方使用它,请将其放在您的路径上。

    方法五:

    此方法基于方法 4 - greptime,它调用 sincetime 并输出与模式匹配的每个进程的秒数。

    #file: greptime
    #!/bin/bash
    pidlist=`ps ax | grep -i -E $1 | grep -v grep | awk '{print $1}' | grep -v PID | xargs echo`
    for pid in $pidlist; do
        sincetime $pid
    done
    

    方法六:

    这将返回进程启动的当前纪元中的秒数 - 对于支持 UNIX 时间数据类型的 MySQL 等数据库很有用。

    stat -t /proc/7382 | awk '{print $14}'
    

    此变体将以秒为单位返回时间

    echo `date +%s` - `stat -t /proc/7382 | awk '{print $14}'` | bc
    

    方法七:

    这些命令提供了使用 ps、ls 和 awk 命令可以完成的操作的示例。有关各种命令,请参见各自的手册页。

    ps -eo pid,cmd,etime,lstart  | grep <pid> | grep -v grep
    
    ps -p <pid> -wo pid,lstart,cmd
    
    ps -eo pid,etime,cmd | grep <pid | pname> |sort -n -k2
    
    ps -p <pid> -o etime=
    
    ps -eo pid,etime | awk '{print $1, $2}' | grep 7382 | grep -v grep
    

    样本输出:

    [pol@localhost inst]$     ps -eo pid,cmd,etime,lstart  | grep 7382 | grep -v grep
     7382 /home/pol/Downloads/softwar    03:00:16 Mon May  4 20:53:24 2015
    
    [pol@localhost inst]$ ps -p 7382 -wo pid,lstart,cmd
      PID                  STARTED CMD
     7382 Mon May  4 20:53:24 2015 /home/pol/Downloads/software/postgres/inst/bin/postgres -D ./data
    
    [pol@localhost inst]$  ps -eo pid,etime,cmd | grep 7382 |sort -n -k2 | grep -v grep
     7382    03:01:29 /home/pol/Downloads/software/postgres/inst/bin/postgres -D ./data
    
    [pol@localhost inst]$  ps -eo pid,etime,cmd | grep post |sort -n -k2 | grep -v grep
     7382    03:01:42 /home/pol/Downloads/software/postgres/inst/bin/postgres -D ./data
     7384    03:01:41 postgres: checkpointer process   
     7385    03:01:41 postgres: writer process   
     7386    03:01:41 postgres: wal writer process   
     7387    03:01:41 postgres: autovacuum launcher process   
     7388    03:01:41 postgres: stats collector process   
    [pol@localhost inst]$ 
    
    [pol@localhost inst]$ ps -eo pid,etime | awk '{print $1, $2}' | grep 7382 | grep -v grep
    7382 03:12:49
    [pol@localhost inst]$
    

    /proc 虚拟文件系统也非常有用。

    ls -ld /proc/7382 | awk '{print $8}'
    

    输出

    20:53

    例子:

    ls -ltrh /proc | grep 7382 | awk '{print "Time started: " $8}'
    

    输出:

    开始时间:20:53

    • 2

相关问题

  • 我可以在使用数据库后激活 PITR 吗?

  • 运行时间偏移延迟复制的最佳实践

  • 存储过程可以防止 SQL 注入吗?

  • PostgreSQL 中 UniProt 的生物序列

  • PostgreSQL 9.0 Replication 和 Slony-I 有什么区别?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve