debian 新建服务

rc.local在老版本的debian系统中是有的,6版本以后,这个就没有了,这里记录一下Debian 9设置开机启动脚本的方式:
新增文件:
vi /etc/systemd/system/rc-local.service
编辑内容:
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
新增文件:
vi /etc/rc.local
内容:
#!/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
添加权限:
chmod +x /etc/rc.local
设置到系统启动:
systemctl enable rc-local
启动脚本:
systemctl start rc-local.service
检查服务状态:
systemctl status rc-local.service
上面你已经成功的创建了一个系统启动脚本,接下来你就可以在/etc/rc.loacl中的exit前增加任意你想启动的脚本了,比如:
cd /home/wjf/flypig
sh run.sh

在Linux中我们常常在/etc/rc.local写入执行命令或脚本,来实现系统开机启动功能。但偶尔也会遇到没有执行的情况,这时候就要来进行调试了。
以下就是调试步骤:
1、确定rc.local具有可执行权限,直接/etc/rc.local看看能不能执行。因为rc.local中已经写明了“In order to enable or disable this script just change the executionbits.”, 要让它生效只需要改变它的执行位。
2、看看当前系统的runlevel,再去对应rcX.d看看它的启动顺序,是不是有Src.local的启动项在这里面。
3、确认了前面两项,我们就可以来调试rc.local的内容了,首先第一行,默认一般是#!/bin/sh -e,查下shell编程就知道,这个是用的sh解释器,-e参数代表出错后直接退出不执行后面内容。
#!/bin/sh -e
echo yes
ls /
echo no
ls /ahfjkasf
echo hahaha
执行以上内容就会发现,hahaha是不会打印的。同时我们也要确认ls -l /bin/sh,来确认sh是对应的哪个解释器,因为在Ubuntu中,sh是链接在dash上。
因此在确认我们使用的SHELL解释器后,可以将-e 改成 -x或是在第二行加入set -x,这样就能将整个rc.local的执行过程打印到syslog中了,然后去查找出错的原因。
在执行某些脚本使用普通的重定向可能不生效,可以试试1 >>/tmp/xxx.log 2>&1这样的格式。

哪些服务能够禁止?
如何知道你需要哪些服务,而哪些又是可以安全地禁用的呢?它总是依赖于你的个性化需求。
这里举例了几个服务进程的作用。许多服务进程都是发行版特定的,所以你应该看看你的发行版文档(比如通过 google 或 StackOverflow)。
• accounts-daemon.service 是一个潜在的安全风险。它是 AccountsService 的一部分,AccountsService 允许程序获得或操作用户账户信息。我不认为有好的理由能使我允许这样的后台操作,所以我选择掩盖mask该服务进程。
• avahi-daemon.service 用于零配置网络发现,使电脑超容易发现网络中打印机或其他的主机,我总是禁用它,别漏掉它。
• brltty.service 提供布莱叶盲文设备支持,例如布莱叶盲文显示器。
• debug-shell.service 开放了一个巨大的安全漏洞(该服务提供了一个无密码的 root shell ,用于帮助 调试 systemd 问题),除非你正在使用该服务,否则永远不要启动服务。
• ModemManager.service 该服务是一个被 dbus 激活的守护进程,用于提供移动宽频broadband(2G/3G/4G)接口,如果你没有该接口,无论是内置接口,还是通过如蓝牙配对的电话,以及 USB 适配器,那么你也无需该服务。
• pppd-dns.service 是一个计算机发展的遗物,如果你使用拨号接入互联网的话,保留它,否则你不需要它。
• rtkit-daemon.service 听起来很可怕,听起来像是 rootkit。 但是你需要该服务,因为它是一个实时内核调度器real-time kernel scheduler。
• whoopsie.service 是 Ubuntu 错误报告服务。它用于收集 Ubuntu 系统崩溃报告,并发送报告到 https://daisy.ubuntu.com 。 你可以放心地禁止其启动,或者永久的卸载它。
• wpa_supplicant.service 仅在你使用 Wi-Fi 连接时需要。

猜你喜欢

转载自blog.csdn.net/seaship/article/details/86225877