Alpine Linux 常用命令

Alpine Linux源管理

1.國內源簡介:
這幾個都有alpine的源
清華大學:https://mirror.tuna.tsinghua.edu.cn/alpine/
阿里雲:https://mirrors.aliyun.com/alpine/
中科大:http://mirrors.ustc.edu.cn/alpine/
網易:http://mirrors.163.com/

2.配置:
直接抄中科大的幫助http://mirrors.ustc.edu.cn/help/alpine.html
一般情況下,將 /etc/apk/repositories 文件中 Alpine 默認的源地址 http://dl-cdn.alpinelinux.org/ 替換爲 http://mirrors.ustc.edu.cn/ 即可。

sudo vi /etc/apk/repositories

Alpine Linux 包管理

1.簡介
Alpine使用apk進行包管理,下面介紹常用命令

2.apk update
apk update #更新最新鏡像源列表

3.apk search
apk search #查找所以可用軟件包
apk search -v #查找所以可用軟件包及其描述內容
apk search -v 'acf*' #通過軟件包名稱查找軟件包apk search -v -d ‘docker’ #通過描述文件查找特定的軟件包

4.apk add
apk add openssh #安裝一個軟件
apk add openssh openntp vim #安裝多個軟件
apk add --no-cache mysql-client #不使用本地鏡像源緩存,相當於先執行update,再執行add

5.apk info
apk info #列出所有已安裝的軟件包
apk info -a zlib #顯示完整的軟件包信息
apk info --who-owns /sbin/lbu #顯示指定文件屬於的包

6.apk upgrade
apk upgrade #升級所有軟件
apk upgrade openssh #升級指定軟件
apk upgrade openssh openntp vim #升級多個軟件
apk add --upgrade busybox #指定升級部分軟件包

7.apk del
apk del openssh #刪除一個軟件

安裝python

sudo apk add --no-cache python3 python3-dev
其中已自帶pip
在這裏插入圖片描述
python其他依賴

sudo pip3 install Flask -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip install paho-mqtt -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip install redis -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
sudo pip install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

Alpine Linux服務管理

1.簡介
alpine沒有使用fedora的systemctl來進行服務管理,使用的是RC系列命令
未安裝openrc時需執行以下命令

sudo apk add --no-cache openrc

2.rc-update
rc-update主要用於不同運行級增加或者刪除服務。

alpine:~# rc-update --help
Usage: rc-update [options] add <service> [<runlevel>...]
   or: rc-update [options] del <service> [<runlevel>...]
   or: rc-update [options] [show [<runlevel>...]]
 
Options: [ asuChqVv ]
  -a, --all                         Process all runlevels
  -s, --stack                       Stack a runlevel instead of a service
  -u, --update                      Force an update of the dependency tree
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors)

3.rc-status
rc-status 主要用於運行級的狀態管理。

alpine:~# rc-status --help
Usage: rc-status [options] <runlevel>...
   or: rc-status [options] [-a | -c | -l | -m | -r | -s | -u]
 
Options: [ aclmrsuChqVv ]
  -a, --all                         Show services from all run levels
  -c, --crashed                     Show crashed services
  -l, --list                        Show list of run levels
  -m, --manual                      Show manually started services
  -r, --runlevel                    Show the name of the current runlevel
  -s, --servicelist                 Show service list
  -u, --unused                      Show services not assigned to any runlevel
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors)

4.rc-service
rc-service主用於管理服務的狀態

alpine:~# rc-service --help
Usage: rc-service [options] [-i] <service> <cmd>...
   or: rc-service [options] -e <service>
   or: rc-service [options] -l
   or: rc-service [options] -r <service>
 
Options: [ ce:ilr:INChqVv ]
  -e, --exists <arg>                tests if the service exists or not
  -c, --ifcrashed                   if the service is crashed then run the command
  -i, --ifexists                    if the service exists then run the command
  -I, --ifinactive                  if the service is inactive then run the command
  -N, --ifnotstarted                if the service is not started then run the command
  -l, --list                        list all available services
  -r, --resolve <arg>               resolve the service name to an init script
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors)

5.openrc
openrc主要用於管理不同的運行級。

alpine:~# openrc --help
Usage: openrc [options] [<runlevel>]
 
Options: [ a:no:s:SChqVv ]
  -n, --no-stop                     do not stop any services
  -o, --override <arg>              override the next runlevel to change into
                                    when leaving single user or boot runlevels
  -s, --service <arg>               runs the service specified with the rest
                                    of the arguments
  -S, --sys                         output the RC system type, if any
  -h, --help                        Display this help output
  -C, --nocolor                     Disable color output
  -V, --version                     Display software version
  -v, --verbose                     Run verbosely
  -q, --quiet                       Run quietly (repeat to suppress errors)

6.我常用的RC系列命令
1.增加服務到系統啓動時運行,下例爲docker

rc-update add docker boot

2.重啓網絡服務

rc-service networking restart

3.列出所有服務

rc-status -a

服務開機自啓

Alpine Linux 的 開機自啓目錄在/etc/local.d下,這個目錄用於放置我們需要在本地服務啓動或停止後執行的腳本

先上目錄下的說明文檔:

This directory should contain programs or scripts which are to be run
when the local service is started or stopped.

If a file in this directory is executable and it has a .start extension,
it will be run when the local service is started. If a file is
executable and it has a .stop extension, it will be run when the local
service is stopped.

All files are processed in lexical order.

Keep in mind that files in this directory are processed sequentially,
and the local service is not considered started or stopped until
everything is processed, so if you have a process which takes a long
time to run, it can delay your boot or shutdown processing.

簡單翻譯了下:

此目錄應包含在啓動或停止本地服務時要運行的程序或腳本。
如果此目錄中的文件是可執行文件且擴展名爲.start,
則在啓動本地服務時將運行該文件。
如果文件是可執行的並且具有.stop擴展名,
則在本地服務停止時將運行該文件。所有文件都按詞彙順序處理。
請記住,此目錄中的文件是按順序處理的,並且在處理完所有內容之前不會將本地服務視爲已啓動或停止,
因此如果您的進程需要很長時間才能運行,則可能會延遲啓動或關閉處理。

自定義開機自啓服務,由於Alpine Linux自帶了nohup後臺守護,我們就直接使用nohup設置開機自啓。

先編輯下startup.start文件,注意,.start後綴需要固定,前面名稱隨意
vi startup.start
我的內容如下

#!/bin/bash
sudo nohup python3 /home/plugin/main.py & >> /home/plugin/plugin.log

然後保存退出。

賦予腳本可執行權限:

chmod +x startup.start

設置 local 服務開機啓動:

rc-update add local
sudo service local start

如執行rc-update add local失敗,可能需執行sudo touch /run/openrc/softlevel之後在執行rc-update add local
這樣當系統開機時就能執行startup.start開達到開機自動的目的。
在這裏插入圖片描述
bashps -a 查看啓動的進程
在這裏插入圖片描述
總結:以上內容也適用於在docker 中基於Alpine 鏡像的業務擴展。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章