說在前面:
在ubuntu 20.04上是沒有像centos 那樣的/etc/rc.local文件的。如果要加開機啟動,可以通過systemd的方式。
方式如下
1. 在 /etc/systemd/system 文件下新建一個文件xx.service
2. 編輯內容
[Unit] Description=xx app init service Documentation=see /opt/init.sh After=network.target auditd.service [Service] ExecStart=/bin/bash /opt/init.sh Restart=on-failure Type=oneshot RemainAfterExit=yes [Install] WantedBy=multi-user.target Alias=tsingyun.service
這里需要注意2點
1. Type=oneshot ,這種方式可以讓你用nohup 起動某些服務,比如spring boot 之類
2. RemainAfterExit=yes 表示雖然nohup后台運行了,也認為這個服務是起來的。可以通過systemctl status xx.service 查看狀態
3. 要啟動的程序可以寫在/opt/init.sh中
例如
#!/bin/bash # 開機啟動腳本,一定要后台運行。java程序要加nohup export JAVA_HOME=/opt/jdk1.8.0_281 export PATH=$PATH:$JAVA_HOME/bin /opt/apache-activemq-5.15.15/bin/activemq start
4.
systemctl enable xx.service
systemctl restart xx.service
參考文檔
http://manpages.ubuntu.com/manpages/focal/man5/systemd.service.5.html
