ubuntu/Debain设置开机自启动
ubuntu/Debain 设置开机自启动
很多时候我们希望设置一些程序开机能够自动启动,把脚本或者某些可执行文件作为一种服务。
这里提供两种方式,假设我们的脚本路径是 /home/userme/a_script.sh。 更推荐使用方案2.
使用 rc.local 配置
可以参考 https://www.cnblogs.com/ssooking/p/6094740.html 提供的几种方式;
最简单的方法就是在 /etc/rc.local 目录下添加可执行脚本的路径,注意加在 exit 0上一行。
#!/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. /home/userme/a_script.sh exit 0
用这种方式配置的自启动服务将在root 下运行。
使用systemd unit配置
ystem daemon 系统守护进程。Systemd 可以管理所有系统资源。不同的资源统称为 Unit(单位)。Unit 一共分成12种。我们要设置开机自启动服务就要使用Service unit.
在 /etc/systemd/system中添加一个a-script.service
[Unit] Description=A system daemon service After=networking.service [Service] RemainAfterExit=false ExecStart=/home/userme/a_script.sh TimeoutStartSec=0 Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
After 指明了这个服务在哪个服务后启动, ExecStart指明了可执行文件的地址, Restart指明了何时重启, always表示任何情况下都会重启(也就是说这是个杀不死的,还可以选择no abort 等).
然后在 terminal 键入以下命令
# reload configure sudo systemctl daemon-reload # enable service sudo systemctl enable a-script.service sudo systemctl start a-script.service
服务就启动了,如果要查看 服务的运行状态可以用 systemctl status a-script.service 命令; 停止服务可以用 stop, 重启用 restart ; 注意停止服务只是在这次开机有效,如果要取消开机自启动,需要用
udo systemctl disable a-scirpt.service.
学习 systemd 命令可以参考阮一峰的博客(http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html) http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html 详细配置 systemd uint 可以参考(http://www.ruanyifeng.com/blog/2016/03/node-systemd-tutorial.html)- 上一篇:Windows和Linux文件传输方式总结
- 下一篇:ubuntu卸载程序软件