how to schedule a script created in VScode (WSL) - Python - scheduled-tasks

I have a script that I want to schedule to run each lets say every 10min.
this script downloads a csv file in this folder.
C:\Users\rsharma\scripts\cylance\external_devices
Script name is external_devices.py and I have created it in visual studio code (WSL).
now I know I can use Crontab and windows scheduler to schedule it but nothing seems to be working.
this is my script path
C:\Users\rsharma\scripts\cylance\External_devices.py
these are the crontab entries I have tried
*/10 * * * * /mnt/c/Users/rsharma/scripts/cylance/External_devices.py
*/10 * * * * /usr/bin/python3 /mnt/c/Users/rsharma/scripts/cylance/External_devices.py
windows scheduler entries -
program - C:\WINDOWS\System32\wsl.exe
argument - /mnt/c/Users/rsharma/scripts/cylance/External_devices.py

Please move the script out of "/mnt/c/Users...." directory and copy to another directory in the C drive. The WSL mount point is only available when the lxss Hyper-V WSL is started so will not be mounted unless VSCode is running. It's now much better to use the WSL2 (Ubuntu 20.4) version which you can download from the Microsoft store and then install into as an extension "VSCode Remote Extensions for WSL" which will mount into "/home/username" and is much much faster and feature rich, especially if you plan to use Docker as it is directly integrated.
Install Windows Python into a directory of your choice for example - c:\Python. You will obviously need to install any libraries that the Unix version uses. There will be a binary
c:\Python\Python3.x\python.exe
That will need to be coded into a batch windows command file (xxx.cmd). To run your script change to the directory the script is saved and run the python command.
Example code for xxx.cmd batch file:
cd c:\scriptdirectory
c:\Python\Python3.x\python.exe External_devices.py
Run this on the command line to ensure it works, then schedule in the Windows Scheduler as appropriate.

Related

Windows subsystem for Linux : Command Not Found Error

I have installed windows subsystem for Linux to run Ubuntu 16.04 on my windows 10 home platform.
I have extracted all required directories to run KSQL on this platform.
Now, when I am trying to run any command after navigating to the bin folder. It's throwing command not found error. I tried to add PATH as well but it's not working.
Please suggest.
There's a typo in your command:
export PATH=$PATH:/opt/kafka/confleuent-5.4.0/bin
Instead of confluent-5.4.0 you misspelled it confleuent-5.4.0.
The easiest way to install Confluent CLI, is by making use of the scripted installation:
Install the Confluent CLI using this script. This command creates a
bin directory in your designated location (<path-to-directory>/bin).
The location must be in your PATH (e.g. /usr/local/bin). On Microsoft
Windows, an appropriate Linux environment may need to be installed in
order to have the curl and sh commands available, such as the Windows
Subsystem for Linux
curl -L https://cnfl.io/cli | sh -s -- -b /<path-to-directory>/bin
Finally, if you run confluent start you can get all services up and running, including KSQL (assuming that you have correct configuration files).
You could just use the path
cd bin
./kafka-topics.sh
Also, all those commands work in CMD / PowerShell as well
If you want to run KSQL, I'd suggest just using Docker

VS Code - Using Code Runner Extension to Run Programs on Virtual Machine (VM)

I develop on Windows using a Linux VM via Vagrant and Virtual Box. I'm trying to figure out how to get the Code Runner extension to run my files on the VM. The biggest hurdle so far is, for a given file, I need to convert from the Windows host path to the Linux guest path.
Background:
The Code Runner extension allows one to map file types to shell commands to run those files. For example,
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt"
tells Code Runner that when I try to run a Java file, it should cd to the directory that contains the file, and compile the file, and then run the compiled file. The mapping from file types to commands is called the code-runner.executorMap which is contained in settings.json. By adding the option
"code-runner.runInTerminal": true
to my settings.json, I can tell Code Runner to run in the integrated terminal. So by simply SSHing into my VM from the integrated terminal via vagrant ssh, I have code runner targeting the virtual machine.
This is where the problem comes in - Code Runner is using my Windows style paths and my Windows file structures as command line arguments to my VM.
For example, suppose my Windows file structure looks like c:\a\b\c\d and my VM has its root in c so that c and d are shared folders. If I want to run a file in d, the command cd $dir will tell my VM to do cd c:\a\b\c\d.
I have thought of workarounds like adding the following to my settings to run python files
"python": "cd \"$(dirname \"$(locate -l1 $fileName)\")\"; python3 $fileName",
This command, which runs on the integrated terminal (the VM) locates and changes to the directory which contains the file which is to be run. It then tells the python3 interpreter to run that file. However, this doesn't always work (e.g. multiple files with the same name), and requires me to update the database that locate depends upon every time I add a file.
There has to be some way to translate my Windows file paths to the paths on the virtual machine (so e.g. c:\a\b\c\d -> /c/d). Perhaps through Vagrant? I would appreciate any help.
I developed a workaround. I would still be interested in a "cleaner" solution.
The workaround is as follows:
Firstly, I wrote a Python script to convert from Windows Paths to paths on my virtual machine. The script takes in the windows path to a file and the file name as arguments.
#pathconverter.py
import sys
windows_path=sys.argv[1]
file_name=sys.argv[2]
path_to_vagrantfile = r"C:\Users\Evan\Google Drive\Development\Vagrantfile"
slashes=path_to_vagrantfile.count("\\")
y=windows_path.split("\\")[slashes:]
linux_path="/vagrant/"+'/'.join(y) + "/" + file_name
print(linux_path)
Thus the following code converts from a windows file location to one on my virtual machine (assuming you saved pathconverter.py at the root of your shared directory, \vagrant :
python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName
Therefore, to run most files of various interpreted languages, I just supply the output of this command as an argument to the interpreter. For example, to run a Python script on my VM automatically, I just add the following line to the code-runner.executorMap:
"python": "python3 \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""
Or for Racket/scheme, I just do:
"scheme": "racket \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""

Run Makefile with crontab

I'm new in Ubuntu and programming.
I'm testing a program that I found on github, to download and import OSM data into postgis.
It works when I run it from terminal (url and name are fake):
make all NAME=dbname URL='http://myurl'
using postgres user.
Now I need to run this command every day.
So I wrote that script:
#!/bin/bash
# go to the directory with Makefile
cd /PCuserhome/directory/to/Makefile/
# run Makefile
make all NAME=dbname URL='http://myurl'
and it works when i run it from terminal.
So I have added it to crontab (of postgres user) in this way:
0,15,30,45 * * * * /PCuserhome/myscript.sh
It create the db but probably fail in running osmosis selection (Osmosis is in the path for all users).
Any idea to solve this? Thank you!
crontab commands are executed only with minimal environment variables, i.e.
PATH=/usr/bin:/bin (on debian anyway),
so if you are relying on programs that are in your $PATH, it will fail.
Consider specifying an absolute path to the osmosis program wherever it's called from.
Alternatively you can change $PATH itself in your script
export PATH="/my/bin:$PATH"
p.s.: you can check the environment by adding a simple cron job
* * * * * env > /tmp/env.txt

Use cygwin to run a batch file and email results

I am new to using cygwin and don't really understand how the scripting of it works. Currently I am running it on Windows 7 and using task scheduler to do this inefficiently.
What I want to do is to run a .bat file already made that runs tests in the cmd line and than take the results of that test and email that people.
Some side notes:
1. It doesn't HAVE to be a batch file, from my reading I think maybe a .sh would be easier to run with bash. Being able to run it on CentOS would be even better, that way others can run if I leave.
2. This needs to run daily. I would like to run the batch file at around 10 am and give it an hour till the emailed results are sent, unless you can trigger the email when the .bat is done.
3. Every time I run this .bat file it saves the results to a .htm file and overwrites it every time the .bat is run.
Thank you
That could be in the crontab for a a centOS server (/etc/crontab)
0 10 * * * user cd /path/ && /bin/bash file.sh >> result_file
Is that what you needed ? Also, you can install Cron as a windows service with cygrunsrv

Perl Network Drive Installation

I need to be able to run a few simple Perl scripts off of an NTFS (win server 2003 R2) drive. Is there any way to install Perl on this network drive (Strawberry or ActiveState, Strawberry preferred for built in cpan module compiler) and be able to run the scripts with whatever real time mounting on machines that do not have have/need a Perl distribution installed?
So far not having much luck with any relevant documentation.
Yes, we do this all the time.
Install Perl on your fileshare. I prefer to do this from the Windows Server hosting the fileshare, but I don't think it's necessary. If you don't have Remote Desktop and Admin access to that server, you'll need 2 PC's, 1 for the install and 1 to test this out. You can't test this on the same PC/server that ran the install.
Run this Windows Registry script on the test PC:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.pl]
#="Perl Script"
[HKEY_CLASSES_ROOT\Perl Script]
#="Perl Script"
[HKEY_CLASSES_ROOT\Perl Script\DefaultIcon]
#="C:\\WINDOWS\\system32\\Icons\\ActivePerl.ico,0"
[HKEY_CLASSES_ROOT\Perl Script\shell]
#="Open"
[HKEY_CLASSES_ROOT\Perl Script\shell\Open]
#="Open"
[HKEY_CLASSES_ROOT\Perl Script\shell\Open\command]
#="\"Z:\\network-folder\\Perl\\bin\\perl.exe\" \"%1\" %*"
[HKEY_CLASSES_ROOT\Perl Script\shellex]
[HKEY_CLASSES_ROOT\Perl Script\shellex\DropHandler]
#="{86C86720-42A0-1069-A2E8-08002B30309D}"
You will need to change this:
"C:\\WINDOWS\\system32\\Icons\\ActivePerl.ico,0"
to a path (preferably on the PC, not the network!) of an icon to go with the .PL files. You can drop those 2 lines from the reg script if you don't want a custom icon.
More importantly, you will need to change this:
"Z:\\network-folder\\Perl\\bin\\perl.exe\"
to the path where you installed Perl.
The DropHandler portion of that script is explained in more detailed in this other post: How do I create drag-and-drop Strawberry Perl programs?
Hope that helps!