JsprX
  • Archive

Ubuntu 任务自启动

  • Linux
  • Ubuntu
  • Jun 15, 2021
  • 0 views

/etc/rc.local

可以直接在 /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.
sleep 2000
ethtool -s eno1 wol g

/etc/init.d/ 目录

  1. 在 /etc/init.d/ 目录下新建一个脚本文件,例如 app1.
    #!/bin/sh
    java -jar /opt/app1.jar &
    
  2. 改变脚本文件权限.
    sudo chmod 755 app1
    
  3. 更新启动目录.
    sudo update-rc.d app1 defaults
    

    如果需要删除此启动项目,

    sudo update-rc.d -f app1 remove
    

详情可见

man update-rc.d

systemd

Systemd1 是一套用于 Linux 基本工具,它提供了一个系统服务管理器,systemd 作为 PID 为 1 的进程启动,全名是 System Daemon.

我们可以在 /etc/systemd/system 目录中新建一个服务 test.service,然后写入:

[Unit]
Description=this is test service

[Service]
Type=simple
ExecStart= <shell command>  #启动命令
ExecStop=<shell command>    #停止命令,可缺省

[Install]
WantedBy=multi-user.target #所有用户组都启动这个任务

service 文件的具体格式,参照 systemd.service

现在我们需要加载 service,并且启动这个 service.

sudo systemctl daemon-reload
sudo systemctl enable test.service
sudo systemctl start test.service

可通过 systemctl status commandname.service 来查看开机自启动项目是否设立成功,也可以在开机之后利用进程监控。

  1. Systemd ↩

Last updated Jun 16, 2021

Weijie Xu

Subscribe

This work is licensed under a Attribution-NoDerivatives 4.0 International license. Attribution-NoDerivatives 4.0 International

PREVIOUS虚拟机搭建 Kubenetes
Search