scheduled node script in crontab doesn't run after reboot - node.js

I've scheduled a node script in crontab to run after reboot by adding this line to my /etc/crontab:
#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js
/home/eugen/Documents/scripts/tests/run_after_boot/index.js file contains this script, which is supposed to open vscode:
const { exec } = require('child_process');
exec('code', (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
}
});
After reboot, nothing happens. Also, I tried to add the same line (#reboot /home/eugen/Documents/scripts/tests/run_after_boot/index.js) by executing the crontab -e command in my terminal, but the same problem appeared: nothing happened after reboot.

Related

Command work in terminal but doesn't work with node.js

I try to unpack img file with 7z on Linux. In terminal it works fine:
7z x myimg.img
But when I try to do this from node.js script it fails:
const childProcess = require('child_process');
childProcess.exec('7z x myimg.img', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.error(`stderr: ${stderr}`);
});
I get this message:
exec error: Error: Command failed: 7z x myimg.img
ERROR: myimg.img
Can not open the file as archive
If I run command whereis 7z I get the same path.

Node.js child_process.exec "/bin/sh: 1: docker: not found" error

I created a micro-service using the "Moleculer" framework. I used child_process.exec for running docker commands in a shell. I get "/bin/sh: 1: docker: not found" error.
const { exec } = require("child_process");
.
.
.
exec("docker --version", (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Tried giving absolute path as:
exec("/usr/bin/docker --version", (err...);
But still got the same error.
I am able to get the node version using:
exec("node --version", (err...);
I just had the same problem. The reason was simply that I had built a new image but the Docker client was not present in my container because I only did docker-compose restart <CONTAINER_NAME> and not docker-compose stop <CONTAINER_NAME>, docker-compose rm <CONTAINER_NAME> and docker-compose up -d <CONTAINER_NAME>.
https://stackoverflow.com/a/63281470/1707015

How I can create build file with Node.JS script?

How I can do it with Node.js(script file not in cmd consol):
go to the folder with the project
do npm i
do webpack -p
?
Create a bash script with all you want to do:
yourbash.sh:
cd /yourdirectory
npm i
webpack -p
then just spawn this process in your node
const spawn = require('child_process').exec
spawn('sh yourbash.sh', [], function (err, stdout, stderr) {
if (err) { console.log(err) } // handle error
// standard output from the bash script
console.log(stdout)
})
Alternatively, you can skip the bash file entirely by doing this:
const spawn = require('child_process').exec
spawn('cd /yourdirectory && npm i && webpack -p', [], function (err, stdout, stderr) {
if (err) { console.log(err) } // handle error
// standard output from the bash script
console.log(stdout)
})

Why is my node child_process terminating early?

I am trying to run an async node child_process which runs a docker container.
let runDockerCmd = `docker-compose -f ./vm/docker-compose.yml run --rm vm node temp.js`;
child_process.exec(runDockerCmd, (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
Where temp.js contains a for loop. Sometimes it runs half the loop, sometimes a third etc. then it terminates.
I can get it to work using child_process.execSync but that is a bit unsavoury. Why does it exit like that, and how do I keep it alive?

Executing grunt through node

I'm building a node application that requires me to run grunt on a directory to build a "package". I'm using Node's exec command to execute grunt like so:
var exec = require('child_process').exec;
exec('grunt', function (error, stdout, stderr) {
if (error) {
console.log(error);
} else {
console.log('Grunt task completed');
}
});
Which triggers the below error and I'm not sure why...? (or what it means)
{ [Error: Command failed: /bin/sh -c grunt
] killed: false, code: 2, signal: null, cmd: '/bin/sh -c grunt' }
I'm running this on OSX but the application will be deployed to a server running Amazon Linux.
Update:
exec('/bin/sh -c grunt', function (error, stdout, stderr) {
if (error) console.log(error);
});
Outputs the below error (but works fine when ran from terminal):
{ [Error: Command failed: /bin/sh -c /bin/sh -c grunt
]
killed: false,
code: 2,
signal: null,
cmd: '/bin/sh -c /bin/sh -c grunt' }

Resources