I want to create a BATCH File which is like a Cron Job. It runs in a loop and should call a PHP File which is on my server. I don't want my BATCH file to open hundrets of Web Browser Windows.
Any idea how to get this working?
Thanks
in your batch file, you can call pathToYourPhpInstallation/php yourphpfile.php the same way you can do it in your command line / terminal.
For instance, if by BATCH you mean a .bat file on your windows system, it could look like this:
dosomething.bat:
c:
cd C:\www\myApp\backend
delete *.tmp
"C:\php\php.exe" ajax-backend.php
If you have a mor complex site that you need to start or do something with, some of the common test frameworks (i.e. selenium) are able, even thou not intended to do so.
Related
I am new to this topic and I have developed a .php file, which has an html code inside, which makes a series of functions through javascript and this through ajax, executes a php code, which registers data in the database, the code works correctly, when opening the url, internally it does the whole process.
But what I want now is to schedule this code to be executed every day at a certain time, but when doing it directly with my hosting, it returns the html code and does not execute it. I also used cron-job.org and it returns the same, it returns all the html code in text format, but it does not execute the functions and others that I have in javascript and php.
I would appreciate if you could help me.
What I would do, create a new PHP file that contains all function calls that normally would be executed through AJAX requests. Then schedule this new PHP file in a cronjob.
You could also script the whole thing as if you were a visitor of the page through a headless browser, for example PhantomJS or Selenium. And then schedule that.
It is not possible to run js on server side using php, but if your script can run on web-server you can use CURL to run it as a web-page. You can also run it on localhost. Set up CURL and the set cron job like this curl https://localhost/script.php. Another way to run script on the web-server is wget, something like this: wget https://localhost/script.php
I'm making a discord bot with discord.js, but my reset command isn't working.
//!reload
if(command === `${botsettings.prefix}reload`) {
console.clear();
bot.destroy()
bot.login(botsettings.token);
message.channel.send("Reloaded");
return;
}
It doesn't give any error; it restarts and goes back on but the commands don't update!
I tried so many different things I found, but it doesn't work.
I only have one file for all commands.
If you use a nodemon system and a local batch file it will automatically reload the file for every change saved in it, and will output the new file changes.
If you need more help on this, watch a few videos from TheSourceCode on YouTube and you'll see in around 3-4 episodes on him using nodemon.
If you end up hosting it on Heroku, which is frowned upon, but I do, it will automatically reload for every change in your GitHub repository, just make sure to replace your token with process.env.token and make sure you have it set on your Heroku.
So i am guessing you have your various files set out (config etc) and one central bot.js file, and you load the bot by using something like
node bot.js
on your host machine, and obviously part of your .js file sets up the bot something like this:
const bot = new Discord.Client();
From what I can tell, your
bot.destroy()
and
bot.login(botsettings.token);
is just refreshing the const 'bot', but what it is not doing is reloading your bot.js file (with the updated commands and code).
What you would need to do, is have it set up to run a batch file or something on your host machine that terminates the entire process bot.js, and then restarts it. As this would then use the new and updated bot.js file.
The only problem is I am still figuring out how to run a batch file from my JS file, as understandably for security that feature isn't built in (other wise most websites that use JS would be vunerable to getting it to run things like format C:\)
I imagine it will involve using WSH in my JS, and I will update here if I do get it going.
I hope this was clear? let me know if you still have a question :)
In the code you provided, it seems like your only refreshing the const 'bot' instead of reloading the bot.js file.
I recommend that you use a batch file.
client.destroy();
client.login();
That's not possible since there's a dead socket.
Instead, you're able to make a Shell script and have a background Daemon service the needs that you want.
i have a c++ file which reads values from a sensor and I want to display those values on a website dynamically. So Im looking for a way to pass these values(integers) from my cpp file to an javascript which displays them on the site.
My first, simple try was to write the values into a js file as variables every second from my cpp script. The Js then uses this file as a source and displays its variables on the site:
cpp:
fprintf(file, "var mx=%d, my=%d, mz=%d, ax=%d, ay=%d, az=%d, gx=%d, gy=%d, gz=%d;\n",
imu.raw_m[0], imu.raw_m[1], imu.raw_m[2], // M = Magnetometer
imu.raw_a[0], imu.raw_a[1], imu.raw_a[2], // A = Accelerometer
imu.raw_g[0], imu.raw_g[1], imu.raw_g[2] // G = Gyroscope
);
html/js:
<script src="./imu.js" type="text/javascript"></script>
The Problem now is of course, that I need to refresh the page all the time, because the imu.js file is cached by the website.
I'd rather have a way to directly pass to integers from the cpp file to the js script. I read something about json or Googles V8 script. But I'd like to hear your suggestions first.
By the way, Im running this on a raspi, if this is important.
Thanks for your help
EDIT:
I'm goning to try it with a mysql database, in which my cpp file writes the data from the sensor with Connector/c++ from http://dev.mysql.com/doc/connector-cpp/en/ and my website reads them.
You could compile your C++ code into a Node.js plugin, you can then register a JavaScript function with your plugin which the C++ calls when it updates the value. That way you can pass values directly from C++ into Javascript in a managed and controlled way.
Node.js has the added benefit of being able to host your webpage and do all the Websocket and HTTP stuff that can be a pain in C++.
You do not have to refresh if your script is smart about how to access the data file! In case you do have a webserver at hand: Take care that your data file is accessible by your webserver and then let your script request the file via ajax (link to w3schools)
I'm doing something similar on a BeagleBone Black. With websocketd you can turn pretty much any program into a websocket endpoint and then send data via stdin and stdout commands. This would be a particularly good solution for you since websockets are designed to handle information that's constantly changing.
I have a Java script that executes a k-shell which calls a program that writes file A. Thereafter, the same Java script parses output file A. No other process read or writes file A.
The problem is that file A is not always ready to be parsed in time, which I assume is because the file system is still finishing its job even after the program & calling shell return. This only occurs when there are a lot of processes running.
lsof does not seem to work on my system. If I open a file with an editor, lsof shows me the editor process, but if I pass the filename to lsof I get:
lsof: no file use located: filename
I don't think a fileLock will work, as I would have to unlock it before either returning from the program or the k-shell, by which time the file still might not have been closed completely.
My thought was to write a short script that would rename (mv) the file, and then have the parser parse the renamed file, my understanding being that a file cannot be renamed until it is completely written. I put the mv command in a while loop that checks to see if the stdOut from the mv command has anything in it, theory being that if it is empty, we're good to go.
#! /bin/ksh
# Move a file. The idea is that this script will not return until the mv is
# complete. If mv is successful, mv.out.txt should be empty.
mv $1/camber.out $1/camber.out.copy > mv.out.txt
while [[ -s mv.out.txt ]] ;
do
echo "mv did not happen"
mv $1/camber.out $1/camber.out.rename > mv.out.txt
done
echo "mv should have taken place"
exit
I have yet to see an instance where the mv was held up. Any comments, suggestions, hints, or insults would be greatly appreciated. Thanks in advance to all.
I lack the ability to comment, so an answer this shall be. Regardless, what it sounds like is a problem with asynchronous execution- the java program calls the bash script, which is handled as a separate program by the OS and thus runs concurrently with the java program. For everything to run correctly, you just need to make sure that the bash script is run synchronously- that is, the script must finish before Java continues. I believe this SO answer related to blocking should do what you need. The solution is this:
ProcessBuilder pb = new ProcessBuilder("myscript.sh");
Process p = pb.start(); // Start the process.
p.waitFor(); // Wait for the process to finish.
System.out.println("Script executed successfully");
That should force the Java program to sleep until the bash script is finished.
The same logic may need to be applied to the k-shell, as you pointed out in your comment below. You can use the wait command, which takes a job as an optional parameter, to wait on a process (if you do not pass in a specific job, the process will wait for all child processes to finish).
again. I'm making a PL/SQL generated HTML5 web page. It's running a Oracle 10g XE server. Okay, now when the setup is clear, my problem - I need to include a Java Script file in the page. Simply
HTP.P('<script type="text/javascript" src="js/ScriptFileName.js"></script>');
Doesn't work of course. So i created a folder object and granted read,write to PUBLIC. Then changed the string to match the newly created object, instead of path. Still doesn't work. I know, i can write
HTP.P(<script type="text/javascript"> MY JAVA SCRIPT HERE</script>);
And i've done so with other scripts(Even had to write CSS this way). But this time this will not work. Reason being - the JavaScript i'm trying to run was normalized(or rather unnormalized), so it's written all in one line. And there is a lot of it too. I tried to reverse it to normal, but faild many a time.
So, I went online and searched for a solution. Found one. It seem's that this include should go not to the page, but to server config. Makes sense, since PL/SQL is server sided. But when i went looking for the usual httpd.conf, it's nowhere to be found in Database directory.So i went online again, result - NOT A WORD OF WHERE THE HELL ARE HTTP SERVER CONFIGS IN 10gXE IN ANY ORACLE MANUALS. Searched some forums - exactly 1 person asked where httpd.conf in XE is, and didn't get an answer. Please, help. I'm desperate.
P.S. I don't use APEX. I don't get that mumbo-jumbo. So i write in Notepad and run the scripts in SQL line.
Firstly, XE has its own built in HTTP server called the 'Embedded PL/SQL Gateway' or EPG. But you don't HAVE to use that. You can use an Oracle HTTP Server with the mod_plsql plugin. Or you can use the Apex listener.
The question is on what server is "ScriptFileName.js" ?
Is it a flat file on the database server ? If so, you'll need to use the Oracle HTTP Server (or Apache or similar) to serve it. The database is pretty much unaware of files on its server and the EPG can't deliver them. [At least not in any practical sense, you could do weird things with chicken entrails and UTL_FILE, but you don't want to go there.]
Is it a file stored in the database ? That sounds exotic, but it is pretty much how all the CSS, images etc are served up through the EPG. The best explanation on how to get files in and out of there is by Dietmar
Is it a file stored on a separate machine ? Often the best answer. The "src=" directive will be read by the end users browser. That will do an HTTP get to the URL. It doesn't have to be a URL on the same domain/host as the rest of the page.