i am reading websocket data in javascript and doing further procedures like inserting data in mysql database.
but all this happens when i open .html page containing javascript code in it.
How can i run the javascript program/code all by itself from lets say command line ?
like the php codes/programmes/bot does , invoking from shell and it can keep doing what its been coded to do, without stopping.
so i can set it as a cron job and it will do its thing on time, instead of me opening .html file every time.
This is exactly what node.js is for. Instead of putting a <script> tag in an HTML file, you would just run your JS file ala $ node myscript.js.
Installe nodejs and you can run .js files by
$ node myscript.js
if you want your script to run forever then you can use https://www.npmjs.com/package/forever like this
$ forever start myscript.js
Related
I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
the Php is used to retrive some data(A LIST OF URLS) from DB.
For each URL Obtained, a Java script API is used.
THe result Obj returned from API contains data for each url.
The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
You can't run Javascript in Cronjobs because Javascript is ran by browsers. I think you should take a look at curl in php to call an api instead.
http://www.php.net/manual/en/book.curl.php
You have to split the work: Cron the JS, Cron the PHP. In the middle, deliver one's results to another. Agree with phantomjs usage for JS execution (or casperJS-I prefer). Execute the JS, output to JSON as a file, read from the file using file_get_contents from PHP. And define these actions in two different cron jobs.
You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs.org/
phantomjs is one possibility, see this thread wget + JavaScript?
Otherwise you could run Node.js on your server to execute JavaScript in a CLI type environment but mixing node.js and PHP could become complicated.
you can schedule javascript with cron by using Node.js
I have a python script that gets called from some javascript code, that I have running. And when the python script is finished I want a call back to go to a Java program of mine, I was trying to make a html page and then check with that, but I am running the Java program locally and can't connect to the FTP?
How could this be accomplished?
Thanks
EDIT
Here is how the flow works
Java calls a javascript function in browser on my local machine ->
Javascript calls python with POST Request on my Server ->
I want a callback to Java to know when Python is done ->
So that Java can move on
Do you see know how it works?
If you are running from the command line you could use pipes | which will call the next command in line with the standard out of the first command. As follows:
firstCommand | secondCommand
So if your Python script writes to standard out you could invoke it from the command line, and have the Java execute from the command line also as the second command receiving it's input from standard in.
So I have a simple php file with the following code in it:
$contents = file_get_contents("path/to/file.html");
var_dump($contents);
Within the html file I have a few scripts that run and returns data to console.log. But, when i execute the php command (linux virtual machine) and run the file it just returns the static html content. Whereas when I open the file from the browser, it executes all the javascript files and returns the expected html output with data in console.log. To be specific, the html file executes a jasmine test suite.
So, I would like to know if there is any way to print out the results from the console.log or actually execute the javascript files through a php script and a linux virtual machine. Also, the main purpose is to execute the test via Bamboo, a continuous integration server.
I have tried phantomjs but it results in other errors and I would prefer not to use it.
Simple saying, no, you cannot do that.
The reason is that you need browser to execute javascript, and PHP just does not have browser capable of doing that as efficient as you want. There is CURL, but it cannot run javascript.
I have three files in the same directory. One is a python script, which takes argumenet. One is a html page with javascript. And the last one is a source .wav file.
./myfolder/sound_manipulation.py
./myfolder/volume_slider.html
./myfolder/the_song.wav
The sound_manipulation.py file can be executed like:
python sound_manipulation.py -v 50
and it generates a new wav file, new_song.wav, based on the_song.wav, but only has 50% of the original volume level.
On the other hand, the volume_slider.html contains a slider goes from 0 to 100%, and a button calling an onclick javascript function, update_vol();
So far, the update_vol() alerts the value of the slider, and that's all.
function update_vol() {
var vol = document.getElementById('vol_slider').value;
alert(vol);
}
But I want the update_vol() to actually execute the python script using the vol.
How can I make that happen?
Also, when the "python sound_manipulation.py -v 50" is executed, how can I return the location of the new_song.wav back to volume_slider.html?
Please help. Thanks!
the simplest and crudest one-time cgi script might solve your problem.
set up a cgi script/environment to just get volume value from user / then use subprocess module to process the .wav file and send it back to user. if you need anything more than that, build your own web app.
import cgi
import subprocess
import sys
form = cgi.FieldStorage()
volume = form.getfirst('volume') #read from form 'volume'
subprocess.call(['python', 'sound_manipulation.py', '-v', volume])
with open('new_song.wav', 'rb') as wav_file:
print("Content-Type: audio/wav\n")
sys.stdout.write(wav_file.read())
Hm, well, I guess you're out of luck. Browser scripts can't execute anything (so no Python script) on your system. As I'm sure you can imagine, this could present a huge security risk -- that's exactly how much trouble Microsoft's ActiveX got into.
I'm assuming you want to create a graphical interface for that script, so alternatively you could :
Do a JavaScript equivalent of your Python script if you absolutely need HTML
Or create an interface in QT, GTK or the like. Python can easily do that, by the way!
I'm having a problem with a small batch script that I'm writing. The point of the batch script is to run a Javascript file that converts an XML file to a csv file and then run a Python script which will analyze the just created csv file and create another csv file.
The batch script is below.
start XML-CSV_Converter.js
python CSV_ANALYZER.py
exit
I didn't write the XML-CSV converter;
It can be found here. (http://gotochriswest.com/blog/2011/05/05/excel-batch-convert-xls-to-csv/) The only thing I changed was I removed all of the alerts and input prompts so it doesn't wait on any user input. Simply put, all it does it look at every XML file in the current directory and produces a csv file in the same directory.
Whenever I run the batch script, I keep getting an IO error in my Python script because even though it can see the created file, it isn't able to open the file.
The exact error is:
"IOError: [Errno 2] No such file or directory: 'NAME_OF_FILE.csv'"
The part of the Python script which is causing errors is listed below.
dirList = os.listdir("C:\FOLDER")
for fname in dirList:
if fname.find(".csv") != -1:
inputFile = open(fname,'r') <---- Script halts here
Anybody know what could be causing the file not being opened in the Python script?
If I run the JavaScript file manually, and then the Python script manually, it works perfectly. But when I try to chain them together in a batch file it breaks. I would appreciate any and all ideas!
Thanks in advance!
The start command launches a program (or, in your case, a file) and immediately returns, without waiting for the program to exit. This means that when Python tries to open the file, the JS script is probably still running and has the file open exclusively for writing.
You should run the JS file by directly calling the Windows Scripting Host (cscript.exe). Just replace the start command with:
cscript //nologo XML-CSV_Converter.js
This will ensure that when the Python command is run, the JS script will have completed its job and safely terminated.
You forgot to add the folder name to the file name in the open() call. listdir() returns only the file names, so unless its path argument matches the current directory, you'll have to add it to the returned file names to properly identify them.
As a side note, you should also avoid using backward slashes in normal strings. In this case "C:\FOLDER" happens to work, but "C:\folder" wouldn't, because \f would be converted to \x0c (the linefeed character). You should use either a raw string (r'C:\FOLDER' which doesn't convert escaped characters), an escaped backslash ('C:\\FOLDER'), or the more portable 'C:/FOLDER' (on Windows '/' is the alternative path separator).
Also a simpler way to check for the extension is to test whether the name ends with it (the s.find(t) == -1 is not very Pythonic). All in all, the modified code could look something like this:
folder = "C:/FOLDER"
for fname in os.listdir(folder):
if fname.endswith(".csv"):
inputFile = open(os.path.join(folder, fname),'r')
The root cause is "python CSV_ANALYZER.py" got executed while the XML-CSV_Converter.js was not finished.
By type "start XML-CSV_Converter.js", it won't wait for this command to finish before executing the next command.
If you have installed js interpreter, you can remove the `start' in the first line, an then it should work.