Windows 10 WSL
With WSL, you can install many Linux distros in Windows, e.g. officially Ubuntu, Ubuntu1804, Debian, … and unofficial like Archlinux. To start a backgroud linux daemon when Windows start, you need
- Recent version of Windows 10 (e.g. 1803, or maybe 1709 but not tested)
- Enable Windows subsystem for Linux
- Install a linux distro from Windows store (e.g. Ubuntu1804)
To test, create a simple script in your home folder, e.g. /home/username/testscript.sh
#! /bin/bash while [ 1 ] do echo `date` >> /mnt/c/tmp/test.txt sleep 10 done
Now at Windows schedule task, create a new task, with
command=bash
argument = -c /home/username/testscript.sh
Modify this task to “Run whether user is logged on or not”.
Here is the output
Reboot your windows machine without user login to see if the task is started or not.
Run Linux system daemon in Windows
Warning: You should understand completely before doing the following because it allows normal user to run as root.
You can start any task, even “cron” can be started without problem. But if you want to start cron, you need a special trick, setuid. This is because normal user cannot start privilege daemon like /usr/sbin/cron.
The command to start ‘cron’ in Windows task scheduler is similar,
bash -c /etc/init.d/cron start
But before you do it, you must setuid of the task, /usr/sbin/cron (login as root, chmod u+s /usr/sbin/cron). You may ask some Linux guys on how to limit which user(s) can run the ‘cron’ daemon.
Alternatively, you can modify the sudo configuration, e.g. /etc/sudoers.d and add the default user there so that the default user don’t need password to sudo the cron daemon.
I create the following cron task
* * * * * /bin/bash -c "echo hello from cron" >> /mnt/c/tmp/test.txt
You already notice the tail output screen above consists of this cron task.
You can also start the ssh server. Points to notice are:
- Port, if you also enable Windows 10 openssh server, you should change the Ubuntu’s sshd port to another one.
- The command to start is (assuming you can sudo without password):
bash -c "sudo service ssh start"
Update 2021-06-18
I try the above on wsl2 with Windows 10, 1909 (details below). It works without problem
Summary (assume windows 10 Pro, 1909 or above, wsl2 enabled):
- Execute wsl –set-default-version 2
- Install Ubuntu-20.04 (or any distro you like)
- Inside the distro, sudo apt install docker.io (you may use the official way to install docker. But this is the simplier way to install docker)
- sudo nohup dockerd & (because wsl2 Ubuntu doesn’t come with systemd, so need to start the executable directly)
- Add the default user to the docker group by editing the group file, sudo vi /etc/group
- sudo nohup /usr/bin/dockerd #may add additional options you like, e.g. -H fd:// –containerd=/run/containerd/containerd.sock
- Check docker daemon is running or not by “docker ps” or “docker run hello-world”
- add a new file, /etc/sudoers.d/username, with entry, username ALL=(ALL) NOPASSWD: /usr/bin/dockerd
- kill -s HUP $dockerd_pid #kill the dockerd in previous step
- At windows, try to execute dockerd: wsl.exe -d Ubuntu-20.04 sudo /usr/bin/dockerd
- If everything is ok, kill the dockerd daemon
- Add new schedule task, “Run whether user is logon or not”, “do not store password”, Trigger: At system start; Action: wsl -d Ubuntu-20.04 sudo /usr/bin/dockerd
- Run the scheduled task created in last step