5

[WSL]win10下Ubuntu子系统解决使用不了system相关命令的问题

 3 years ago
source link: https://blog.dugulingping.com/Linux/WSL2-Ubuntu.html
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.

[WSL]win10下Ubuntu子系统解决使用不了system相关命令的问题

January 24, 2021 on Linux

使用service命令代替systemctl相关命令,以下是自启动服务的操作方法

进入子系统中,创建并编辑文件:sudo nano /etc/init.wsl,文件里写入需要开机启动的服务:

#! /bin/sh
/etc/init.d/supervisor start
Shell

然后给init.wsl加上x的执行权限:sudo chmod +x /etc/init.wsl

随后在win10的Win+R运行输入shell:startup,在里面创建并编辑文件linux-start.vbs,文件内容为:

Set ws = WScript.CreateObject("WScript.Shell")        
ws.run "wsl -d Ubuntu-20.04 -u root /etc/init.wsl"

上述文件保存退出后重启windows后生效。。。

注意:使用PowerShell命令wsl --shutdown彻底关闭Linux子系统后,再如要再次开启子系统需要使用wsl -d Ubuntu-20.04 -u root /etc/init.wsl命令启动子系统,不然子系统中自启动服务不会工作。。。

前言(第一个坑)

这两天因为要写行走中的代码这篇文章,所以我安装了基于WSL的win10子系统Ubuntu20.04,在折腾systemctl enable supervisor时候报错Synchronizing state of supervisor.service with SysV service script with /lib/systemd/systemd-sysv-install.。下面开始着手解决这个问题。

上述说到systemctl enable supervisor命令报错,好嘛,既然行不通,那就按照报错提示中说的使用sysv命令,经过查询,和systemctl edis/enable severName具有一样功能的命令是chkconfig serverName on/off,好嘛,奥里给,干了!!!

结果。。。。

$ sudo chkconfig supervisor on
sudo: chkconfig: command not found
Shell

那这个坑怎么解决呢,这就要说到第三个坑(我直接好家伙)。

好嘛,再次经过一番搜索查询之后,得出结论是Ubuntu早在16.04就使用sysv-rc-conf命令代替了chkconfig,继续奥里给!!!

结果。。。。

$ sudo sysv-rc-conf supervisor on
sudo: sysv-rc-conf: command not found
Shell

于是又是一顿程序员的日常操作之后,得出结论需要apt安装一下,继续奥里给!!!!

结果给我报错E: Unable to locate package sysv-rc-conf (无法定位sysv-rc-conf软件包),欲哭无泪。。。我靠,这是为啥啊。。。。

最后是在/etc/apt/sources.list文件末尾添加deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse(清华阿里云的源应该也行),随后使用$ sudo apt update命令更新软件List,最后在终端输入下述命令即可解决!!!

$ sudo apt update
$ sudo apt install -y sysv-rc-conf
$ sysv-rc-conf supervisor on
Shell

使用sysv-rc-conf supervisor on来代替systemctl enable supervisor以实现开机服务自动启动功能~

或者干脆在/etc/rc.local末尾添加一行sudo /etc/init.d/supervisor start,可恶啊,可恶啊,一开始我居然没想到。。。

后来我重启了一次发现上述两种方法均没有生效,继续在网上搜索,在WSL2的文档中找到了解决办法,参见本文开头结论部分,hhhhhh

windows真香(逃)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK