Run a php file contain javascript from cron job - javascript

I tried many online methods people have suggested and still not reached a solution
i need to run a php file that contain javascript from cronjob
For now, I can not do it through PHP
If anyone has an idea I'd love to read

You will probably have to trigger it through a browser. Get a computer to open the URL via the cron tab (or some other timed device... I know you can do it through Automator/AppleScript on a Mac).

How I do it: I split the job in to tasks.
Task 1: Execute Javascript
Javascript would require a browser to run. In this respect you can try casperjs/phantomjs. You can create a a cronjob for this easily. Please check: Running a casper.js script from cron
Task 2: Deliver results to PHP
I assume that you take the results of the JS and use it in the PHP. So JSON can do the job. At the end of the first task output to a file. In this task, read it using file_get_contents.
Task 3: PHP Cron
It is obvious :)
Sure that there are better, easier and simpler solutions. I am a newbie also here, but this method all my problems for a similar work.

Related

How to compile Javascript console application locally?

I am a beginner in Javascript, I decided to practice Javascript by problem solving using it, I found an online judge that accepts Javascript V8 4.8.0 code.
So, I searched online to get that version of Javascript V8 on my machine, but I couldn't find any easy way, All the pages were explaining how to build it, and it seems to be a process that I don't need to go through.
Is there an easy way to compile and run command line apps written in Javascript on my machine?
Note: I don't want to use node.js because I tried using it's I/O and
as a beginner I think it is complex in some way.
Update: I found that package manager pbox.me which provides a version of V8 JavaScript Engine and I managed to install it.
Yet another problem appeared: whenever I try to run a js file writing d8 myfile.js in command line nothing happens as if it is an empty program, knowing that I tryied to d8.exe file and it is working, and I made sure the PATH is inserted in the environment variables.
What am I doing wrong?
The easiest way to get started with JavaScript is probably to use it in a browser. You can type simple things directly into the browser's JavaScript console (check the menu); or you can embed your code in a simple HTML document.
If you want, you can even pretty easily implement the readline()/print() functions, so you can pretend to be doing stdin/stdout based I/O: just read from an array of strings, and send output to console.log (or create DOM nodes if you want to be fancy and/or learn how to generate dynamic website content by hand).
Side note: V8 4.8 is severely outdated, don't use it to execute code you haven't written yourself.

How to I turn a python script into a web app?

Sorry for the noob question, but I created a script that web scrapes data off our sccm server and compares it to programs that need to be pushed to hostnames.
My script works perfectly when I run it in pycharm, but I would now like to make this program available to the other guys on my team via webpage, instead of them needing to borrow my computer everytime, LOL!
Is there a way I can use a javascript front end to let them enter their username, password and serial number, and than based off that info run my script?
I'm not a programmer to be honest so I apologize if this seems like a straightforward thing.
This would be difficult for someone to just 'answer' but maybe a rough outline could help?
I'm certainly no professional but this is the way I might approach the issue.
create HTML/CSS/JS front end page with a form
upon clicking the 'submit' button have the JS take form data and export it into a JSON (just did a quick search on 'execute python script on button click' and things like that to find some implementations of the idea)
pass the JSON from the page onto the server and have the python script open up the JSON for the arguments it needs to use
python 'wraps' the results up into a JSON file and gives it back to the front end where you have another JS script that unpacks the JSON file and gives it to the user requesting the info. Alternatively, you could have Python put the data into some sort of excel sheet or csv or whatever and have python email the results to the user.
Once all done you'd simply have an instance of Python and all the appropriate libraries sitting on the server ready to use. Pycharm is simply an editor for code. You could theoretically install python itself, and the appropriate libraries needed for your code and just give your folks the .py file itself and have them run the script out of CMD/Terminal and achieve the same result.
Like I say, no professional by any means, and I'm sure I skipped a couple steps but that's a general outline. Obviously in the forms you'd wanna have validation and such to make sure the appropriate data gets used to avoid errors.

Saving javascript script output to file on server

So far Gurus here helped me a lot so far and I am back again with my little queries again.
I have a js that is being called from a webserver through a internal site I am taking care of. js has lots of very helpful debugging comments through
console.log($output)
that are being displayed on the console. I was wondering if there is any way i can redirect these comments to a log file on server.
I know it is hard but not impossible... I thought of doing something like
> exec 1>>${LOG_FILE}
but then it means it will get outputs from every file will be going to that file.
I only need the output from one file... is it possible?? plus do I need to run it through cron? because I don't have permission to change anything in js itself.
-Thanks

Multiple javascripts solution

I Have created a flatfile based cms. PHP and jquery mostly. It is very dynamic and easy in use. I have 3 javascript includes for juery and other functions. This is for the main cms files. So that is quite allright.
But i have written multiple plugins/addons for he cms, also jquery and php, guestbook, comments, rating system, album galleries, site search,.... The problem is that each plugin has javascripts included. And i need to include all scripts in the head part of my main cms in order for the plugin to work on the cms. Now all javascripts get loaded every time the page reloads or if u click a link. That gives many http requests wich slows the cms down. About 15 javascrip files are included in the head now
Is here a way i can load only the needed javascript files and not all of them with a function of some kind.
i tried to compress all the javascript into one file, but that gives errors...
I hope my question is clear in my bad english :)
thx for any response
The tool you are looking for is called grunt.
http://gruntjs.com/
You have over 2000 packages for doing many things and one of them in concatenation and minification.
grunt-contrib-cssmin
grunt-contrib-uglify
grunt-contrib-concat
the list goes on an on, but check 'em all here.
https://github.com/gruntjs/grunt-contrib
grunt is a little confusing the first time you see it, but there are heaps of resources for it and also heaps of stackoverflow examples.
Once you go grunt you never go back!
I'd think you can merge all javascript files and then minify it. are you using double function names or do you have javascript code outside of functions? maybe that is what is throwing up your errors.
would leave you with only one request to retrieve all your javascript functions , and you would only need to minify / compress one file.

At what stage do you compress/minimize javascript?

When building, or "on the fly" (perhaps with caching) when the users request pages.
And what are the dis/advantages of each.
When the site moves from dev to the live server.
I always have an uncompressed version of the JS on the dev server and a minimized version on the live server.
The advantage of that is when developing i can run into JS trouble and fix it very simply, but i need to run each changed script through a minimizer, but for me its not that much.
When building or deploying to a stage environment is a good time to compress javascript. That way you will have a chance to test it in the stage environment and catch any errors that could occur.
Occasionally, there are errors that do come up when compressing. You may want to include a command-line version of jslint that runs before compressing, to make sure that the js passes. That will minimize, but not eliminate, all compression errors.
I'd imagine that on-the-fly would be an unnesessary unless you were adding dynamic data to the JavaScript (in which case there are better ways around it). IT's just a needless expenditure that will only slow down the page load.
Personally, I'd do so when deploying/building the app, it's a one-time thing really.
I'd say you have the js files as you'd code them in source control, when you kick off an automated build that's when, as part of your build script, it runs all the javascript files through a compressor. This way when you deploy it to a test / staging environment you've got the latest script but also compressed for performance testing and as they would be once they go to production.
I agree that on-the-fly is probably not really necessary (and eats up some cpu cycles) if the JS does not change.
There might be some middleware involved though which can check if the JS has changed and compress it only if requested (and maybe even group various JS files into one resulting one).
A good thing when deploying might also be to add some time stamp or random string as parameter to the JS link (e.g. .../scripts.js?t=cdkjnsccsds7sc8cshcsjhbcs). That way when the JS changes you use a different string and there will be no caching problems because it's a new URL. Same for CSS.

Categories

Resources