写这篇博文的原因是我发现windows自动下载的ubuntu20.04是没有systemd服务的,而是wsl优化过的init,这时候就无法用systemd启用守护进程了(前情提要:使用Tailscale对多台服务器进行内部组网并进行流量转发#wsl

利用 .bashrc 或 .profile(最简单)

适用于希望服务在每次打开 WSL 终端时自动启动,并且该服务不需要在后台完全独立于终端运行(尽管可以通过 nohup 或 & 放到后台)

缺点

  • 只有在你打开 WSL 终端时才会启动。
  • 如果关闭所有 WSL 终端,服务可能会停止(取决于服务本身和如何启动)。
    不适合真正的系统级后台服务。

步骤

1、编辑 shell 配置文件:

nano ~/.bashrc

或者

nano ~/.profile

通常,~/.bashrc 用于交互式 shell,~/.profile 用于登录 shell。对于 WSL,两者都可能在启动时执行。

2、在文件末尾添加启动服务的命令:

使用 nohup 和 & 将服务放到后台运行,并防止它在终端关闭时被杀死。

# 自定义脚本
nohup /path/to/your/service_script.sh > /dev/null 2>&1 &

# 直接运行Web服务器的可执行文件
sudo /usr/sbin/apache2ctl start > /dev/null 2>&1 &
sudo /usr/sbin/nginx > /dev/null 2>&1 &

# 启动Python程序
nohup python3 /path/to/your/app.py > /dev/null 2>&1 &

# 确保只启动一次,避免重复启动
# 可以通过检查进程是否存在来避免重复
if ! pgrep -x "your_service_process_name" > /dev/null; then
nohup /path/to/your/service_script.sh > /dev/null 2>&1 &
fi
  • nohup: 防止进程在终端关闭时收到 SIGHUP 信号而终止。
  • > /dev/null 2>&1: 将标准输出和标准错误重定向到 /dev/null,避免在终端中打印大量信息。
  • &: 将命令放到后台运行。

利用 /etc/rc.local(传统 SysVinit 风格)

rc.local 是一个传统的 SysVinit 脚本,在系统启动的最后阶段执行。在一些没有 systemd 的系统上,它仍然是一个方便的自启动方式。

在 WSL 中,它可能不存在,需要你手动创建:

ls /etc/rc.local

优点

  • 在 WSL 实例启动时自动运行,即使没有打开终端。
  • 适合启动真正的后台服务。

缺点

  • 可能需要 sudo 权限来创建和编辑。
  • 需要确保脚本是可执行的。
  • 错误处理和日志记录不如 systemd 方便。

步骤

1、创建或编辑 /etc/rc.local:

sudo nano /etc/rc.local

2、添加以下内容:

#!/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.

# 启动一个自定义脚本
nohup /path/to/your/service_script.sh > /dev/null 2>&1 &

# 启动Apache
/usr/sbin/apache2ctl start > /dev/null 2>&1 &

# 启动Nginx
/usr/sbin/nginx > /dev/null 2>&1 &

exit 0
  • 非常重要:确保 #!/bin/sh -e 在第一行。
  • 非常重要:确保 exit 0 在文件的最后。
  • 使用 nohup& 将服务放到后台。
  • 使用服务的绝对路径。
  • 保存文件并退出。

3、使脚本可执行:

sudo chmod +x /etc/rc.local

4、完全重启 WSL 实例:

使用 Supervisor

Supervisor 是一个进程控制系统,可以管理非 systemd 环境下的后台进程。

优点

  • 更健壮的服务管理,包括自动重启。
  • 提供统一的配置文件和管理界面。
  • 不依赖于 systemd。

缺点

  • 需要额外安装和配置 Supervisor 本身。

步骤

步骤:

1、安装 Supervisor:

sudo apt update
sudo apt install supervisor

2、配置 Supervisor:

创建服务的配置文件,通常在 /etc/supervisor/conf.d/ 目录下。

sudo nano /etc/supervisor/conf.d/my_service.conf

:这个 my_service 可替换为实际的服务名字

示例 my_service.conf 内容:

[program:my_service]
command=/path/to/your/service_script.sh ; 服务的启动命令
autostart=true ; Supervisor 启动时自动启动
autorestart=true ; 服务退出时自动重启
startsecs=10 ; 启动后10秒内如果退出,认为是启动失败
stopwaitsecs=10 ; 停止服务时等待10秒
user=root ; 运行服务的用户
redirect_stderr=true ; 将标准错误重定向到标准输出
stdout_logfile=/var/log/supervisor/my_service.log ; 日志文件路径

更新 Supervisor 配置并启动服务:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start my_service

使 Supervisor 自身开机自启:

如果 supervisord 进程本身没有开机自启,需要使用上述方法一或方法二来启动 supervisord。可以将 supervisord -c /etc/supervisor/supervisord.conf 添加到 /etc/rc.local 中。

启动 tailscale 为守护进程

方法一

# --- Tailscale Autostart (via .bashrc) ---
# Check if tailscaled is already running
if ! pgrep -x "tailscaled" > /dev/null; then
echo "Starting tailscaled daemon..."
sudo nohup /usr/sbin/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock > /dev/null 2>&1 &
# Give it a moment to start
sleep 2
else
echo "tailscaled daemon already running."
fi
# --- End Tailscale Autostart ---
  • sudo: tailscaled 需要 root 权限,所以在 .bashrc 中使用 sudo 会在每次打开终端时提示你输入密码(如果不是 root 登陆的话)。这可能不太方便。如果你不想每次输入密码,可以配置 sudoers 文件允许你的用户在没有密码的情况下运行 tailscaled,但要小心操作。
  • pgrep -x "tailscaled":检查是否有精确匹配 tailscaled 进程名的进程在运行。

方法二

#!/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.

# --- Tailscale Autostart (via rc.local) ---
# Start tailscaled daemon in the background
nohup /usr/sbin/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock > /var/log/tailscale_rc.log 2>&1 &
sleep 2
# --- End Tailscale Autostart ---

exit 0

使脚本可执行:

sudo chmod +x /etc/rc.local

方法三

1、安装 Supervisor :

sudo apt update
sudo apt install supervisor

2、查找 tailscaled 路径:

which tailscaled

假设是 /usr/sbin/tailscaled

3、创建 Supervisor 配置文件:

sudo nano /etc/supervisor/conf.d/tailscale.conf

粘贴以下内容:

[program:tailscale]
command=/usr/sbin/tailscaled --state=/var/lib/tailscale/tailscaled.state --socket=/var/run/tailscale/tailscaled.sock
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=10
user=root
redirect_stderr=true
stdout_logfile=/var/log/supervisor/tailscale.log
stderr_logfile=/var/log/supervisor/tailscale_error.log

保存文件并退出。

4、更新 Supervisor 配置:

sudo supervisorctl reread
sudo supervisorctl update

5、禁用:

  • 运行 sudo supervisorctl stop tailscale
  • 删除 /etc/supervisor/conf.d/tailscale.conf 文件
  • 运行 sudo supervisorctl rereadsudo supervisorctl update