9

I have Ubuntu 18.04.1 LTS running under Windows 10 Pro. After every reboot, the cron daemon is not started.

I can start it manually with sudo service cron start.

What can I configure or what system file I need to modify so that after a reboot, the cron daemon starts?

pa4080
  • 29,831
Tulio
  • 93

3 Answers3

17

As it is described in the article Starting Linux Background Services on Windows Login you need to trigger your command sudo service cron start from Windows. You can use Windows Task Scheduler for this purpose. Here are the steps from the mentioned article with a slight modifications:

1. Create startup script and make it executable:

echo "service cron start" | sudo tee /usr/local/bin/cronstart.sh
sudo chmod +x /usr/local/bin/cronstart.sh

Thus cronstart.sh will be accessible as shell command system wide.

2. Create a file within /etc/sudoers.d/ with purpose to allow your $USER to execute cronstart.sh by sudo without password. Run the following command to generate the line that must be placed in the sudoers file:

echo "$USER ALL=(ALL) NOPASSWD: /usr/local/bin/cronstart.sh"

Copy the output of the command, use the command sudo visudo -f /etc/sudoers.d/cronstart and paste the copied line as content of the newly created file. Save the file and exit.

3. Within Windows, go to the search bar, find and run Task Scheduler (as administrator if your current account isn't administrators one).

Now, click Task Scheduler Library on the left and then Create Task… on the right to create a new task. You can use the following parameters to configure the task:

  • General tab:

    Name the task anything you want, like WSL service cron start.

    Choice the option Run whether user is logged or not.

    Mark Do not store password and Run with highest privileges.

    In the Configure for dropdown select Windows 10.

    If you need to setup a task for another user click on the button Change User or Group....

  • Triggers tab:

    Click New… to add a new trigger for this task.

    In the Begin the task dropdown select At startup.

    Within the Advanced settings you can check Delay task for 1 minute.

  • Actions tab:

    Click New… to add a new action for this task.

    Pick Start a program for the action type and then enter C:\Windows\System32\wsl.exe as the program to run.

    At Add arguments (optional) set this: sudo cronstart.sh.

That’s it. Reboot the system, then open WLS terminal and use service cron status to check whether cron is running.


I've made some tests and unfortunately I found Windows 10 experiences some bugs and it is almost impossible to setup such task for a non-administrator user's account, while you are using Windows account instead of local one ... complete mess :O

As workaround in this case you can create a file called cronstart.bat in the folder shell:startup with content as follow:

C:\Windows\System32\wsl.exe sudo cronstart.sh

How to use VBS instead of a bat file to achieve the same result is shown here:

pa4080
  • 29,831
  • also you can use Task Scheduler > https://askubuntu.com/questions/1177273/wsl-ubuntu-is-there-an-easy-way-to-have-bash-start-automatically-on-windows-sta – Cornea Valentin Oct 05 '19 at 15:09
  • 4
    Sudoers part is not needed anymore as we can run commands as root: wsl.exe -u root my_command – Karolis Nov 17 '19 at 21:13
7

A new feature in Windows 11, assuming you can upgrade, is the ability to specify startup tasks for WSL using the /etc/wsl.conf file. If you have Windows 11, create that file with the following lines:

[boot]
command="service cron start"

According to the Microsoft doc, this will run the command as root when the WSL instance starts.

If you need to run multiple commands at WSL startup, separate them with semicolons in the same command line:

[boot]
command="service ssh start; service cron start"

You would still need to run WSL in a Windows startup task of some sort (as mentioned in the previous answer) in order to trigger the boot options, but this task can now be something as simple as wsl -e true. This will run the true command and then simply exit, but will also trigger the boot command.

NotTheDr01ds
  • 17,888
0

The only way I found and its seems to work perfectly and only takes 2 min to setup is described here

In short:

  1. Execute shell:startup in the basice windows shell application

  2. This will open the folder "C:\Users****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"

  3. Add shorcut "wsl.exe" with the target "C:\Windows\System32\wsl.exe sudo /etc/init.d/cron start"

  4. Edit you sudo config file via "sudo visudo"

  5. Add "%sudo ALL=NOPASSWD: /etc/init.d/cron start" to the file to prevent admin password for the cron service

Julian
  • 1