
本文共 1431 字,大约阅读时间需要 4 分钟。
-
Overview
每次关机重启之后,
wsl
里面的cron
服务是关闭的,如果忘记开启,就会导致定时任务失败。首先要明确跟随谁启动:
wsl
里的cron
服务跟随wsl
自启动wsl
里的cron
服务跟随Window10
自启动
-
Starting Linux Background Services on Windows Login
WSL now have supports for background tasks when all your Linux console windows are closed. However, you still needed to start those services yourself because Linux sessions could only be initiated explicitly. There was no start-up config to launch services.
Here is how you can start WSL background services on Windows login:
First, you’ll need a startup script
# create a file named start_cron.sh and put it in ~/.local/bin/echo "service cron start" >> ~/.local/bin/start_cron.shchmod +x ~/.local/bin/start_cron.sh
Running service requires
sudo
privledges. But, since we need to run this at start-up without interaction, so we should enable password-lesssudo
.Second, enable password-less sudo for specific commands
# Open visudo to edit your sudo configsudo visudo# add this line to enable sudo run only this script, replace userid with your ownuserid ALL=(root) NOPASSWD: /home/userid/.local/bin/start_cron.sh
Third, get Windows to run this script in the WSL layer when you login
We can use the Task Scheduler to do this, so open the start menu and type “Task Scheduler(任务计划程序)” to find the application and run it.
任务计划程序库 --> 创建任务
常规:start_cron
触发器:新建>>登录时>>确定
操作:新建>>启动程序>>程序或脚本(
C:\Windows\System32\bash.exe
)>>参加参数(-c"sudo ~/.local/bin/start_cron.sh"
) -
References
发表评论
最新留言
关于作者
