(20210128已解决)WSL2开机启动cron
发布日期:2021-05-07 14:24:30 浏览次数:21 分类:精选文章

本文共 1431 字,大约阅读时间需要 4 分钟。

  • Overview

    每次关机重启之后,wsl里面的cron服务是关闭的,如果忘记开启,就会导致定时任务失败。

    首先要明确跟随谁启动:

    1. wsl里的cron服务跟随wsl自启动
    2. 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-less sudo.

    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

上一篇:(20210129已解决)Pandas通过某列值包含特定字符串过滤行
下一篇:(20210116已解决)Windows下的CTF加载程序是什么?

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月07日 05时49分21秒