Is there any way to stop ts-node-dev? - ts-node

I'm trying to make a Discord bot that can shut down on command. Yesterday I asked another question related to this but no one answered. So now I'm thinking is there any way to stop ts-node-dev from command line / .ts file?

Have you tried process.exit(1)? It works in js. I think you need to install #types/node for ts?
Alternatively you can use a bit of a whacky method:
You can try launching ts-node with pm2 or nodemon and in order to kill your bot try executing cli command like
exec("killall nodemon", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
I hope it helps

Related

Jenkins Scripted Pipeline: sshCommand execution statusCode

In my jenkins scripted pipeline one stage I am running a bash script in remote machine. I tried few ways as follows but not coping with requirement :
- Since I have argument to pass to remote server command to run which sshScript doesn't supports it
- publish over ssh plugin displays my execCommand in the jenkins logs which I dont want too.
So I use sshPut to put my bash script on remote server and sshCommand with arguments to run it there. All good , except when I have errors, I need to exit and do some other things. What happens is if there is an error, jenkins jobs exits execute with exception. This can be override by setting failOnError: false for sshCommand; but then that Job will never fail at all.
I need that if the sshCommand: exits with an error then I need to do some thing like send slackNotify or so. So is there any thing like statusCode or exit-bhalah which I can compare it like != 0 and do some function ?
I am thinking of something like
stage('Deploy'){
// some blocks here
sshCommand remote: remote, failOnError: false, command: "bash Filescript.sh $ARGS1"
if (statusCode != 0){
//do my thing here
}
}
You can use try-catch:
stage('Deploy'){
// some blocks here
try {
sshCommand remote: remote, command: "bash Filescript.sh $ARGS1"
}
catch {
//do my thing here
}
}

How to run command in Laravel kernel after another successful command

I'm creating a custom command which will truncate a table every thirty minutes in the console kernel (for development purposes). I want to run another right after the previous command.
P.S: I have an if statement which prevents running these commands on the production server.
$schedule->command('db:seed')->after(function () use ($schedule) : void {
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->environments(['demo', 'local']);
});
I expect to run the seeder right after "my-command" runs successfully every thirty minutes. However, in this way, only db:seed runs.
If you want to run B after A you need to schedule A and AFTER that run B:
$schedule->command('my-command:remove-users-from-tables')
->everyThirtyMinutes()
->after(function() {
$this->artisan->call('db:seed');
});
I have checked the source code for Illuminate\Console\Scheduling\Schedule class.
I think when we say:
$schedule->command(...);
The artisan command will be scheduled, not run straightaway.
So when you write like this:
$schedule->command('first-command')->after(function () use ($schedule) {
$schedule->command('second-command');
});
The second command will be registered, not run right after the first command.
So the best approach that I can think of is run the second command inside the first command according to this link
You might try something like this:
namespace App\Console\Commands;
use Illuminate\Console\Command;
class RemoveUsersFromTable extends Command
{
public function handle()
{
// Do something to remove users from table.
$this->call('db:seed');
}
}
An alternative that may be relevant in some cases (but I can imagine would also be a bad idea in others) is to run shell_exec: https://www.php.net/manual/en/function.shell-exec.php
If you want to chain two artisan commands together as you would on the cli using && you can simply shell_exec('php artisan command:first && php artisan command:second').
shell_exec returns the console output so if your commands print anything (ie, via $this->info) then you can print that to console like so: $this->info(shell_exec('php artisan command:first && php artisan command:second'))
I'm probably going to get a lot of hate for this answer...

Nodejs child_process doesnt find commands

im trying to execute the folowing with nodejs with Windows
var exec = require('child_process').exec;
var cp = exec('A:\Programme\node\node.exe',function(err,std,ste) {
console.log(err);
});
i tried replacing node with echo, ipconfig etc.. but i always get the same response:
{ [Error: spawn ENOENT] code: 'ENOENT', errno: 'ENOENT', syscall: 'spawn' }
Im sure that I linked the PATH correctly and I have avoided Spaces.
Does someone have an idea where the Problem could be?
edit
Alright i got, some douchebag removed System32 from PATH.
Added it, now it runs again.
thx for the help.

Is it possible to get the output of a bash shell command as a string in node.js? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Node.js Shell Script And Arguments
I'd like to get the output of a shell command as a string in node.js, but I'm not exactly sure where to start. An example of such a command is the bash command "ls", which lists the contents of the current folder, and prints the results in the terminal window. Is it possible to convert this output to a Javascript string?
See the documentation for "Child Processes" in the nodejs.org API documentation which provides example code to handle the exact task you mention, i.e. running the 'ls' command and capturing its output.
var spawn=require('child_process').spawn,
ls=spawn('ls', ['-lh', '/usr']); // runs the 'ls -lh /usr' shell cmd
ls.stdout.on('data', function(data) { // handler for output on STDOUT
console.log('stdout: '+data);
});
ls.stderr.on('data', function(data) { // handler for output on STDERR
console.log('stderr: '+data);
});
ls.on('exit', function(code) { // handler invoked when cmd completes
console.log('child process exited with code '+code);
});

Why does this VBScript give me an error?

I saved this VBScript script to my local machine as c:\test.vbs:
WScript.StdOut.WriteLine "This is a test"
When I run it from the command line, I get this error:
---------------------------
Windows Script Host
---------------------------
Script: C:\test.vbs
Line: 1
Char: 1
Error: The handle is invalid.
Code: 80070006
Source: (null)
---------------------------
OK
---------------------------
I get this under Windows Vista (SP1) and Windows XP Pro (SP3).
This link may help you:
http://www.tech-archive.net/Archive/Scripting/microsoft.public.scripting.vbscript/2004-07/0979.html
It appears that the handle StdOut is only available when using a console host (cscript.exe) and not a windowed host (wscript.exe). If you want the code to work, you have to use cscript.exe to run it.
The post also describes how to change default behavior to run scripts with cscript and not wscript.
As described by the article in the accepted answer, my script worked when I called it from the command prompt like this:
cscript test.vbs
You can also change the default script host, so that a call to cscript is not necessary every single time. After doing that, the original command works unmodified.
cscript //h:cscript //s
You can restore the original behavior with:
cscript //h:wscript //s
Thanks!!
I submitted this solution in bug "cscript - print output on same line on console?" which I feel is related to this issue.
I use the following "log" function in my JavaScript to support either wscript or cscript environment. As you can see this function will write to standard output only if it can.
var ExampleApp = {
// Log output to console if available.
// NOTE: Script file has to be executed using "cscript.exe" for this to work.
log: function (text) {
try {
// Test if stdout is working.
WScript.stdout.WriteLine(text);
// stdout is working, reset this function to always output to stdout.
this.log = function (text) { WScript.stdout.WriteLine(text); };
} catch (er) {
// stdout is not working, reset this function to do nothing.
this.log = function () { };
}
},
Main: function () {
this.log("Hello world.");
this.log("Life is good.");
}
};
ExampleApp.Main();

Resources