root@debian:~# cat /lib/systemd/system/rc-local.service # SPDX-License-Identifier: LGPL-2.1-or-later # # 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=/etc/rc.local Compatibility Documentation=man:systemd-rc-local-generator(8) ConditionFileIsExecutable=/etc/rc.local After=network.target
cat <<EOF >/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 EOF
在exit 0之前写要启动的命令即可
然后赋予它执行权限
1
chmod +x /etc/rc.local
接着启动服务
1
systemctl enable --now rc-local
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
root@debian:~# systemctl enable --now rc-local The unit files have no installation config (WantedBy=, RequiredBy=, Also=, Alias= settings in the [Install] section, and DefaultInstance= for template units). This means they are not meant to be enabled using systemctl. Possible reasons for having this kind of units are: • A unit may be statically enabled by being symlinked from another unit's .wants/ or .requires/ directory. • A unit's purpose may be to act as a helper for some other unit which has a requirement dependency on it. • A unit may be started when needed via activation (socket, path, timer, D-Bus, udev, scripted systemctl call, ...). • In case of template units, the unit is meant to be enabled with some instance name specified. Job for rc-local.service failed because the control process exited with error code. See "systemctl status rc-local.service" and "journalctl -xe" for details.
Jun 19 13:22:16 debian systemd[1]: Starting /etc/rc.local Compatibility... Jun 19 13:22:16 debian rc.local[590482]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined Jun 19 13:22:16 debian rc.local[590482]: At least one of these environment variable is needed to run this program Jun 19 13:22:16 debian systemd[1]: rc-local.service: Control process exited, code=exited, status=1/FAILURE Jun 19 13:22:16 debian systemd[1]: rc-local.service: Failed with result 'exit-code'. Jun 19 13:22:16 debian systemd[1]: Failed to start /etc/rc.local Compatibility.