linux 拾遗

linux 拾遗

hosts file编辑 /etc/hostsflush DNS cache: systemctl restart systemd-resolvedlinux 开机运行 shell 脚本方式一、将脚本...

花野猫

花野猫

更新于 2024-01-17

729

hosts file

  1. 编辑 /etc/hosts
  2. flush DNS cache: systemctl restart systemd-resolved

linux 开机运行 shell 脚本

方式一、将脚本添加到 /etc/rc.local 文件中

只适用于使用 SysVinit 或 Systemd 的 Linux 系统。

当 Linux 开机时,/etc/rc.local 文件中的命令将被自动执行

  1. 将脚本设置为可执行文件:chmod +x /usr/local/bin/myscript.sh
  2. 编辑 /etc/rc.local 文件,追加行如:/usr/local/bin/myscript.sh & 并保存。这里的 & 符号表示在后台运行该脚本。

方式二、将脚本添加到 systemd 启动项中

适用于大多数现代 Linux 发行版,但是一些较老的发行版可能不支持 systemd。此时可以尝试使用 SysVinit 或其他类似的启动项管理器。

  1. 创建 systemd 服务文件:sudo nano /etc/systemd/system/myscript.service

  2. 在文件中添加以下内容并保存:

    [Unit] Description=My Script Service After=network.target [Service] Type=simple ExecStart=/usr/local/bin/myscript.sh Restart=on-failure [Install] WantedBy=multi-user.target

    注意:这里的 ExecStart 行指定了要运行的脚本的路径。

  3. 启用该服务:

    bash
    sudo systemctl enable myscript.service
    sudo systemctl start myscript.service