5

wsl2 配置服务自启动

 1 year ago
source link: https://www.80shihua.com/archives/2890
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

wsl2 配置服务自启动 – 梦回故里

梦回故里 梦回故里是一个php程
梦回故里 无产阶级在这个革命中失去的只是锁链,他获得的将是整个世界。
Steps-to-automatically-start-WSL-services-with-Windows-10-boot-up.jpg
wsl2 配置服务自启动

2022-12-12 21:03

WSL2 本身是由 Windows 负责运行的,因此使用 tree 或 ps 命令时会看到根进程不是 systemd,这将导致无法启动 Linux 系统服务的守护进程 (deamon)。 当我们执行 systemctl 命令的时候,会显示出我们的 init system (PID 1) 并非 systemd,而是微软提供的 /init。

wsl2-service-autostart.jpg

因此一些服务无法自启动,因此我们需要一些办法来解决它。

解决的办法就是采用 windows 的自启动方法来,因为 windows 可以配置启动服务,那么我们可以添加一个 windows 的自启动服务来让它启动 wsl2 上的服务,这样当我们访问 wsl2 的时候,服务就已经启动了,这样会变得非常方便。

解决不了问题,我们不要去解决提出问题的人。而是要想尽一切办法来解决问题。

  1. 在 wsl 中创建一个启动配置文件,里面填写上要启动的服务。
sudo vi /etc/init.wsl
#! /bin/shservice docker startservice mysql start
  1. 赋予文件可执行权限
chmod +x /ect/init.wsl
  1. 在 windows 系统上创建一个启动脚本 startup.vbs。
Set ws = WScript.CreateObject("WScript.Shell") ws.run "wsl -d Ubuntu-22.04 -u root /etc/init.wsl",0
  1. 将启动脚本添加到启动目录。(通过 win+r,输入 shell:startup 进入 )。

之后,每次电脑重启的时候,wsl 上的服务就会自动启动,对于想要自启动的服务,只需要添加到 init.wsl 文件里面即可。

当然除了这种方式之外,其实对于服务来说,启动都是非常快的,并且,每次在 wsl 启动的时候都会加载.bashrc 文件,因此我们也可以把服务启动放到.bashrc 文件里面,这样打开 wsl 的时候,服务也会自动启动。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK