Debian 10 配置开机自启动脚本

文章介绍了在DebianGNU/Linux10系统中,通过修改~/.bashrc、/etc/profile和启用rc.local服务来添加启动脚本的方法。rc.local的设置包括创建文件、赋予执行权限以及启动和检查服务状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、环境:

操作系统:Debian GNU/Linux 10

二、方法1:~/.bashrc

可以添加脚本在 ~/.bashrc末尾执行。

三、方法2:/etc/profile

可以添加脚本在 /etc/profile末尾执行。

四、方法3:rc.local

1、查看一下rc-local.service文件是不是存在
路径:/lib/systemd/system/rc-local.service
内容:

#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# This unit gets pulled automatically into multi-user.target by
# systemd-rc-local-generator if /etc/rc.local is executable.
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

注:这里面就指出了可执行文件是 /etc/rc.local 。

2、默认的状态

root@npi:/lib/systemd/system# systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset:
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: inactive (dead)
     Docs: man:systemd-rc-local-generator(8)

注:因为没有 /etc/rc.local 这个文件,或者这个文件没有权限。

3、创建 /etc/rc.local
(1)写入内容

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
  
exit 0

注:如果有什么要开启启动的脚本可以插入在 exit 0 之前。

(2)赋予可执行权限

chmod +x /etc/rc.local

3、启动rc.local服务

systemctl start rc.local

注:重启后服务会自动启动

4、重启后查询状态

root@npi:~# systemctl status rc.local
● rc-local.service - /etc/rc.local Compatibility
   Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor
  Drop-In: /lib/systemd/system/rc-local.service.d
           └─debian.conf
   Active: active (exited) since Tue 2022-01-04 16:06:09 CST; 3min 51s ago
     Docs: man:systemd-rc-local-generator(8)
  Process: 367 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)

Jan 04 16:06:09 npi systemd[1]: Starting /etc/rc.local Compatibility...
Jan 04 16:06:09 npi systemd[1]: Started /etc/rc.local Compatibility.

注:验证确实有执行rc.local内的内容

Debian 系统中,如果你想要一个脚本开机时自动运行,你可以创建并管理名为 "init scripts" 或者 systemd units 的服务。以下是两个常见的方法: 1. 使用 init.d (旧版 Debian 和 Ubuntu): - 首先,你需要创建一个 shell 脚本,例如 `/etc/init.d/my_script`,内容应类似这样: ``` #!/bin/sh # 启动部分 start() { echo "Starting my_script..." your_command_here } # 停止部分 stop() { echo "Stopping my_script..." your_command_here } # 检查状态部分 status() { your_command_here_to_check_status } case "$1" in start) start ;; stop) stop ;; status) status ;; *) echo "Usage: /etc/init.d/my_script {start|stop|status}" exit 1 ;; esac ``` - 接着,需要设置脚本的权限: ```bash chmod +x /etc/init.d/my_script ``` - 最后,将脚本添加到系统的 runlevel 中,通常是在 `update-rc.d` 命令中指定: ```bash update-rc.d my_script defaults ``` 2. 使用 systemd (新版本 Debian 和 Ubuntu): - 创建一个新的 service 文件,比如 `/etc/systemd/system/my_script.service`,文件内容应包含: ```ini [Unit] Description=My Script Service After=network.target [Service] ExecStart=/usr/local/bin/my_script.sh Restart=always User=my_username [Install] WantedBy=multi-user.target ``` - 替换 `my_script.sh` 为你实际的脚本路径,`my_username` 为执行脚本的用户。 - 设置权限: ```bash sudo systemctl daemon-reload sudo systemctl enable my_script.service ``` - 启动服务: ```bash sudo systemctl start my_script.service ``` 记得替换 `your_command_here` 和相关路径信息,并确保脚本开机时有必要的环境和依赖。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值