#62 Systemd 子命令

2019-12-21
Unit Commands:
  list-units [PATTERN...]             List units currently in memory
  list-sockets [PATTERN...]           List socket units currently in memory,
                                      ordered by address
  list-timers [PATTERN...]            List timer units currently in memory,
                                      ordered by next elapse
  is-active PATTERN...                Check whether units are active
  is-failed PATTERN...                Check whether units are failed
  status [PATTERN...|PID...]          Show runtime status of one or more units
  show [PATTERN...|JOB...]            Show properties of one or more
                                      units/jobs or the manager
  cat PATTERN...                      Show files and drop-ins of specified units
  help PATTERN...|PID...              Show manual for one or more units
  list-dependencies [UNIT...]         Recursively show units which are required
                                      or wanted by the units or by which those
                                      units are required or wanted
  start UNIT...                       Start (activate) one or more units
  stop UNIT...                        Stop (deactivate) one or more units
  reload UNIT...                      Reload one or more units
  restart UNIT...                     Start or restart one or more units
  try-restart UNIT...                 Restart one or more units if active
  reload-or-restart UNIT...           Reload one or more units if possible,
                                      otherwise start or restart
  try-reload-or-restart UNIT...       If active, reload one or more units,
                                      if supported, otherwise restart
  isolate UNIT                        Start one unit and stop all others
  kill UNIT...                        Send signal to processes of a unit
  clean UNIT...                       Clean runtime, cache, state, logs or
                                      configuration of unit
  freeze PATTERN...                   Freeze execution of unit processes
  thaw PATTERN...                     Resume execution of a frozen unit
  set-property UNIT PROPERTY=VALUE... Sets one or more properties of a unit
  bind UNIT PATH [PATH]               Bind-mount a path from the host into a
                                      unit's namespace
  mount-image UNIT PATH [PATH [OPTS]] Mount an image from the host into a
                                      unit's namespace
  service-log-level SERVICE [LEVEL]   Get/set logging threshold for service
  service-log-target SERVICE [TARGET] Get/set logging target for service
  reset-failed [PATTERN...]           Reset failed state for all, one, or more
                                      units
Unit File Commands:
  list-unit-files [PATTERN...]        List installed unit files
  enable [UNIT...|PATH...]            Enable one or more unit files
  disable UNIT...                     Disable one or more unit files
  reenable UNIT...                    Reenable one or more unit files
  preset UNIT...                      Enable/disable one or more unit files
                                      based on preset configuration
  preset-all                          Enable/disable all unit files based on
                                      preset configuration
  is-enabled UNIT...                  Check whether unit files are enabled
  mask UNIT...                        Mask one or more units
  unmask UNIT...                      Unmask one or more units
  link PATH...                        Link one or more units files into
                                      the search path
  revert UNIT...                      Revert one or more unit files to vendor
                                      version
  add-wants TARGET UNIT...            Add 'Wants' dependency for the target
                                      on specified one or more units
  add-requires TARGET UNIT...         Add 'Requires' dependency for the target
                                      on specified one or more units
  edit UNIT...                        Edit one or more unit files
  get-default                         Get the name of the default target
  set-default TARGET                  Set the default target

Machine Commands:
  list-machines [PATTERN...]          List local containers and host

Job Commands:
  list-jobs [PATTERN...]              List jobs
  cancel [JOB...]                     Cancel all, one, or more jobs

Environment Commands:
  show-environment                    Dump environment
  set-environment VARIABLE=VALUE...   Set one or more environment variables
  unset-environment VARIABLE...       Unset one or more environment variables
  import-environment VARIABLE...      Import all or some environment variables

Manager State Commands:
  daemon-reload                       Reload systemd manager configuration
  daemon-reexec                       Reexecute systemd manager
  log-level [LEVEL]                   Get/set logging threshold for manager
  log-target [TARGET]                 Get/set logging target for manager
  service-watchdogs [BOOL]            Get/set service watchdog state

System Commands:
  is-system-running                   Check whether system is fully running
  default                             Enter system default mode
  rescue                              Enter system rescue mode
  emergency                           Enter system emergency mode
  halt                                Shut down and halt the system
  poweroff                            Shut down and power-off the system
  reboot                              Shut down and reboot the system
  kexec                               Shut down and reboot the system with kexec
  exit [EXIT_CODE]                    Request user instance or container exit
  switch-root ROOT [INIT]             Change to a different root file system
  suspend                             Suspend the system
  hibernate                           Hibernate the system
  hybrid-sleep                        Hibernate and suspend the system
  suspend-then-hibernate              Suspend the system, wake after a period of
                                      time, and hibernate

#61 Systemd 的问题

2019-12-20

开源中国上看到 Debian 正在投票是否支持非 systemd 初始化系统

  1. 2014 年素来混乱的 Debian 社区,经过了一番激烈争论之后(甚至是生命安全威胁),终于决定支持 systemd。
    PS: 这场争议导致部分核心人员离开,主要是对 Debian 社区的管理机制(混乱 “民主”)产生质疑。
  2. 2015 年 5 月发布的 Debian 8.0 (Jessie) 开始使用 Systemd 当默认的初始化系统。
    但是关于 Systemd 的争论从未停止,甚至有部分开发者独自维护了一个拒绝 Systemd 的分叉:Devuan

Systemd 真是闹出好大争议,和 GNOME3 一样。也正是有 GNOME3 的例子(社区总体来说,或者说大多数人,最后还是拥抱了变化,接纳 GNOME3),而且我只是一个实用主义的使用者,我觉得 Systemd 挺好的,能提升性能、使用又方便,为什么不呢。

#60 PowerShell 在 Ubuntu 下的安装和试用

2019-08-22

PowerShell 的最近的一次更新修复了其作为 SSH 的登录 Shell 的一些问题。
Win 10 对 NFS,SSH 的支持推进也是有目共睹。
随着 MS 对 Windows 的策略调整,估计以后 Windows 服务器也会变多。

# Download the Microsoft repository GPG keys
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb

# Register the Microsoft repository GPG keys
sudo dpkg -i packages-microsoft-prod.deb

# Update the list of products
sudo apt-get update

# Enable the "universe" repositories
sudo add-apt-repository universe

# Install PowerShell
sudo apt-get install -y powershell

# Start PowerShell
pwsh

PS 常用命令

# 获取本地 IP
(invoke-webrequest -uri "http://ifconfig.me/ip").content

get-nettcpconnection | where-object {$_.State -eq "Listen"}
get-nettcpconnection -localport 49667
get-process -id (get-nettcpconnection -localport 49667).owningprocess
stop-process -id (get-nettcpconnection -localport 11).owningprocess

"# README" | out-file README.md
type README.md

#59 Shell 编程备忘录

2019-08-21

PS: Shell 编程时总是不能区分什么是 Shell 语法,什么是系统命令。

基础

x=abc

echo hello world
echo 'Just output $x'
echo "The value of x: $x"

echo ${x:?变量未设置}
echo ${x:+变量已设置}
echo ${x:-默认值}

变量

Shell 就是标准的弱类型,所有的数据都是按照字符串的形式存储,然后根据上下文决定如何处理。

x='hello world' # 等于号两边不能有空格
local x='hello world' # 本地作用域,函数体中使用
echo $a
echo ${a}

unset a
readonly a  # 将变量设置成只读,不能修改,不能 unset
readonly b=1
# 没有提供方法取消 readonly 属性,但是我看到 SO 上有提供非常规方法来做这件事情

字符串变量

单引号不支持变量和转义(也就是说字符串中不能出现单引号)!

x="hello world"
echo $x

echo ${#x}
echo ${x/world/markjour} # 字符串替换

# 子字符串/字符串切片
echo ${x::5}  # hello
echo ${x:7:8} # world
echo ${x::4}  # hell
echo ${x::-4} # hello w

特殊变量

$0
$1
...
$n

# 所有参数
$*
$@

$# # 参数个数
$$ # 进程号
$! # 后台进程号
$? # 上一个命令的退出状态
$- # himBHs 当前 Shell 的选项

数组

arr=(zhao qian sun li)
arr[4]=zhou
arr[5]=wu
arr[6]=zheng
arr[7]=wang
echo ${arr[0]} # zhao
echo ${arr[@]} # zhao qian sun li zhao wu zheng wang

# 获取数组元素个数
echo ${#arr}    # 4 定义时的长度,后来新加的不算,也就是说这个值定义变量的时候就定了
echo ${#arr[@]} # 8

条件

if condition:
then
    :
else:
    :
fi
  • [ ]
  • [[ ]]
  • test

循环

for var in list:
do
    :
done

数学计算

test

逻辑

数学比较

字符串比较

文件相关

bcdfgkpurwxse
白菜豆腐干,靠谱如我,羞涩

最常用的:

  • -e 检查路径是否存在
  • -d 检查是否为目录
  • -f 检查是否为文件
  • -r 检查文件是否可读
  • -w 检查文件是否可写
  • -x 检查文件是否可执行
  • -s 检查文件是否为空

参考资料与拓展阅读

#58 ImageMagick 与中文渲染问题

2019-06-22

为什么 convert 和 montage 命令中使用的汉字都没能正确渲染在图片中去呢?
或许,可以将本文中的中文替换成 non-ascii 字符,或者 Unicode 字符。
或许,也可将 ImageMagick 替换成其他不支持中文的库。

参考了实在是太多文档,都没来得及记下来,只是从 Shell 的历史纪录中翻出来这些命令,做一个记录。

结论

字体问题,必须选择包含中文字形的字体,使用完整的字体名称,比如:Noto-Sans-Mono-CJK-SC-Regular

#56 我的 xterm 配置

2019-06-20

其实我没有怎么用过这个终端模拟器,用的多的还是 GNOME Terminal。
但这个灵活的 X11 终端模拟器,它的名字还是经常能在一些地方碰到的,比如 XShell、PuTTY 的配置中。
除了 xterm 之外,什么 Linux Console、rxvt、vt100、vt220、vt320 之类的,关于终端,可以说的太多。以后如果有空,对这个仔细了解之后再专门写一篇博客说这个吧!

#55 在终端看图

2019-06-19
  1. 通过 ASCII 字符或者色块,加上颜色转换实现
  2. 通过终端的图形支持实现

理论上,可以看图之后,也可以看视频,甚至做更多复杂的事情。

#54 转载:Linux 下使用 unzip 解压缩中文乱码问题

2019-05-06

一、问题提出

使用info-zip.org 的 unzip类 解压zip压缩包,如果压缩包中有中文文件名,那么解压这些文件出来时文件名是乱码的。

二、分析

参考这篇文章 让 Unzip 正确解压其中包含中文文件名的 Winzip 压缩包链接失效 (注:疑似转载链接),原因是 unzip 试图将 zip 文件中用 oem(ibm-dos) codepage 编码的文件名转换成自己的内部编码。可惜 unzip 只能转换极少数几种 codepage,中文的 cp936 不在其列。