I've been attempting to update all packages in a WSL distro by scheduling an event in Windows Task Scheduler. I'm complicating things a little by having the task also change the message of the day (motd) to display when the last updates happened.
I have a task scheduled to run this command:
wsl --distribution Arch -u root sh /mnt/d/sam8s/Arch/update.sh
Upon running the same thing in a Windows command prompt, things work great-- all packages update, and my motd file is changed to be current. When run from task scheduler, the command returns 0x1 and no update to the motd is made.
Here are the contents of update.sh (please forgive my bad use of regexes!):
pacman -Syu
cd /mnt/d/sam8s/Arch
CUR_DATE=$(date +"%Y-%m-%d %T")
sed -E "s/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/${CUR_DATE}/g" motd > motd.bak && mv motd.bak motd
exit
Which removes an old date (YYYY-MM-DD HH:MM:SS) from the file and adds the new one, which is formatted the same way.
Any thoughts on why this might work from command prompt but not task scheduler?
Looks like the answer was removing the option "Run whether the user is logged in or not". I'll continue looking into why this might be the case, but simply switching over to only switching to run only when the user is logged in is enough to resolve the issue.