Crontab never executes in Windows Subsystem for Linux [closed]
Asked Answered
X

5

14

I set up some cronjobs a while back using crontab -e. My crontab includes the following line:

* * * * * /usr/bin/touch /home/blah/MADEBYCRON

It's been weeks since I did this. I have never seen /home/blah/MADEBYCRON. I set permissions on my home directory so it should be able to create files in this directory, so why does this file never exist?

/var/log/syslog does not exist.

Xanthus answered 17/2, 2020 at 6:22 Comment(4)
please share the syslog: grep CRON /var/log/syslogSpacial
Running cron on WSL seems to require some crazy hacks. There is no indication in your question that you have any such hacks in place. The least wacky I saw from quick google results amounted to turning WSL into a Windows service. (Not particularly precise I'm afraid; my recommended solution is always to ditch Windows.)Subjoin
Did you manage to get this to work? I have an exact similar problem; I start atd and cron both manually on every restart, but the cron jobs never execute. In my case I noticed when I was upgrading some packages, the cron started working. However on next reboot it was back to the same situation.Latvina
Hi, I found this scottiestech.info/2018/08/07/… but its a kinda hacking and requires very very big effort .. :/Principle
T
61

Ensure that the cron service is running. I use WSL with cron every day for my local backups using rsync so this should work.

Use which cron to check its installed, mine says /usr/sbin/cron.

Use crontab -l to list your configured jobs.

Use ps aux | grep cron to look see if cron is running, you should see /usr/sbin/cron if it is.

Use service cron status to check if the service is started.

Use sudo service cron start to start the cron service if it is not running.

Triviality answered 17/2, 2020 at 12:36 Comment(7)
so after running the start command there is still no cron process nor does the status check say anything useful.Triviality
This worked for me, figured the service hadn't been startedSurtax
@Triviality That worked for me, but now it doesn't log anything. The file /var/log/cron.log is empty file) and /var/log/syslog has no entries about cron.Maunsell
I solved the log issue updating the conf /etc/rsyslog.d/50-default.conf, see serverfault.com/a/470938/321667 and then restarting rsyslog sudo service rsyslog restartMaunsell
Use ps aux | grep cron to look see if cron is running. And if it isn't running, then go to #41281612. This worked for me with Pengwin (Ubuntu based) in WSL 2.Ancilla
@Triviality Will the cron job still run if your computer reboots over vacation or must you log on and start WSL for these jobs to be started? Asking to assess feasibility of using WSL as a potential external VM replacement.Version
@Version WSL needs to be running, which it will not be under normal circumstances after a fresh reboot.Triviality
S
3

Since WSL is not starting services on startup you also need to start rsyslog before starting cron if you want to see the cron logs in /var/log/syslog:

sudo service rsyslog start
sudo service cron start

Then view logs

grep -i cron /var/log/syslog

syslog only has the information which script was run and when. If you want to see the output of the script you need to redirect it to a file, e.g. like this:

* * * * * /usr/bin/touch /home/blah/MADEBYCRON >> /home/blah/cron_output.log 2>&1
Sakovich answered 22/8, 2022 at 11:57 Comment(0)
P
2

Under recent WSL releases, you can enable Systemd support as mentioned in this Community Wiki answer. With Systemd, the cron service (unit) should automatically be started when you start your WSL distribution.

Note that Systemd is not required under WSL, and may add additional, unneeded overhead. Consider using one of the other existing answers if you don't need Systemd support.

Parkland answered 17/2, 2020 at 6:22 Comment(0)
D
1

You need to start the cron service. Services do note start automaticaly on WSL as ist does not use systemd. The easyest way to do this is to add the following line to your .bashrc:

service cron status || sudo service cron start

On the first start you will need to enter the sudo password and you will see somthing like this

start cron service

Drawbridge answered 8/7, 2022 at 15:14 Comment(0)
P
1

Recent WSL releases also offer the possibility to execute custom commands upon booting the WSL image. See https://superuser.com/a/1685207/7833 for an details and examples.

In short, to make cron be started automatically when starting the WSL image, create or edit (as sudo) inside your running WSL Linux the /etc/wsl.conf file.

Then add or extend the [boot] section with following line that automatically starts cron when your image starts.

[boot]
command="service cron start"

This should do the trick and give you a running cron in your WSL image.

If you already have an existing boot/command config be aware, that you have tho chain commands in that config to make it work, like eg.:

[boot]
command="<existing commands> ; service cron start"
Positron answered 29/4 at 8:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.