目錄下作以下七步操作
# samxiao @ rm411 in ~/auto-run-when-boot [17:40:52]
$ vi xsz.service
$ vi xsz-boot.sh
$ chmod +x xsz-boot.sh
$ sudo ln -s /home/samxiao/auto-run-when-boot/xsz.service /etc/systemd/system/xsz.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable xsz.service
$ sudo reboot
操作結果
xsz.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=SamXIAO home booting
# Documentation=man:systemd-rc-local-generator(8)
# ConditionFileIsExecutable=/etc/rc.local
# Requires=basic.target
# AllowIsolate=yes
# Wants=thin
After=network.target
[Service]
Type=forking
# Type=simple
# ExecStart=/etc/rc.local start
ExecStart=/home/samxiao/auto-run-when-boot/xsz-boot.sh
TimeoutSec=0
RemainAfterExit=yes
KillMod=control-group
# Restart=no #on-failure
# RestartSec=10
GuessMainPID=no
[Install]
WantedBy=multi-user.target
Alias=SamXIAO.service
xsz-boot.sh內容
#! /bin/bash
cd /home/samxiao/redmine-4.1.1
bundle exec rails server thin -e production -p 10086 -d
systemctl status thin
其它
# samxiao @ rm411 in ~/auto-run-when-boot [17:40:44]
$ ll
total 8.0K
-rwxrwxr-x 1 samxiao samxiao 123 Feb 19 17:40 xsz-boot.sh
-rw-r--r-- 1 samxiao samxiao 966 Feb 19 17:22 xsz.service
# samxiao @ rm411 in ~/auto-run-when-boot [17:26:05]
$ chmod +x xsz-boot.sh
chmod +x xsz-boot.sh
# samxiao @ rm411 in ~/auto-run-when-boot [17:27:43] C:1
$ sudo ln -s /home/samxiao/auto-run-when-boot/xsz.service /etc/systemd/system/xsz.service
sudo ln -s /home/samxiao/auto-run-when-boot/xsz.service /etc/systemd/system/xsz.service
[sudo] password for samxiao:
# samxiao @ rm411 in ~/auto-run-when-boot [17:28:10]
$ ll /etc/systemd/system/xsz.service
ll /etc/systemd/system/xsz.service
lrwxrwxrwx 1 root root 44 Feb 19 17:27 /etc/systemd/system/xsz.service -> /home/samxiao/auto-run-when-boot/xsz.service
# samxiao @ rm411 in ~/auto-run-when-boot [17:28:14]
$ sudo systemctl daemon-reload
sudo systemctl daemon-reload
# samxiao @ rm411 in ~/auto-run-when-boot [17:30:19] C:1
$ sudo systemctl enable xsz.service
sudo systemctl enable xsz.service
Created symlink /etc/systemd/system/SamXIAO.service → /home/samxiao/auto-run-when-boot/xsz.service.
Created symlink /etc/systemd/system/multi-user.target.wants/xsz.service → /home/samxiao/auto-run-when-boot/xsz.service.
# samxiao @ rm411 in ~/auto-run-when-boot [17:40:07]
$ sudo systemctl restart xsz.service
# samxiao @ rm411 in ~/auto-run-when-boot [17:40:15]
$ sudo systemctl status xsz.service
● xsz.service - SamXIAO home booting
Loaded: loaded (/home/samxiao/auto-run-when-boot/xsz.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-02-19 17:40:15 CST; 10s ago
Process: 6965 ExecStart=/home/samxiao/auto-run-when-boot/xsz-boot.sh (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 2248)
Memory: 168.8M
CGroup: /system.slice/xsz.service
└─7072 /usr/local/bin/ruby bin/rails server thin -e production -p 10086 -d
Feb 19 17:40:15 rm411 xsz-boot.sh[6966]: => Rails 5.2.4.2 application starting in production on http://0.0.0.0:10086
Feb 19 17:40:15 rm411 xsz-boot.sh[6966]: => Run `rails server -h` for more startup options
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: ● thin.service - LSB: thin initscript
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Loaded: loaded (/etc/init.d/thin; generated)
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Active: active (exited) since Fri 2021-02-19 17:31:50 CST; 8min ago
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Docs: man:systemd-sysv-generator(8)
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Process: 837 ExecStart=/etc/init.d/thin start (code=exited, status=0/SU>
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Feb 19 17:31:48 rm411 systemd[1]: Starting LSB: thin initscript...
Feb 19 17:40:15 rm411 xsz-boot.sh[7071]: Feb 19 17:31:50 rm411 systemd[1]: Started LSB: thin initscript.
Feb 19 17:40:15 rm411 systemd[1]: Started SamXIAO home booting.
停止,重啓,啓動,查看狀態
$ sudo systemctl start xsz.service
$ sudo systemctl stop xsz.service
$ sudo systemctl restart xsz.service
$ sudo systemctl status xsz.service
參考文章
-
Redmine-4.1.1 使用Thin作爲Web服務器(Ubuntu 20.04.2)
- 又耗時6天-解決JNOJ(江南OJ)判題機dispatcher在Ubuntu18.04.3中不能自動啓動的問題
- ubuntu18.04 使用systemd方式添加開機運行sh腳本
- Systemd 入門教程:實戰篇