如何查看 Linux 系統(tǒng)的運行時間?1. 使用 uptime 命令可直接查看系統(tǒng)已運行時間,輸出如“up 3 days, 5:12”表示運行了 3 天 5 小時 12 分鐘;2. 使用 top 或 htop 工具,在動態(tài)界面的第一行或頂部狀態(tài)欄中查看運行時間信息;3. 查看 /proc/uptime 文件,第一個數字表示系統(tǒng)運行的總秒數,可結合腳本轉換為天、小時、分鐘格式;4. 在腳本或程序中通過 awk 提取并格式化輸出運行時間,實現更人性化的展示。這些方法適用于不同場景,可根據需求選擇使用。
想知道你的 Linux 系統(tǒng)已經運行了多久?其實查看系統(tǒng) uptime(運行時間)并不難,而且方法還挺多的。最簡單的就是打開終端,輸入命令就能看到結果。

用 uptime
命令快速查看
這是最直接的方法。在終端里輸入:
uptime
你會看到類似這樣的輸出:

14:25:36 up 3 days, 5:12, 2 users, load average: 0.12, 0.08, 0.05
up 3 days, 5:12
就表示系統(tǒng)已經運行了 3 天又 5 小時 12 分鐘。- 后面還顯示了當前登錄用戶數和系統(tǒng)的平均負載。
這個命令適合快速檢查,不需要額外參數,簡單好記。
使用 top
或 htop
查看
如果你習慣用動態(tài)監(jiān)控工具,top
或更友好的 htop
也能看到 uptime。

運行:
top
在第一行就能看到 uptime 的信息,格式跟 uptime
命令基本一致。
如果用了 htop
,它會把這部分放在頂部狀態(tài)欄里,看起來更直觀。
從 /proc/uptime
文件讀取
想了解底層怎么實現的?Linux 把系統(tǒng)運行時間記錄在 /proc/uptime
文件中,你可以用 cat
來看:
cat /proc/uptime
輸出是兩個數字,比如:
289324.12 287563.45
第一個數字代表系統(tǒng)總共運行了多少秒(這里是大約 3 天多),第二個是所有 CPU 總共空閑的時間。
你也可以寫個簡單的腳本把它轉換成可讀性更強的格式。
在腳本或程序中使用
如果你想在 shell 腳本或者別的程序里處理 uptime 數據,可以用下面這種方式來提取運行時間秒數:
awk '{sec=$1; print sec}' /proc/uptime
再配合一些計算,就能轉成天、小時、分鐘的形式。例如:
awk '{t=$1; printf "系統(tǒng)已運行:%d 天 %02d:%02d:%02d\n", t/86400, (t%86400)/3600, (t%3600)/60, t%60}' /proc/uptime
這樣輸出就更人性化了。
基本上就這些方法。不復雜但容易忽略的是,不同命令展示的信息細節(jié)略有差別,選對場景用就行。
The above is the detailed content of how to check system uptime linux. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

If you find that the server is running slowly or the memory usage is too high, you should check the cause before operating. First, you need to check the system resource usage, use top, htop, free-h, iostat, ss-antp and other commands to check CPU, memory, disk I/O and network connections; secondly, analyze specific process problems, and track the behavior of high-occupancy processes through tools such as ps, jstack, strace; then check logs and monitoring data, view OOM records, exception requests, slow queries and other clues; finally, targeted processing is carried out based on common reasons such as memory leaks, connection pool exhaustion, cache failure storms, and timing task conflicts, optimize code logic, set up a timeout retry mechanism, add current limit fuses, and regularly pressure measurement and evaluation resources.

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

In this article, we will learn how to install, update, remove, find packages, manage packages and repositories on Linux systems using YUM (Yellowdog Updater Modified) tool developed by RedHat. The example commands shown in this article are practicall

Written in C, MySQL is an open-source, cross-platform, and one of the most widely used Relational Database Management Systems (RDMS). It’s an integral part of the LAMP stack and is a popular database management system in web hosting, data analytics,
