Cron Jobs Fail to Trigger Node.js Script on External Server - javascript

I have a node.js application on a Windows Server and a series of cron jobs setup via cPanel on an external CENTOS server. The cron jobs all target the URL of my node.js application using wget. I confirmed that the jobs are running according to the cron logs in WHM, but they are not causing the script on the other server to run as they should.
The format of the cron jobs are like this:
wget -q -O - "http://example.com/app?&param=1&param=2" >/dev/null 2>&1
The result of the cron job should be the execution of the script on the other server. That script uses Puppeteer to scrape web pages, take screenshots, parse the results into RSS feeds, and create rss formatted XML files on the file system. Despite the cron jobs firing and targeting the right URLs, I have yet to see a single cron job result in a new image or xml file being created.
This leads me to wonder if there is something I don't know about when it comes to cPanel, cron jobs and javascript. I'm thinking maybe they won't work with URLs leading to javascript files, but if that were true then wouldn't I have the same problem when executing the same commands via SSH? I just tried one using the WHM terminal and it triggered the desired result.

I think the issues was due to the firewall on the IIS server. I added the remote IP address to the whitelist and now the cron jobs work.

Related

Live console input/output for a gameserver using JavaScript or PHP

General information: I have a Minecraft server running in the basement for some friends that is running for a couple months now. We were using TeamViewer to input commands into the console and view possible errors while the server was running. The console of a Minecraft server basically shows you a log of events with timestamps as well as a command input line at the bottom that enables you to execute in-game commands with administrator rights.
The console is started via a batch file:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui
goto startup
The spigot.jar file is the file that basically runs the server.
Because we manage multiple servers and the TeamViewer process is quite insecure and slow, i wanted to find other ways to access the console.
Target: I want to create a HTML webinterface that uses JavaScript or PHP to communicate with the server console. It should be able to show the live console log as well as being able to type commands in the browser that are then executed in the commandline on the server.
Problems:
Is there any way to send a JavaScript/PHP input to the console so that I can execute commands from a webinterface?
Can I save the live console output to a .txt file that the JavaScript/PHP script can then display live on the webinterface?
I would definitely recommend moving your setup outside of a Windows environment, as you're going to have a much easier time hosting a PHP server, etc. in a Linux environment.
Admittedly, I'm not familiar with MineCraft, but it looks like the batch file you have is effectively just launching a Java JAR which can be done from any computer with the JRE installed. Java console apps read input on the STDIN of a process and output on STDOUT for any application-generated output. This is incredibly easy to pickup in a Ubuntu environment.
For example, let's say you install the standard LAMP stack on a fresh Ubuntu install. You'll have PHP for running your server-side process, and Apache for hosting any web interface you decide to build. Keeping a focus on frontend vs. backend, I'd probably try to setup an API in PHP using Slim or another lightweight framework. In the API endpoint you use to start your server, it would look something like this:
exex('screen -d -m -S ServerOne');
exec('screen -S ServerOne -p 0 -X stuff "java -Xmx%v1%G -jar spigot.jar nogui > /some_path_to_log^M"');
The first exec() will create a screen session named "ServerOne" inside of which your server process can run. The second exec() will send the command needed to start your server process inside of the screen session. Effectively, this will run your server asynchronously to your PHP script and write the output to the specified path. Knowing where to expect the output, in the API endpoint you use to get your log, you'd have something like:
$logContent = file_get_content('/some_path_to_log');
Finally, in the API endpoint you use to write commands to your server, you would have something that looks like:
exec('screen -S ServerOne -p 0 -X stuff "<your_command_here>^M"');
This will write to the STDIN stream of your server's Java process which should, in turn, be interpreted as if you were actually typing at the keyboard of the console itself.
That's the basics of it, or at least where I'd get started. There are some pretty cool things that you could do with WebSockets to open a live I/O session for your server's console, but it'd be a bit more involved than this post. If you're interested, check out Ratchet for PHP or ws for Node.js.
Consider this ServerFault post for more information on sending input to screen sessions.
I suppose you could show the log on a website like this:
first gameserver script:
:startup
#echo off
set v1=6
cls
Java -Xmx%v1%G -jar spigot.jar nogui > log.txt
fileup log.txt
goto startup
second gameserver script(called fileup.bat){replace myusername, mypassword and servername.com with the credentials if your php server}:
#echo off
echo user MyUserName> ftpcmd.dat
echo MyPassword>> ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat SERVERNAME.COM
del ftpcmd.dat
php-server side script:
<?php
$filename = "log.txt";
$fp = fopen($filename, "r");
$content = fread($fp, filesize($filename));
$lines = explode("\n", $content);
fclose($fp);
print_r($lines);
?>
by the way: I have not tested any of these scripts and they might not work, I just based this on my knowledge of php and batch. Also, the second batch script needs to be in the same directory as the first batch script and you do not have to run the second batch script
Note:
I am an experienced PHP user but I do not know very much about batch(I use linux) but I suppose this will work and I have used batch very often in the past. Also I recommend using the PHP script on an external hosted php server from 000webhost.com or infinityfree.net because they are free and save you a lot of time + they have all the needed ftp things preconfigured.
Please let me know if this worked

Downloading text file using curl in HTML

I am trying to download text file from server to local directory.
If I execute following curl command it copies myfile.txt from server and saves in same directory as newfile.txt.:
curl -o newfile.txt http://myserverip/myfile.txt
I want to automate this by running the command from javascript while loading the webpage.
For example if I open an html page (which runs a javascript) like http://myserverip/getnewfile.html in the browser the myfile.txt from the server should be copied to newfile.txt in the loacl directory.
Can anyone help me to write javascript to execute the curl command?
Please note that the server in local area network and router is configured to allow connections only from white-listed mac ids of local machines so there is no any authentication required to connect to server.
You can't run local command-line commands from JavaScript, because that would be a massive security vulnerability. The tool you want to use for this is either window.open (to open a tab with the file in order to cause the browser to download it) or the fetch API (to retrieve the file for use in JavaScript).

Downloading remote file using nodejs

Here is my problem... I have a node server that multiple node terminals(raspberry pi) connect to. These node terminals run a series of jobs and some of them generate files. The files are not saved in the terminal but in a mysql blob. Now these terminals are managed through an interface in the server (a CRM webpage). I manage them using socket.io and there is also redis available. Through socket.io I can tell the terminal what file I want, but the problem I'm facing is getting the file to the requesting browser client. I can identify the browser via the socket id but I am not sure as to how I am going to serve that file. Any help or suggestion would be great. Note: Im not using any JS or nodejs frameworks.

Can programs be written with JavaScript that run Windows 8 terminal commands when a gui button is clicked?

I know Windows 8 'apps' can be developed using web technologies but I haven't been able to find out if terminal commands can be run in the background using web technologies as an interface. I basically have mongoDB on my computer and it takes two terminal windows open to run it. I thought it might be a neat project to see if I could write a little app that is nothing more than a button that launches both commands behind the scenes saving me the hassle of going to the directories and running the commands manually for both terminal windows.
If you plan to launch apps via server-side JavaScript (e.g. node.js), use the child_process module..
The workflow would be that in the windows 8 gui side, it will just issue a request to your own local server in node.js, then it would execute those commands.
Example:
var exec = require('child_process').exec;
var child = exec("insert command here", function(err, stdout, stderr) { });
See examples exec and spawn for more examples.
======
Another thing you can do is create a batch (.bat) file that contains those two commands needed for your mongodb instance and put that as a shortcut in the Windows 8 Start Screen.
It depends on what kinds of commands you need to execute, and when and where. If you plan to execute commands remotely, I'd assume server-side JS would be appropriate, but if you plan to execute commands locally, I think all you need is just batch scripting.

Deploying Node JS application over a server

I have done quite a research of deploying an application over the local server that I have on my machine. Each source code for the Node JS application or the example that is available over the internet specifies to run the application from the console.
Is there any way that i can configure my MAMP server so that when i hit a URL the Node code specified is executed.
Are there any parameters to set for the same ?
I looking forward to the steps to achieve this as i was not able to found a relevant answer for the same as such.

Categories