Windows has several alternatives to automate tasks. The most common tool is the Windows Task Scheduler, but if you are using the Windows for Linux Subsystem (WSL), also there is the cron daemon that runs background tasks for your WSL installation.
Cron does not run by default
In Windows 10 and Windows 11, cron comes bundled with Linux environments like Ubuntu. The problem is that WSL does not start cron automatically, which means your automated tasks don't run by default.
To fix this problem, you can start cron manually every time you open the command line, but manually starting a tool that is supposed to automate tasks is like losing consciousness.
Fortunately, there is an easy way to fix this problem and you need to use Task Scheduler.
If you've never used cron on Linux to run tasks, check out our previous tutorial on scheduling tasks in Linux. For our purposes here, we will assume that you have already created some cron jobs in your WSL installation and you need help making sure they run instead of taking care of cron all the time.
For this tutorial, we will use sudo service
to check and start cron, which is the recommended way to stop and start services in modern versions of Ubuntu, the most popular distribution for WSL.
At the same time, Please note that this tutorial assumes you have administrator rights on your version of WSL. If you are the only user on your PC and you enabled WSL on your own, then you have administrator rights.
Tip: This also works on the Windows subsystem for Linux on Windows 11, not only on Windows 10.
RELATED: How to schedule tasks in Linux: Introduction to Crontab files
Prepare Linux
The first thing we will do is allow the computer to start cron without a password. When you start a service like cron, use the command sudo service cron start
. But that command needs a password, which Windows will not have access to when it starts. The way around this is to disable the requirement for a password for this command.
To do that, open your WSL terminal window and type sudo visudo
. Press Enter on your keyboard, enter your Linux password and hit the Enter key again. If you are using Ubuntu, this opens the file “sudoers” using the beginner friendly Nano command line text editor. Sudoers is a file for system administrators that can change the privileges and access rights of users.
Add the following command to the end of the sudoers file and then press Ctrl + or to save and Ctrl + x to exit the file.
%sudo ALL=NOPASSWD: /usr/sbin/service cron start
This sudoers command says that any user who has sufficient privileges to use the sudo command (that should incorporate you) you don't need a password to run the command sudo service cron start
, which starts the cron daemon.
Once you have saved the file, you can verify that the command is doing its job by typing sudo service cron start
, and it should start cron without asking for a password. If that worked, let's turn cron off again so we can test that the task we are creating in the next step works correctly. To do that, please run sudo service cron stop
.
Configure Cron in Windows Task Scheduler
That's stage one of our journey to cron automation.. Let's move on to the part 2 with Task Scheduler. Tap the Windows key on the keyboard and then search for “Task scheduler”. Launch the shortcut of the “Task scheduler”.
When it starts, look in the section “Actions” and select “Create basic task”.
This opens the Basic Task Wizard. First, it will ask you to name the task and give it a description. You can enter what you want here. We call homework “cron” and the description is “A task to start cron at system startup”. Now, press “Next”.
In the next section, we get on let's do it. First, Windows wants to know when we want to run the task. Choose the radio button “When the computer starts” and click “Next”.
In the next section, we want “Start a program”. That option is selected by default, therefore click “Next”.
Now, we have to specify the program we want to run, what is WSL. Enter the following in the text entry box “Program / Script”: C:WindowsSystem32wsl.exe
We also need to add some arguments, since all we have done so far is start WSL, but within WSL, we need to tell Ubuntu to start cron. Then, in the frame “Add arguments”, add: sudo /usr/sbin/service cron start
Press “Next” one more time, check the box that says “Open properties dialog when I click Finish” and then click on “Finalize”.
The task is created, but we have to do one last thing to make sure everything works. A new window will open, which shows the summary of the task you created, but it is only configured to run when logged in. We need to choose the radio button that says “Run if user is logged in or not” and then press “To accept”.
Now, Let's test our task in two ways. First, in the main window of Task Scheduler, scroll down until you see the name of your task. If you used the name “cron”, should be at the top of the list. Right-click on the task and select “Run”.
Subsequently, go back to your WSL terminal and type sudo service cron status
, and it should say that cron is running. If that is not the case, check that you have entered everything correctly in the steps above.
If everything worked correctly on the first check, it's time for the big test. Restart your PC and, when I come back, open a WSL terminal and run sudo service cron status
, which should report that cron is now running.
Congratulations! You have taken your first step towards a bigger automated world. With cron running in the background, the cronjobs you set up in WSL will run automatically as scheduled.