Thursday, October 1, 2020

Running a Cygwin Bash Script in Task Scheduler

 Familiar readers may know the context. I have a textfile full of famous/other quotations, one per line. I deployed a freeware product Qliner Quotes which provided a way of generating HTML/other format signature formats which could be used to select a random quote from my custom or other quote files snd to attach the filled boilerplate to the bottom of my Thunderbird emails. I later basically wrote a bash script which could separately fill the html boilerplate file and also would read my latest quote html file into a browser tab and use it to headline my latest daily miscellany political post. I ran the bash script in Windows' Ubuntu interface, later putting it into Windows Task Scheduler to generate a new quote html file every 30 minutes. No major problems but it helped set my expectations into performance.

I'm not sure exactly what happened but probably recent Windows patching caused a functionality problem. I know there was a recent Linux-related patch, but my issue predated that implementation. I recall 2 or 3 weeks back updating my Ubuntu stack. So my scheduled task was no longer working, and I decided to install or reinstall cygwin. (I may have last reinstalled cygwin on an earlier PC.) There were various practical uses for using cygwin, like dealing with naming conflicts in merging file directories, attaching timestamps to certain files, etc. Yes, there are freeware renaming utilities, etc.; Windows has powershell, etc. But there are things I can do in seconds based on writing Unix scripts for over 20 years. I had to slightly tweak the bash script I had written for Windows linux subsystem but no major effort.

What I didn't expect were issues implementing my cygwin shell script into Task Scheduler. There were minor tweaks (like dealing with embedded spaces in directory paths, the arguments to the bash command to use, i.e., -c -l, specification of the target script reference, etc.) But one of the most irritating things was how long it took for the job to run out of the scheduler, e.g., up to an hour or longer vs. several seconds in a regular bash session. While I could launch a bash session if and when I wanted to rotate quotation html files used in emails and blog posts, I don't recall running into these kinds of delays when I was running a similar script in Task Scheduler with the Windows integration of Ubuntu, and I didn't want to  launch a session just to run a script.

I finally got the performance issue resolved using a tip from the third source below: export the task xml file from Task Scheduler, edit the priority from 7 to 4, drop the old task in Scheduler, and import the edited xml file back into Task Scheduler. Now if and when my bash session launches every half hour, the session completes within several seconds.

Note: I found the following sources useful in troubleshooting my issues: 1, 2, 3.