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> |- | 取...")
 
 
(23 intermediate revisions by the same user not shown)
Line 1: Line 1:
== systemctl ==
{| class="wikitable"
{| class="wikitable"
! 情境 || 指令 
! Purpose || Command
|-
|-
| 取得本地時間 || <source lang="bash">date</source>
| Monitor ||
<syntaxhighlight lang="bash">
journalctl -u llama-swap -n 10 -f
 
journalctl -u llama-swap --since "10:20"
journalctl -u llama-swap --since "10 minutes ago"
journalctl -u llama-swap --since today
 
systemctl status llama-swap
</syntaxhighlight>
|-
|-
| 取得 UTC 時間 || <source lang="bash">date -u</source>
| Setup ||
<syntaxhighlight lang="bash">
sudo systemctl disable llama-swap
sudo systemctl daemon-reload
 
sudo ln -s /home/xxx/yyy/llama-swap.service llama.service
sudo systemctl enable llama-swap
sudo systemctl daemon-reload
</syntaxhighlight>
|-
|-
| 取得 Timestamp || <source lang="bash">date +'%s'</source>
| Control ||
<syntaxhighlight lang="bash">
systemctl start  nginx
systemctl stop  nginx
systemctl reload nginx
systemctl reload systemd-networkd
</syntaxhighlight>
|-
|-
| 產生 ISO 8601 時間字串 || <source lang="bash">date -u +'%FT%TZ'</source>
| List ||
|-
<syntaxhighlight lang="bash">
| 顯示目前的時區 || <source lang="bash">date +'%Z'</source>
systemctl -t service
|-
</syntaxhighlight>
| 以日期命名備份檔 || <source lang="bash">BKFILE=`date +'wiki-%Y%m%d.sqlite.gz'`</source>
|}
|-
 
| 取得上週的日期 || <source lang="bash">date -r $((`date +%s`-86400*7))</source>
== Run Level ==
 
{| class="wikitable"
! Target || ! Description
|-
|-
| crontab 裡面使用 date || <source lang="bash">date +"\%F \%T"</source>(注意 % 字元之前需要使用 \ 字元 escape)
| multi-user.target || Multi-user mode (CLI) (Equivalent to Runlevel 3)
|-
|-
| 取得檔案的 mtime 並且輸出成日期格式 || <source lang="bash">stat -f '%Sm' -t '%Y%m%d' test.sql</source>
| graphical.target || Graphical mode (GUI) (Equivalent to Runlevel 5)
|-
|-
| 取得檔案的 mtime 並且輸出成 ISO 8601 格式 || <source lang="bash">stat -f '%Sm' -t '%F %T' test.sql</source>
| rescue.target || Rescue mode (Equivalent to Runlevel 1)
|-
|-
| 取得檔案的 mtime 並且輸出 Timestamp ||
| emergency.target || Emergency mode (Equivalent to Runlevel 0)
<source lang="bash">
stat -f '%Dm' test.sql
stat -c '%Y' test.sql
stat -c '%Y %n' *.gz
</source>
|-
|-
| 與時間伺服器對時(台灣) ||
| reboot.target || Reboot
<source lang="bash">
sudo ntpdate time.stdtime.gov.tw
sudo ntpdate clock.stdtime.gov.tw
</source>
|-
|-
| 變更系統時區 ||
| poweroff.target || Power off
<source lang="bash">
sudo dpkg-reconfigure tzdata
sudo service cron restart    # 做完記得要重開 cron 服務,否則排程時間會亂掉
</source>
|}
|}

Latest revision as of 02:48, 10 September 2026

systemctl

Purpose Command
Monitor
journalctl -u llama-swap -n 10 -f

journalctl -u llama-swap --since "10:20"
journalctl -u llama-swap --since "10 minutes ago"
journalctl -u llama-swap --since today

systemctl status llama-swap
Setup
sudo systemctl disable llama-swap
sudo systemctl daemon-reload

sudo ln -s /home/xxx/yyy/llama-swap.service llama.service
sudo systemctl enable llama-swap
sudo systemctl daemon-reload
Control
systemctl start  nginx
systemctl stop   nginx
systemctl reload nginx
systemctl reload systemd-networkd
List
systemctl -t service

Run Level

Target ! Description
multi-user.target Multi-user mode (CLI) (Equivalent to Runlevel 3)
graphical.target Graphical mode (GUI) (Equivalent to Runlevel 5)
rescue.target Rescue mode (Equivalent to Runlevel 1)
emergency.target Emergency mode (Equivalent to Runlevel 0)
reboot.target Reboot
poweroff.target Power off