每天学习一个命令:ss socket 数据

ss 命令可以用来获取 socket 信息,可以用来显示和 netstat 类似的信息,但 ss 能够显示更多 TCP 和状态的信息,包括 PACKET,TCP,UDP,DCCP,RAW,和 Unix domain sockes 等等。

通过 ss ,可以获取当前机器如何和外界通信非常详细的信息,包括网络连接信息,网络协议,Linux socket 连接状态等等。通过这些信息,可以非常轻松和方便的定位网络问题。当服务器 Socket 连接数量比较大时,netstat 可能就罢工了,这个时候 ss 还是能够应付的,ss 使用了 TCP 协议栈中的 tcp_diagtcp_diag 是一个用于分析统计的模块。

命令格式

ss [options] [ FILTER ]

当没有选项使用时,ss 会显示一组已经建立 open non-listening sockets (TCP/UNIX/UDP) 连接的 sockets。

选项

-h, --help	帮助信息
-V, --version	程序版本信息
-n, --numeric	不解析服务名称
-r, --resolve        解析主机名
-a, --all	显示所有套接字(sockets)
-l, --listening	显示监听状态的套接字(sockets)
-o, --options        显示计时器信息
-e, --extended       显示详细的套接字(sockets)信息
-m, --memory         显示套接字(socket)的内存使用情况
-p, --processes	显示使用套接字(socket)的进程
-i, --info	显示 TCP 内部信息
-s, --summary	显示套接字(socket)使用概况
-4, --ipv4           仅显示 IPv4 的套接字(sockets)
-6, --ipv6           仅显示 IPv6 的套接字(sockets)
-0, --packet	        显示 PACKET 套接字(socket)
-t, --tcp	仅显示 TCP 套接字(sockets)
-u, --udp	仅显示 UCP 套接字(sockets)
-d, --dccp	仅显示 DCCP 套接字(sockets)
-w, --raw	仅显示 RAW 套接字(sockets)
-x, --unix	仅显示 Unix 套接字(sockets)
-f, --family=FAMILY  显示 FAMILY 类型的套接字(sockets),FAMILY 可选,支持  unix, inet, inet6, link, netlink
-A, --query=QUERY, --socket=QUERY
      QUERY := {all|inet|tcp|udp|raw|unix|packet|netlink}[,QUERY]
-D, --diag=FILE     将原始 TCP 套接字(sockets)信息转储到文件
 -F, --filter=FILE   从文件中都去过滤器信息
       FILTER := [ state TCP-STATE ] [ EXPRESSION ]

使用实例

显示 TCP 连接,UDP 连接,Unix Sockets

ss -u -a
ss -t
ss -x

显示摘要信息

ss -s

列出当前的 established, closed, orphaned and waiting TCP sockets

查看进程使用的 socket

命令

ss -lp

说明:

  • -l 参数显示当前正在监听的 socket

在结果中可以过滤出端口占用的进程

ss -lp | grep 80

过滤 TCP 状态

ss 命令能够通过 TCP states 来过滤,状态列表

  • established
  • syn-sent
  • syn-recv
  • fin-wait-1
  • fin-wait-2
  • time-wait
  • closed
  • close-wait
  • last-ack
  • listening
  • closing

ss 命令能够识别的其他状态

  • all (all of the above states)
  • connected (all the states with the exception of listen and closed)
  • synchronized (all of the connected states with the exception of syn-sent)
  • bucket (states which are maintained as minisockets, for example time-wait and
  • syn-recv)
  • big (Opposite to bucket state)

对于 tcp ipv4

ss -4 state FILTER
ss -4 state listening

对于 ipv6

ss -6 state FILTER

显示所有状态为 established 的 SMTP 连接

ss -o state established '( dport = :smtp or sport = :smtp )'

显示所有状态为 Established 的 HTTP 连接

ss -o state established '( dport = :http or sport = :http )'

这里一定要注意引号中的写法,该有的空格一定要有。

显示特定目的地的连接

ss 另外一个非常便利的功能就是可以查看特定 IP 地址的连接情况,比如想要查看多少连接从 IP 192.168.1.130 连接到本机,则可以

ss dst 192.168.1.130

对于本地连接同理

ss src 192.168.1.200
ss src 192.168.1.200:80
ss src 192.168.1.200:http
ss src 192.168.1.200:smtp

reference


2013-01-25 linux , command , socket

每天学习一个命令:kill 杀掉进程

Linux 中的 kill 命令用来终止指定的进程(terminate a process)的运行,是 Linux 下进程管理的常用命令。通常,终止一个前台进程可以使用 Ctrl+C 键,但是,对于一个后台进程就须用 kill 命令来终止。此时就需要先使用 ps/pidof/pstree/top 等工具获取进程 PID,然后使用 kill 命令来杀掉该进程。

kill 命令是通过向进程发送指定的信号来结束相应进程的。在默认情况下,采用编号为 15 的 TERM 信号。TERM 信号将终止所有不能捕获该信号的进程。对于那些可以捕获该信号的进程就要用编号为 9 的 kill 信号,强行“杀掉”该进程。

命令格式:

kill [options] [PID]

命令功能:

发送指定的信号到相应进程。不指定将发送 SIGTERM(15)终止指定进程。如果仍然无法终止该程序可用 “-KILL” 参数,其发送的信号为 SIGKILL(9) ,将强制结束进程,使用 ps 命令或者 jobs 命令可以查看进程号。root 用户可以控制用户的进程,非 root 用户只能杀死自己的进程。

命令参数:

-l  信号,若果不加信号的编号参数,则使用“-l”参数会列出全部的信号名称
-a  当处理当前进程时,不限制命令名和进程号的对应关系
-p  指定 kill 命令只打印相关进程的进程号,而不发送任何信号
-s  指定发送信号
-u  指定用户

注意:

kill 命令可以带信号号码选项,也可以不带。如果没有信号号码,kill 命令就会发出终止信号 (15),这个信号可以被进程捕获,使得进程在退出之前可以清理并释放资源。也可以用 kill 向进程发送特定的信号。例如:

kill -2 PID

它的效果等同于在前台运行 PID 的进程时按下 Ctrl+C 键。但是,普通用户只能使用不带 signal 参数的 kill 命令或最多使用 -9 信号。

kill 可以带有进程 ID 号作为参数。当用 kill 向这些进程发送信号时,必须是这些进程的所有者。如果试图撤销一个没有权限撤销的进程或撤销一个不存在的进程,就会得到一个错误信息。

可以向多个进程发信号或终止它们。

当 kill 成功地发送了信号后,shell 会在屏幕上显示出进程的终止信息。有时这个信息不会马上显示,只有当按下 Enter 键使 shell 的命令提示符再次出现时,才会显示出来。

应注意,信号使进程强行终止,这常会带来一些副作用,如数据丢失或者终端无法恢复到正常状态。发送信号时必须小心,只有在万不得已时,才用 kill 信号 9,因为进程不能首先捕获它。要撤销所有的后台作业,可以输入 kill 0。因为有些在后台运行的命令会启动多个进程,跟踪并找到所有要杀掉的进程的 PID 是件很麻烦的事。这时,使用 kill 0 来终止所有由当前 shell 启动的进程,是个有效的方法。

使用实例

列出所有信号名称

命令:

kill -l

输出:

HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT
CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

说明:

只有第 9 种信号 (SIGKILL) 才可以无条件终止进程,其他信号进程都有权利忽略。

下面是常用的信号:

HUP    1    终端断线
INT     2    中断(同 Ctrl + C)
QUIT    3    退出(同 Ctrl + \)
TERM   15    终止
KILL    9    强制终止
CONT   18    继续(与 STOP 相反, fg/bg 命令)
STOP    19    暂停(同 Ctrl + Z)

获取指定信号的数值

命令:

kill -l KILL
kill -l TERM

用 ps 查找进程,然后用 kill 杀掉

命令:

ps -ef | grep 'program'
kill PID

无条件彻底杀死进程

命令:

kill –9 PID

杀死指定用户所有进程

命令:

kill -9 $(ps -ef | grep username)
kill -u username

init 进程是杀不死的

命令:

kill -9 1

说明:

init 是 Linux 系统操作中不可缺少的程序之一。所谓的 init 进程,它是一个由内核启动的用户级进程。内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后,就通过启动一个用户级程序 init 的方式,完成引导进程。所以,init 始终是第一个进程(其进程编号始终为 1)。其它所有进程都是 init 进程的子进程,init 进程是无法杀死的。


2013-01-24 linux , command

Jekyll Introduction

This Jekyll introduction will outline specifically what Jekyll is and why you would want to use it. Directly following the intro we’ll learn exactly how Jekyll does what it does.

Overview

What is Jekyll?

Jekyll is a parsing engine bundled as a ruby gem used to build static websites from dynamic components such as templates, partials, liquid code, markdown, etc. Jekyll is known as “a simple, blog aware, static site generator”.

Examples

This website is created with Jekyll. Other Jekyll websites.

What does Jekyll Do?

Jekyll is a ruby gem you install on your local system. Once there you can call jekyll --server on a directory and provided that directory is setup in a way jekyll expects, it will do magic stuff like parse markdown/textile files, compute categories, tags, permalinks, and construct your pages from layout templates and partials.

Once parsed, Jekyll stores the result in a self-contained static _site folder. The intention here is that you can serve all contents in this folder statically from a plain static web-server.

You can think of Jekyll as a normalish dynamic blog but rather than parsing content, templates, and tags on each request, Jekyll does this once beforehand and caches the entire website in a folder for serving statically.

Jekyll is Not Blogging Software

Jekyll is a parsing engine.

Jekyll does not come with any content nor does it have any templates or design elements. This is a common source of confusion when getting started. Jekyll does not come with anything you actually use or see on your website - you have to make it.

Why Should I Care?

Jekyll is very minimalistic and very efficient. The most important thing to realize about Jekyll is that it creates a static representation of your website requiring only a static web-server. Traditional dynamic blogs like Wordpress require a database and server-side code. Heavily trafficked dynamic blogs must employ a caching layer that ultimately performs the same job Jekyll sets out to do; serve static content.

Therefore if you like to keep things simple and you prefer the command-line over an admin panel UI then give Jekyll a try.

Developers like Jekyll because we can write content like we write code:

  • Ability to write content in markdown or textile in your favorite text-editor.
  • Ability to write and preview your content via localhost.
  • No internet connection required.
  • Ability to publish via git.
  • Ability to host your blog on a static web-server.
  • Ability to host freely on GitHub Pages.
  • No database required.

How Jekyll Works

The following is a complete but concise outline of exactly how Jekyll works.

Be aware that core concepts are introduced in rapid succession without code examples. This information is not intended to specifically teach you how to do anything, rather it is intended to give you the full picture relative to what is going on in Jekyll-world.

Learning these core concepts should help you avoid common frustrations and ultimately help you better understand the code examples contained throughout Jekyll-Bootstrap.

Initial Setup

After installing jekyll you’ll need to format your website directory in a way jekyll expects. Jekyll-bootstrap conveniently provides the base directory format.

The Jekyll Application Base Format

Jekyll expects your website directory to be laid out like so:

.
|-- _config.yml
|-- _includes
|-- _layouts
|   |-- default.html
|   |-- post.html
|-- _posts
|   |-- 2011-10-25-open-source-is-good.markdown
|   |-- 2011-04-26-hello-world.markdown
|-- _site
|-- index.html
|-- assets
    |-- css
        |-- style.css
    |-- javascripts
  • _config.yml Stores configuration data.

  • _includes This folder is for partial views.

  • _layouts This folder is for the main templates your content will be inserted into. You can have different layouts for different pages or page sections.

  • _posts This folder contains your dynamic content/posts. the naming format is required to be @YEAR-MONTH-DATE-title.MARKUP@.

  • _site This is where the generated site will be placed once Jekyll is done transforming it.

  • assets This folder is not part of the standard jekyll structure. The assets folder represents any generic folder you happen to create in your root directory. Directories and files not properly formatted for jekyll will be left untouched for you to serve normally.

(read more: https://github.com/mojombo/jekyll/wiki/Usage)

Jekyll Configuration

Jekyll supports various configuration options that are fully outlined here: (https://github.com/mojombo/jekyll/wiki/Configuration)

Content in Jekyll

Content in Jekyll is either a post or a page. These content “objects” get inserted into one or more templates to build the final output for its respective static-page.

Posts and Pages

Both posts and pages should be written in markdown, textile, or HTML and may also contain Liquid templating syntax. Both posts and pages can have meta-data assigned on a per-page basis such as title, url path, as well as arbitrary custom meta-data.

Working With Posts

Creating a Post Posts are created by properly formatting a file and placing it the _posts folder.

Formatting A post must have a valid filename in the form YEAR-MONTH-DATE-title.MARKUP and be placed in the _posts directory. If the data format is invalid Jekyll will not recognize the file as a post. The date and title are automatically parsed from the filename of the post file. Additionally, each file must have YAML Front-Matter prepended to its content. YAML Front-Matter is a valid YAML syntax specifying meta-data for the given file.

Order Ordering is an important part of Jekyll but it is hard to specify a custom ordering strategy. Only reverse chronological and chronological ordering is supported in Jekyll.

Since the date is hard-coded into the filename format, to change the order, you must change the dates in the filenames.

Tags Posts can have tags associated with them as part of their meta-data. Tags may be placed on posts by providing them in the post’s YAML front matter. You have access to the post-specific tags in the templates. These tags also get added to the sitewide collection.

Categories Posts may be categorized by providing one or more categories in the YAML front matter. Categories offer more significance over tags in that they can be reflected in the URL path to the given post. Note categories in Jekyll work in a specific way. If you define more than one category you are defining a category hierarchy “set”. Example:

---
title :  Hello World
categories : [lessons, beginner]
---

This defines the category hierarchy “lessons/beginner”. Note this is one category node in Jekyll. You won’t find “lessons” and “beginner” as two separate categories unless you define them elsewhere as singular categories.

Working With Pages

Creating a Page Pages are created by properly formatting a file and placing it anywhere in the root directory or subdirectories that do not start with an underscore.

Formatting In order to register as a Jekyll page the file must contain YAML Front-Matter. Registering a page means 1) that Jekyll will process the page and 2) that the page object will be available in the site.pages array for inclusion into your templates.

Categories and Tags Pages do not compute categories nor tags so defining them will have no effect.

Sub-Directories If pages are defined in sub-directories, the path to the page will be reflected in the url. Example:

.
|-- people
    |-- bob
        |-- essay.html

This page will be available at http://yourdomain.com/people/bob/essay.html

Recommended Pages

  • index.html You will always want to define the root index.html page as this will display on your root URL.
  • 404.html Create a root 404.html page and GitHub Pages will serve it as your 404 response.
  • sitemap.html Generating a sitemap is good practice for SEO.
  • about.html A nice about page is easy to do and gives the human perspective to your website.

Templates in Jekyll

Templates are used to contain a page’s or post’s content. All templates have access to a global site object variable: site as well as a page object variable: page. The site variable holds all accessible content and metadata relative to the site. The page variable holds accessible data for the given page or post being rendered at that point.

Create a Template Templates are created by properly formatting a file and placing it in the _layouts directory.

Formatting Templates should be coded in HTML and contain YAML Front Matter. All templates can contain Liquid code to work with your site’s data.

Rending Page/Post Content in a Template There is a special variable in all templates named : content. The content variable holds the page/post content including any sub-template content previously defined. Render the content variable wherever you want your main content to be injected into your template:

...
<body>
  <div id="sidebar"> ... </div>
  <div id="main">
    {{content}}
  </div>
</body>
...

Sub-Templates

Sub-templates are exactly templates with the only difference being they define another “root” layout/template within their YAML Front Matter. This essentially means a template will render inside of another template.

Includes

In Jekyll you can define include files by placing them in the _includes folder. Includes are NOT templates, rather they are just code snippets that get included into templates. In this way, you can treat the code inside includes as if it was native to the parent template.

Any valid template code may be used in includes.

Using Liquid for Templating

Templating is perhaps the most confusing and frustrating part of Jekyll. This is mainly due to the fact that Jekyll templates must use the Liquid Templating Language.

What is Liquid?

Liquid is a secure templating language developed by Shopify. Liquid is designed for end-users to be able to execute logic within template files without imposing any security risk on the hosting server.

Jekyll uses Liquid to generate the post content within the final page layout structure and as the primary interface for working with your site and post/page data.

Why Do We Have to Use Liquid?

GitHub uses Jekyll to power GitHub Pages. GitHub cannot afford to run arbitrary code on their servers so they lock developers down via Liquid.

Liquid is Not Programmer-Friendly.

The short story is liquid is not real code and its not intended to execute real code. The point being you can’t do jackshit in liquid that hasn’t been allowed explicitly by the implementation. What’s more you can only access data-structures that have been explicitly passed to the template.

In Jekyll’s case it is not possible to alter what is passed to Liquid without hacking the gem or running custom plugins. Both of which cannot be supported by GitHub Pages.

As a programmer - this is very frustrating.

But rather than look a gift horse in the mouth we are going to suck it up and view it as an opportunity to work around limitations and adopt client-side solutions when possible.

Aside My personal stance is to not invest time trying to hack liquid. It’s really unnecessary from a programmer’s perspective. That is to say if you have the ability to run custom plugins (i.e. run arbitrary ruby code) you are better off sticking with ruby. Toward that end I’ve built Mustache-with-Jekyll

Static Assets

Static assets are any file in the root or non-underscored subfolders that are not pages. That is they have no valid YAML Front Matter and are thus not treated as Jekyll Pages.

Static assets should be used for images, css, and javascript files.

How Jekyll Parses Files

Remember Jekyll is a processing engine. There are two main types of parsing in Jekyll.

  • Content parsing. This is done with textile or markdown.
  • Template parsing. This is done with the liquid templating language.

And thus there are two main types of file formats needed for this parsing.

  • Post and Page files. All content in Jekyll is either a post or a page so valid posts and pages are parsed with markdown or textile.
  • Template files. These files go in _layouts folder and contain your blogs templates. They should be made in HTML with the help of Liquid syntax. Since include files are simply injected into templates they are essentially parsed as if they were native to the template.

Arbitrary files and folders. Files that are not valid pages are treated as static content and pass through Jekyll untouched and reside on your blog in the exact structure and format they originally existed in.

Formatting Files for Parsing.

We’ve outlined the need for valid formatting using YAML Front Matter. Templates, posts, and pages all need to provide valid YAML Front Matter even if the Matter is empty. This is the only way Jekyll knows you want the file processed.

YAML Front Matter must be prepended to the top of template/post/page files:

---
layout: post
category : pages
tags : [how-to, jekyll]
---

... contents ...

Three hyphens on a new line start the Front-Matter block and three hyphens on a new line end the block. The data inside the block must be valid YAML.

Configuration parameters for YAML Front-Matter is outlined here: A comprehensive explanation of YAML Front Matter

Defining Layouts for Posts and Templates Parsing.

The layout parameter in the YAML Front Matter defines the template file for which the given post or template should be injected into. If a template file specifies its own layout, it is effectively being used as a sub-template. That is to say loading a post file into a template file that refers to another template file with work in the way you’d expect; as a nested sub-template.

How Jekyll Generates the Final Static Files.

Ultimately, Jekyll’s job is to generate a static representation of your website. The following is an outline of how that’s done:

  1. Jekyll collects data. Jekyll scans the posts directory and collects all posts files as post objects. It then scans the layout assets and collects those and finally scans other directories in search of pages.

  2. Jekyll computes data. Jekyll takes these objects, computes metadata (permalinks, tags, categories, titles, dates) from them and constructs one big site object that holds all the posts, pages, layouts, and respective metadata. At this stage your site is one big computed ruby object.

  3. Jekyll liquifies posts and templates. Next jekyll loops through each post file and converts (through markdown or textile) and liquifies the post inside of its respective layout(s). Once the post is parsed and liquified inside the the proper layout structure, the layout itself is “liquified”. Liquification is defined as follows: Jekyll initiates a Liquid template, and passes a simpler hash representation of the ruby site object as well as a simpler hash representation of the ruby post object. These simplified data structures are what you have access to in the templates.

  4. Jekyll generates output. Finally the liquid templates are “rendered”, thereby processing any liquid syntax provided in the templates and saving the final, static representation of the file.

Notes. Because Jekyll computes the entire site in one fell swoop, each template is given access to a global site hash that contains useful data. It is this data that you’ll iterate through and format using the Liquid tags and filters in order to render it onto a given page.

Remember, in Jekyll you are an end-user. Your API has only two components:

  1. The manner in which you setup your directory.
  2. The liquid syntax and variables passed into the liquid templates.

All the data objects available to you in the templates via Liquid are outlined in the API Section of Jekyll-Bootstrap. You can also read the original documentation here: https://github.com/mojombo/jekyll/wiki/Template-Data

Conclusion

I hope this paints a clearer picture of what Jekyll is doing and why it works the way it does. As noted, our main programming constraint is the fact that our API is limited to what is accessible via Liquid and Liquid only.

Jekyll-bootstrap is intended to provide helper methods and strategies aimed at making it more intuitive and easier to work with Jekyll =)

Thank you for reading this far.

Next Steps

Please take a look at or jump right into Usage if you’d like.


2011-12-29 intro , beginner , jekyll , tutorial

电子书

本站提供服务

最近文章

  • 2024 年台北之行 去年的时候就得知了海外的大陆人可以通过官方网站申请入台证,从而可以在海外直接入境台湾,所以 4 月份女朋友过来日本之后就通过线上系统申请了入台证,入台证申请通过并付费之后是只有 3 个月有效期的,因为我们申请的比较晚,所以有效期的三个月正好落在了最热的 7,8,9 月份,但考虑到暑假有假期,我们还是决定硬着头皮买了机票。
  • macOS 上的多栏文件管理器 QSpace QSpace 是一个 macOS 上的多窗口平铺的文件管理器,可以作为 Finder 的代替,在 Windows 上曾经用过很长时间的 [[Total Commander]],后来更换到 Linux Mint 之后默认的文件管理器自带多面板,反而是用了很多年 macOS ,才意识到原来我缺一个多窗口,多面板的文件管理器。
  • Dinox 又一款 AI 语音转录笔记 前两天介绍过 [[Voicenotes]],也是一款 AI 转录文字的笔记软件,之前在调查 Voicenotes 的时候就留意到了 Dinox,因为是在小红书留意到的,所以猜测应该是国内的某位独立开发者的作品,整个应用使用起来也比较舒服,但相较于 Voicenotes,Dinox 更偏向于一个手机端的笔记软件,因为他整体的设计中没有将语音作为首选,用户也可以添加文字的笔记,反而在 Voicenotes 中,语音作为了所有笔记的首选,当然 Voicenotes 也可以自己编辑笔记,但是语音是它的核心。
  • Emote 又一款 AI 语音笔记应用 继发现了 Voicenotes 以及 Dinox 之后,又发现一款语音笔记 Emote,相较于前两款应用,Emote 吸引我的就是其实时转录的功能,在用 Voicenotes 的时候时长担心如果应用出现故障,没有把我要录下来的话录制进去,后期怎么办,而 Emote 就解决了这个问题,实时转录的功能恰好作为了一个声音录制的监听。
  • 音流:一款支持 Navidrome 兼容 Subsonic 的跨平台音乐播放器 之前一篇文章介绍了Navidrome,搭建了一个自己在线音乐流媒体库,把我本地通过 [[Syncthing]] 同步的 80 G 音乐导入了。自己也尝试了 Navidrome 官网列出的 Subsonic 兼容客户端 [[substreamer]],以及 macOS 上面的 [[Sonixd]],体验都还不错。但是在了解的过程中又发现了一款中文名叫做「音流」(英文 Stream Music)的应用,初步体验了一下感觉还不错,所以分享出来。