I am trying to run cygwin script from java code. I have used following code for same,
String[] command = {"cmd.exe","/C","Start","C:/cygwin64/bin/bash","./bin/script.sh","input_file.mkv","I:/video/output.avi"};
Runtime.getRuntime().exec(command);
it works fine but, instead of calling script from "cmd" as described above. Can we run cygwin bash script directly? I have tried following code but it is not working
String[] command = {"C:/cygwin64/bin/bash","./bin/script.sh","input_file.mkv","I:/video/output.avi"};
Runtime.getRuntime().exec(command);
I am not getting what I am doing wrong. Also I want script to run as background process, so what can we do for same?
Related
I'm new to programming and I just started learning JS. I tried running my first code via 'code runner' on Visual Studio Code but it wasn't giving me the output. enter image description here
You need to install NodeJS first in order to run the JavaScript on your machine. You can download it here. Then, you should make sure that Node path is added. After that restart VS Code and run it again.
I want to build a Web-based Terminal and I want to execute a shell command.
How can I use jQuery Terminal library to send command to shell and print the reply?
So to execute the shell commands you need to have a server code. The simplest way is to use, Apache and PHP. For this, you need to create a PHP script and use one of the functions that allow executing shell commands.
shell.php
<?php
if (isset($_POST['command'])) {
echo shell_exec($_POST['command']);
}
with this server-side script in place, you can write JavaScript code using jQuery Terminal that will send the request to that PHP script and display the results.
$('body').terminal(function(command) {
return fetch('shell.php').then(res => res.text());
});
What else you can do to improve the terminal is to use a unix_formatting.js file that will display any ANSI Escape code that may be generated by the shell in the browser:
<script src="https://cdn.jsdelivr.net/npm/jquery.terminal/js/unix_formatting.js"></script>
If you want to see an example of this that is more advanced, you can take a look at:
jcubic/jsh.php that was created as a single file, PHP shell that looks like a real terminal. It uses few tricks to make it more user-friendly like using unbuffer (part of expect package on GNU/Linux) and other bash tricks to make it display ANSI escapes code for every command. By default ls don't return output in colors.
If you want something even more advanced you can look at Leash Shell project that was created to give access to the shell on shared hosting.
Alternatives are:
websocket.sh which has an example of using jQuery Terminal with Unix shell via Web Sockets written as shell script.
web-console that is PHP shell that uses jQuery Terminal.
I have a simple JavaScript myScript.js with code as below;
var printer = function(someString){
console.log(someString);
}
printer("This is printed on console.");
I'm on Windows 10 and need to be able to call myScript.js on a command prompt and have the code executed without doing node myScript.js. Is there a way to set up things so that Command prompt or PowerShell can automatically call Nodejs or other JavaScript engine?
If your Windows can run normal shell scripts on the command line then you can add a shebang line to your Node script just like you do with shell scripts. For example:
#!/usr/bin/env node
// your node code here
Also you can try to configure Node to be invoked for all files with the .js extension but this can be a security hazard because you may execute arbitrary code just by clocking on JavaScript files that you may have downloaded on your system so I wouldn't really recommend that.
Another alternative is to make a BAT script for you Node app:
example.bat:
node example.js
example.js:
// your Node script
That you will be able to run with just:
example
on your command line.
I'm running on Windows Server 2012R2.
I have a node.js script which I want to run when the user performs logins.
To do this I'm setting the command to run in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry path.
I set this command:
/path/to/node/node /path/to/node/script args
This works fine, but it will spawn a terminal showing the command output, while I'd like to run this command in background and detached from any command terminal.
I tried to replace the previous command with:
start /b /path/to/node/node /path/to/node/script args
but in this case there is no evidence that the script was even started.
I also tried to wrap the following command in a .bat script:
start \b node script args
set the name of the script in the Run key: in this case I can see terminal flash but then the script is not running anymore (I suppose that the script is executed but then it is stopped as soon as the parent process is terminated).
I want to avoid to convert the script in a windows service, as long as a simpler solution is possible.
A solution not using the Run registry key is also fine, as long as it fulfils my requirement (run a script in background when the user logins).
Actually it's pretty easy , use forever.js module
after installing the module use
"forever start main.js"
It will start running as your background process
How can I run shell command in javascript in code behind
I have this exe file on:
C:\Program Files\test.exe
that i want to run using javascript and I want to do it in codebehind or at least call this function from code behind.
The reason want to do it on code behind is that I have parameters need to pass them in the shell command.
For security reasons you can not run commands from JavaScript.
If you are using JScript, then
var shell = WScript.CreateObject("WScript.Shell");
shell.Run("command here");