Useful Commands/Service Management: Difference between revisions

From Fundamental Ramen
Jump to navigation Jump to search
(Created page with "{| class="wikitable" ! 情境 || 指令 |- | 取得本地時間 || <source lang="bash">date</source> |- | 取得 UTC 時間 || <source lang="bash">date -u</source> |- | 取...")
 
No edit summary
Line 1: Line 1:
== CentOS 7.x / Ubuntu 16.04 ==
{| class="wikitable"
{| class="wikitable"
! 情境 || 指令
! 情境 || 指令
|-
|-
| 取得本地時間 || <source lang="bash">date</source>
| 列舉系統服務 ||
<source lang="bash">
systemctl -t service            # 省略式
systemctl list-units -t service # 完整式
</source>
|-
|-
| 取得 UTC 時間 || <source lang="bash">date -u</source>
| 服務操作 ||
<source lang="bash">
systemctl status nginx
systemctl start  nginx
systemctl stop  nginx
systemctl reload nginx
</source>
|}
 
== Linux 傳統 ==
{| class="wikitable"
! 情境 || 指令
|-
|-
| 取得 Timestamp || <source lang="bash">date +'%s'</source>
| 管理系統服務 || <source lang="bash">sudo service start/stop/reload/restart {服務名稱}</source>
|-
|-
| 產生 ISO 8601 時間字串 || <source lang="bash">date -u +'%FT%TZ'</source>
| 顯示服務狀態 || <source lang="bash">service --status-all</source>
|-
|-
| 顯示目前的時區 || <source lang="bash">date +'%Z'</source>
| 顯示服務 (啟動/關閉) 時機 (runlevel 0~6) || <source lang="bash">chkconfig --list</source>
|-
|-
| 以日期命名備份檔 || <source lang="bash">BKFILE=`date +'wiki-%Y%m%d.sqlite.gz'`</source>
| 設定服務在開機後自動啟動 || <source lang="bash">sudo chkconfig --level 2345 {服務名稱} on</source>
|}
 
== Run Level 說明 ==
{| class="wikitable"
! runlevel || 英文說明 || 中文說明
|-
|-
| 取得上週的日期 || <source lang="bash">date -r $((`date +%s`-86400*7))</source>
| 0 || halt || 系統關機
|-
|-
| crontab 裡面使用 date || <source lang="bash">date +"\%F \%T"</source>(注意 % 字元之前需要使用 \ 字元 escape)
| 1 || single user mode || 單人模式,用在系統出問題時的維護
|-
|-
| 取得檔案的 mtime 並且輸出成日期格式 || <source lang="bash">stat -f '%Sm' -t '%Y%m%d' test.sql</source>
| 2 || Multi-user, without NFS || 類似 runlevel 3,但無 NFS 服務
|-
|-
| 取得檔案的 mtime 並且輸出成 ISO 8601 格式 || <source lang="bash">stat -f '%Sm' -t '%F %T' test.sql</source>
| 3 || Full multi-user mode || 完整含有網路功能的純文字模式
|-
|-
| 取得檔案的 mtime 並且輸出 Timestamp ||
| 4 || unused || 保留
<source lang="bash">
stat -f '%Dm' test.sql
stat -c '%Y' test.sql
stat -c '%Y %n' *.gz
</source>
|-
|-
| 與時間伺服器對時(台灣) ||
| 5 || X11 || 類似 runlevel 3,有使用 X Window
<source lang="bash">
sudo ntpdate time.stdtime.gov.tw
sudo ntpdate clock.stdtime.gov.tw
</source>
|-
|-
| 變更系統時區 ||
| 6 || reboot || 重新開機
<source lang="bash">
sudo dpkg-reconfigure tzdata
sudo service cron restart    # 做完記得要重開 cron 服務,否則排程時間會亂掉
</source>
|}
|}

Revision as of 03:56, 14 August 2018

CentOS 7.x / Ubuntu 16.04

情境 指令
列舉系統服務
systemctl -t service            # 省略式
systemctl list-units -t service # 完整式
服務操作
systemctl status nginx
systemctl start  nginx
systemctl stop   nginx
systemctl reload nginx

Linux 傳統

情境 指令
管理系統服務
sudo service start/stop/reload/restart {服務名稱}
顯示服務狀態
service --status-all
顯示服務 (啟動/關閉) 時機 (runlevel 0~6)
chkconfig --list
設定服務在開機後自動啟動
sudo chkconfig --level 2345 {服務名稱} on

Run Level 說明

runlevel 英文說明 中文說明
0 halt 系統關機
1 single user mode 單人模式,用在系統出問題時的維護
2 Multi-user, without NFS 類似 runlevel 3,但無 NFS 服務
3 Full multi-user mode 完整含有網路功能的純文字模式
4 unused 保留
5 X11 類似 runlevel 3,有使用 X Window
6 reboot 重新開機