Enable cron to run without asking for password
sudo service cron start in WSL will ask for the sudo password. This is an issue when we want to run cron from the script below. To fix this, we need to enable
cron service to run without asking for the password by adding it to
sudo nano /etc/sudoers # Add the following line to the end of the sudoers file %sudo ALL=(ALL) NOPASSWD: /usr/sbin/service cron * # Save and close the file. Exit the WSL window and reopen to ensure WSL restartCode language: PHP (php)
Create a file to start cron in a hidden window
Create a .vbs file (VBScript) with the following code.
(If you’re copying the code, you can remove the comment in line 4 starting with `’`.)
Dim objShell Set objShell = WScript.CreateObject ("WScript.shell") WScript.sleep 10000 'Below line will ask for WSL user's password if cron service is not added to NOPASSWD in sudoers objShell.run "C:\Windows\System32\bash.exe -c ""sudo service cron start""" Set objShell = NothingCode language: VBScript (vbscript)
Start WSL cron on Windows startup
To start cron when Windows starts up, copy the above .vbs file to the Windows startup folder. You can get to the startup folder by running
shell:startup in the Run window (Win+R).
Leave a Reply