I am currently developing an Electron app and need it to look into the crontab file and parse it.
How can Node.js (or something else in Electron) access the main crontab file and read it?
I looked into the cron-parser library but it is only able to read cron-formatted strings, not access the file.
Thanks in advance.
Since the crontab file we see when we run is just a tmp file that shows whatever cron jobs the current user has created (which are stored in /var/spool/cron/crontabs) when whe don't normally have access to those files, I'd suggest you run a shell script to get these data:
const { exec } = require("child_process");
exec("crontab -l", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
//Do stuff with the read string on stdout
});
Hopefully this should give you the contents of the crontab under the user that's running the node script
Related
Hello I have got a jar file lavalink to be exact which is basically a music module now i would like to start the jar file from my index.js(Main bot file) as I am not in the financial state to pay for a separate host for the lavalink server and would like to start and use the lavalink server in the same hosting container
No I am not running it in a browser I am using discord.js wrapper to interact with the discord API to make a backend program
Installation
Install child process npm i child-process this allows you to execute shell comands
Command
give the exec function as first parameter your command
exec("cd ~/<path-to-directory-jarFile> && java -jar Myjar_file.jar
cd ~/<path-to-directory-jarFile> goes in the directory where your jar file is
java -jar Myjar_file.jar executes your jar file
the && executes your commands consecutevely first the one before then the one after
Example
const { exec } = require("child_process");
exec("cd ~/<path-to-directory-jarFile> && java -jar Myjar_file.jar ", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
Executing Shell Commands in NodeJS:
https://stackabuse.com/executing-shell-commands-with-node-js/
In the middle of my Node.js file I want to store the terminal response of docker run -v ${PWD}/app.py:/app.py proj1part1dockerimage in my file and store the output as a var. How would i go about getting a terminal response without opening a terminal?
In node.js everything is asynchronous, so you have to use a callback:
child_process.exec("docker run -v ${PWD}/app.py:/app.py proj1part1dockerimage",
(err, stdout, stderr) => {
const output = stdout.toString();
}
)
But if you require it to be instant which I don't recommend:
const output = child_process.execSync("docker run -v ${PWD}/app.py:/app.py proj1part1dockerimage").toString();
Hi I want to print(using printer) list of PDF files using nodejs. But not able to find any proper way. I found one JavaScript library called print.js(http://printjs.crabbly.com)
But with that also I m not able to call it in loop.
Is there anything I can do for this.
var pdflist = [a.pdf,b.pdf] //(this is my PDF list)
Thanks for help.
You can use ghostscript command line tools from your node app by forking a child process to execute the commands and loop thru your pdfs.
// OS : windows 64bits (for other OSs : linux, macosx ...etc; it's almost the same thing)
//assuming here that pdf is the path string to your pdf file
//printer name : Apple LaserWriter II NT
pdflist.foreach( function (pdf,index){
require("child_process").exec('gswin64c.exe ... -sOutputFile="%printer%Apple LaserWriter II NT" ' + pdf,
(error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
);
});
I'm trying to use the Atom electron to write a Desktop App for both Mac and Windows.
What I need here is :
A button.
And when the user click the button it runs the following shell (or python script):
ping x.x.x.x
And the result will be displayed in a TextArea.
I tried to use [shelljs] and [yargs] but it seems like it is not workable with Atom electron.
All I want is to use JAVASCRIPT to write Desktop App (with GUI of course) that calls some script (shell && python) to do some automation work.
Any suggestion will be appreciated, thanks :)
It can be done directly with Node, you can use the child_process module. Please notice this is asynchronous.
const exec = require('child_process').exec;
function execute(command, callback) {
exec(command, (error, stdout, stderr) => {
callback(stdout);
});
};
// call the function
execute('ping -c 4 0.0.0.0', (output) => {
console.log(output);
});
I encourage you to also have a look at npm, there are tons of modules that could help you to do what you want, without calling a python script.
Try node-powershell npm. You can directly execute shell script commands and display result.
var shell = require('node-powershell')
var ps = new shell()
ps.addCommand('ping -c 4 0.0.0.0')
ps.invoke()
.then(function (output) {
console.log(output)
})
.catch(function (err) {
console.log(err)
ps.dispose()
})
See: https://www.npmjs.com/package/node-powershell
you could use child_process to archive what you are trying to do by using the following code
var exec = require('child_process').exec
function Callback(err, stdout, stderr) {
if (err) {
console.log(`exec error: ${err}`);
return;
}else{
console.log(`${stdout}`);
}
}
res = exec('ping xxx.xxx.xxx', Callback);
I am working with node.js, and I am trying to embed a console in the web browser to work in a remote server. The web application do the connection so the user does not need to do the ssh username#host but only type commands.
I have tried the node.js' ssh2 module and other modules which use ssh2. But I'm experiencing always the same problem. Every time I execute a command programmatically using exec(), the ssh session is restarted. I'll explain it better with an example.
> ls
returns the content of home directory, one of the directories in the home directory is mydir
> cd mydir
> ls
returns the content of my home directory again, because after a command is executed the ssh session is closed/restarted.
Any node.js library which can do the job? or even a library of other technology different to javascript?
Edit: Other example for clarifying, using the node.js' module ssh-exec
The server has to execute some commands in other machine using ssh. A function in the server contains the following code
var c = exec.connection('username#host.com'); // It takes the ssh key from the default location
exec('cd mydir', c).pipe(process.stdout);
exec('ls -lh', c).pipe(process.stdout);
As you can see I am not ending the connection after the first exec but the output I obtain is the content of the home directory not the content of mydir directory, because the ssh session is reset after each exec.
The maintainer of node.js' ssh2 module provided the solution.
To use the method shell() instead of the method exec().
The method shell() creates an interactive session with the server we are connecting.
The method shell() provides a stream as a parameter of its callback (like the method exec()).
Like when using exec(), stream.on('data', function(data, extended) {...}); can be used to get the output of the commands. However, in this case, to provide commands (input) to the machine you connected with, you need to use stream.write(yourcommand+'\n');
PS. Feel free to edit to improve the accuracy of the answer.
I have to guess a bit, but you do something like child = exec('ssh username#host ls')?
You can do something like
child = exec('ssh username#host');
upfront and in the "loop" of your browser
child.stdin.write('ls\n');
When finished, just close stdin:
child.stdin.end()
which also finishes the child process.
I know this link is old but I figured this may help someone if they're looking for a solution. The
To use the method shell() instead of the method exec().
Works. Here's another solution. Use absolute file paths i.e.
conn.exec("mkdir -p /home/user/Direc/{one,two,three}/", function(err, stream) {
if (err) throw err;
stream.on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});
conn.exec("ls -la /home/user/", function(err, stream) {
if (err) throw err;
stream.on('close', function(code, signal) {
console.log('Stream :: close :: code: ' + code + ', signal: ' + signal);
conn.end();
}).on('data', function(data) {
console.log('STDOUT: ' + data);
}).stderr.on('data', function(data) {
console.log('STDERR: ' + data);
});
});