跳转到内容

博客

Execute Scripts on Period with Systemd

本教程旨在通过Systemd自带的Timer实现定时执行任务

本文以实现定时的git pull为例,讲解Systemd的定时器

该脚本的主要目的为进入指定的git路径,并执行git pull指令

需要确保该路径存在一个已经clone到本地的git仓库

#!/bin/sh
cd ${pwd_of_git_repo_path}
git pull

${pwd_of_git_repo_path}表示git仓库的文件夹

(2). 编辑完后保存退出:wq,为脚本添加运行权限:

Section titled “(2). 编辑完后保存退出:wq,为脚本添加运行权限:”
sudo chmod +x gitpull.sh

命令行运行./gitpull.sh,当命令行出现和正常运行git pull指令一样的输出时,说明脚本正常

(1). 在/etc/systemd/system/或者/usr/lib/systemd/system/目录中创建一个名为gitpull.service的文件,内容如下:

Section titled “(1). 在/etc/systemd/system/或者/usr/lib/systemd/system/目录中创建一个名为gitpull.service的文件,内容如下:”
[Unit]
Description=gitpull service
[Service]
User=${USER}
Group=${GROUP}
ExecStart=/bin/bash ${pwd_of_scripts}/gitpull.sh
[Install]
WantedBy=multi-user.target

${pwd_of_scripts} 表示上一步创建的脚本所在文件夹 ${USER}表示执行脚本的用户,${GROUP}表示用户所在组

重载并运行gitpull.service

sudo systemctl daemon-reload
sudo systemctl start gitpull.service

查看Systemd的输出日志,确定service是否正常运行,输出与直接git pull是否相同

sudo systemctl status gitpull.service
[Unit]
Description=run gitpull.sh every 1hour
[Timer]
OnUnitActiveSec=1h
Unit=gitpull.service
[Install]
WantedBy=multi-user.target

Timer的名称需要与Service的名称一致

OnUnitActiveSec表示运行定时器运行的间隔

Oncalnedar表示在特定时间运行

Unit表示需要运行的service

  • Oncalnedar * *-*-* *:*:*
    • * - To signify the day of the week eg:- Sat,Thu,Mon
    • *-*-* - To signify the calendar date. Which means it breaks down to - year-month-date.
      • 2021-10-15 is 15th of October
      • *-10-15 means every year at 15th October
      • *-01-01 means every new year.
    • *:*:* is to signify the time component of the calnedar event. So it is - hour:minute:second
sudo systemctl daemon-reload
sudo systemctl start gitpull.timer

检查gitpull.service和gitpull.timer的运行状态和输出

sudo systemctl status gitpull.service
sudo systemctl status gitpull.timer
sudo systemctl enable gitpull.service
sudo systemctl enable gitpull.timer

[1]. http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html

[2]. https://silentlad.com/systemd-timers-oncalendar-(cron)-format-explained

[3]. Systemd-初探

[4]. Cloudflare-DDNS-with-ShellScripts

[5]. Auto-Archive-Files-with-ShellScripts

Linux Auto PAC

如果没有安装pip,先安装pip sudo apt-get install python-pip pip install genpac

mkdir ~/shadowsocks
cd shadowsocks
genpac --proxy="SOCKS5 127.0.0.1:1080" --gfwlist-proxy="SOCKS5 127.0.0.1:1080" -o autoproxy.pac --gfwlist-url="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
  • 其中1080表示代理的端口号

3. 命令行中输入以下命令,查看代理是否正常运行

Section titled “3. 命令行中输入以下命令,查看代理是否正常运行”
curl -so /dev/null -w "%{http_code}" google.com -x socks5://127.0.0.1:1080
  • socks5表示使用的代理协议,可以换成http等其他协议
  • 10080是代理的端口号,对应代理服务的Input端口

打开下列路径 [system] settings -> network -> network proxy 选择代理方式为automatic, 配置url填写为

file:///PATH_TO_PAC/autoproxy.pac

  • 按照实际情况填写pac文件的路径

[1]. https://stackoverflow.club/using-pac-in-linux/

[2]. 检测代理是否运行正常

Manage Podman Container With Systemd

Podman与Docker不同,在架构上无需docker daemon控制容器的运行,因此容器的重启需要由systemd接管。

podman generate systemd -t 30 -n --new -f web
  • -t: 超时时间
  • -n: 表示使用容器名代替容器id
  • -f: 表示生成服务文件
  • –new: 每次启动都删除旧容器,启动一个新的容器
podman pod create --name POD_NAME
podman container run --pod POD_NAME --name CONTAINER_NAME1
podman container run --pod POD_NAME --name CONTAINER_NAME2

(3). Generate systemd service files for Pod and Containers inside

Section titled “(3). Generate systemd service files for Pod and Containers inside”
podman generate systemd -f --new --name POD_NAME
  • -f: 表示生成服务文件
  • –new: 每次启动都删除旧容器,启动一个新的容器
  • POD_NAME: pod名称

(1). Move service file to /usr/lib/systemd/system

Section titled “(1). Move service file to /usr/lib/systemd/system”

(2). Restore SELinux Label with command below

Section titled “(2). Restore SELinux Label with command below”
restorecon /usr/lib/systemd/system/container-***.service
systemctl daemon-reload
systemctl enable container-***.service

[1]. https://blog.51cto.com/omaidb/4531846

[2]. https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/assembly_porting-containers-to-systemd-using-podman_building-running-and-managing-containers#doc-wrapper

[3]. https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/9/html/building_running_and_managing_containers/proc_auto-starting-pods-using-systemd_assembly_porting-containers-to-systemd-using-podman

[4]. Systemd-初探

Podman Volumes and SELinux

Security-Enhanced Linux(SELinux)是一种适用于Linux系统的安全架构,它允许管理员更好地控制谁可以访问系统WhatisSELinux?。详细有关SELinux的叙述参考SELinux策略与规则。

当Podman运行在启用了SELinux的系统上,且需要映射Volumes时,将会遇到权限问题。

在以非root账户运行podman容器时,/home/user/目录下的文件可能存在以下的上下文权限:unconfined_u:object_r:data_home_t:s0,但是podman volumes需要这样的权限system_u:object_r:container_file_t:s0才能正常运行。

podman在运行时创建的容器卷将自动设置适当的上下文。 但是,对于/home/user/目录卷,podman默认不会更改上下文。

podman提供了两种lables用于运行时修改安全上下文::z和:Z

  • Shared Lables
  • Private Lables

Shared Lables 允许多个容器同时访问一个volume;

Private labels 只允许特定的容器访问该volum。

[1]. https://blog.christophersmart.com/2021/01/31/podman-volumes-and-selinux/

[2]. https://m-zhoujie2.gitbooks.io/-linux-devops-2/content/chapter3-10.html

[3]. SeLinux-初探

Podman 网络注意事项


标签: - 防火墙 - 波德曼类别: - Linux - 容器日期:创建时间:2022-06-02更新时间:2024-01-19—

0.简介这是关于 RHEL 上 podman 网络和防火墙相关的一些问题的说明。

Section titled “0.简介这是关于 RHEL 上 podman 网络和防火墙相关的一些问题的说明。”

2. 问题详情|- 网络|- 网|-p|防火墙状态|

Section titled “2. 问题详情|- 网络|- 网|-p|防火墙状态|”

|-|-|-|-| | - |[x]|[x]|允许| |桥|[x]|[x]|被阻止| |主持人| - | - |被阻止|

如上表所示,如果仅使用带有--net和-p参数的 podman,firewalld 不会阻止该端口上的访问。 但是,配置了--network bridge并配置了上述参数后,firewalld 将阻止此访问。 如图所示,firewalld 将阻止配置--network host的所有访问。

3.修复上述问题在端口映射上,主机端口上的特定 IP 地址如127.0.0.1。

Section titled “3.修复上述问题在端口映射上,主机端口上的特定 IP 地址如127.0.0.1。”

[2]. Netstat-常用参数