Trying to use grunt-kill to create a task that will kill my server-scorm task, and eventually any related tasks. The instructions are extremely short because it assumes I know all about PID files (which I don't, not the devs fault) and Grunt Manual makes 0 reference to them.
The problem is that I don't know how to properly reference the PID file path and what naming scheme Grunt uses.
I can get the the IDs when I run the following command:
ps aux | grep grunt
But of course that does me no good when the PID changes every time, so I can't directly reference it.
The instructions say to put this in the config:
kill: {
myService: {
src: [ 'my-service.pid' ]
},
secondary: {
pid: 'secondary.pid'
}
}
This is one example of how to use it... but ['my-service.pid']I have no idea how to get to that.
In my case, the name of the command grunt task I'm trying to kill is server-scorm so I assumed it was:
kill: {
serverScorm: {
src: [ 'server-scorm.pid' ]
},
}
of course when I run it, it doesn't recognize it. I get this:
seems that the missing piece to this puzzle is that I can't get to the server-scorm.pid or what ever its called.
If you're wondering why I'm not using cntrl+c it's the team is using an IDE called brackets and it "nicely" provides a grunt interface where all you have to do is click a button and it will run a command... As you may have guessed, there is no place where you can input commands like that.
I am copying my answer from your bug report on GitHub.
Hi #phillt The grunt-kill plugin cannot natively determine the pid for any process because without a pid, how would it know which process you want? Each application's way of "discovering" it could be different, and it could even be running multiple processes.
There are two ways for this plugin to determine the pid:
Specify the pid file. This requires the process you are attempting to kill to write its pid to a file. This is a common pattern for daemons in Unix, particularly when they run in the background as a service. The approach is when starting up, they write the PID of the main process that should be killed into a file in a well-known location and then tools like grunt-kill can read that file and know which process to kill. For example, the Apache web server on RedHat typically writes its pid file to something like /var/run/httpd.pid. I am not familiar with server-scorm, so cannot say for sure if it uses a pid file.
Specify the PID itself or a function which returns the PID. If you have a way to reliably find the PID programmatically, you can write it as a function in your Gruntfile and grunt-kill will happily execute it to discover the PID. You can probably put some filters around ps aux to find the pid. Maybe filter by process name and user login name?
On a side-note, I work in the e-learning space, so server-scorm and the adapt framework is going to give me some homework. :)
Related
I'm writing some tests for my React-Native application (using JS) in a NodeJS environment. In one scenario, I need to attach to an already-running Windows application. In order to attach to this Application, I need to know the NativeWindowHandle value.
For example, if you open Inspect.exe on a window, you'll find the "NativeWindowHandle" hex value.
Is there anyway I can find this value programmatically?
What I've Tried:
I'm able to find the PID of the app using:
const exec = require('child_process').exec;
exec('tasklist', function (err, stdout) {
....
}}
However, I haven't been able to turn that into the window handle. Does anyone have any ideas here? Is this possible?
This can be reliably accomplished by writing a native (C++) node addon which calls the appropriate Windows API functions and passes the results back to JS land.
eg you might want to call FindWindowEx and Windows will find and return the HWND (native window handle) of the matching open window. Or use one of the enumeration functions if you need to do the search yourself.
I did a quick search of npm and it looks like there might be a few packages that have done this work already, but you'll need to evaluate them.
If none of the npm packages will work, you'll need to write it yourself. This isn't too hard if you have a little C++ knowledge, but alternatively you might be able to get away with using node-ffi, which lets you write everything in JS and marshals the native calls for you.
(Using ffi will be a little slower than writing the native module yourself, but for your purposes that doesn't really matter. Either native or ffi will be much faster than spawning child processes.)
So, I'm teaching myself various AI techniques, and I figured the best way to do so would be to create my own Python bot which can play the game Pokemon using an online open-source Pokemon simulator.
The issue is that this simulator has all its source code written in JavaScript, whereas my bot uses Python. At first, I solved the problem of grabbing the full list of valid Pokemon and such by simply downloading the server's .js files, which (handily) contained all valid Pokemon/moves as JSON objects that I could easily import into Python. This was all well and good when I just needed Pokemon names and such, but now that I'm building out the actual engine I've discovered that it uses these same JSON files for its actual gameplay code.
For example, here's the Pokemon "Ability" Aftermath. Pay attention to onAfterDamage:
"aftermath": {
desc: "If this Pokemon is knocked out with a contact move, that move's user loses 1/4 of its maximum HP, rounded down. If any active Pokemon has the Damp Ability, this effect is prevented.",
shortDesc: "If this Pokemon is KOed with a contact move, that move's user loses 1/4 its max HP.",
id: "aftermath",
name: "Aftermath",
onAfterDamageOrder: 1,
onAfterDamage: function (damage, target, source, move) {
if (source && source !== target && move && move.flags['contact'] && !target.hp) {
this.damage(source.maxhp / 4, source, target);
}
},
rating: 2.5,
num: 106,
}
As you can see, onAfterDamage is a JavaScript function, one that I can't easily call from Python (to my knowledge). So now I'm at a crossroads:
Stop grabbing the .js files from the server and write my own code entirely in Python, essentially "translating" the JavaScript code myself.
Rewrite my entire bot in JavaScript instead of Python, although I'm far less experienced and comfortable in JavaScript.
Find a way to run the JavaScript code I'm downloading from the server from within my Python bot, calling arbitrary functions from within the code and returning the results.
Option 1 has the issue of rapidly becoming out of date -- when new Pokemon mechanics are introduced or new Pokemon are created, I wouldn't get it "for free" -- I'd have to figure out what changed and implement the changes myself.
I'd like to avoid Option 2 just because I'm not very comfortable writing JavaScript -- it's something I've dabbled with in the past, but it's far from my favorite language and not something I want to deal with when it's not the primary focus of my project.
I realize that Option 3 is a pretty massive security risk -- while I'm reasonably sure that a man-in-the-middle attack won't happen to a bot that likely nobody will ever use but myself, I'm still just downloading random JavaScript from a server somewhere and trying to execute it.
I've seen things like PyExecJS, but they're no longer maintained, and I haven't really seen anyone recommending a good replacement for them. PyExecJS's functionality is exactly what I'm looking for (namely the function execjs.eval(js_code)), but I'm not sure I want to use something that's reached EOL. I've seen the answers here, but they don't recommend any replacements for PyExecJS; they only show that user that they're using it wrong.
I've also seen people suggest Selenium or running a headless browser, but I'm not sure if I want to deal with all that extra overhead. The very first programming project I ever did outside of school was very similar to this, and I wound up coding it in Java using Selenium. I wasn't happy with that experience, and I'm not looking forward to having to do it again, especially when I only need to execute JavaScript I already have downloaded locally.
Am I completely off-base here? Is there another approach I should take, or should I just get to work translating all this JS code to Python by hand?
Ideally you could run your .js-file from cli just like any other executable. With node -e you can execute your JavaScript code on cli.
Relating to this post https://stackoverflow.com/a/450329/6478277 you could similarly run your script with your local node.
node -e "function func(){console.log(123)};func();"
From a file
print subprocess.check_output(['node -e "require(\"./file.js\").test()"'])
Where file.js could be something like
module.exports = {
test: () => console.log(123),
}
I have a EC2 instance, and I would like to write a script (never done them before) so that every time I start my E2 instance in the AWS console, the following files are run automatically.
Lets say I have a file called example.js which contains this:
var test(){
console.log('hello world');
}
test()
And then I have a similar file called example2.js
Everytime I run my EC2 instance, I need to ssh into it and do node example.js and node example2.js in order to run the functions.
However, I would like to write a startup script so that when the EC2 instance state is turned to running (i.e. online), I would like the command node example.js and node example2.js executed by themselves.
Is this possible? If yes, where do I put this script?
I have a Centos image running on my EC2 instance, and the EC2 instance is turned off at night, and turned back on in the morning - hence the script will simplify things!
I read this link, however cannot quite figure it out in my case: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
Any help will be appreciated. Thanks
use pm2. It's an excellent solution for managing node processes. Also can start the processes when you boot the machine.
ref: http://pm2.keymetrics.io/docs/usage/startup/
here is another good tutorials: https://futurestud.io/tutorials/pm2-restart-processes-after-system-reboot
Specific to EC2, you can use the 'User Data' option, available when you create your instance. It takes a script as input that will be executed at launch.
Here's how it looks like :
To add user data to your instance, wait until the night so you don't disrupt yor apps, then right click on it and do "Create Image".
Once the image is created, you can use it to spawn a "new" instance, with user data, and delete the old one without losing anything. Don't forget to reassign the elastic IP of the old instance :)
More information/documentation on aws.amazon.com
Ok, so I'm creating a webpage using socket.io in node.js.
This works great, and data is going to each device as it should.
Now i want to expand, and use node.js to control my pc.
I've read this: Child Process
and gotten to this code wich runs an executable, and prints its output in the console.
var spawn = require('child_process').spawn;
var ahk = spawn('outsideNode.exe');
ahk.stdout.on('data', function(data){
console.log(data.toString());
});
Here is the code, or script for outsideNode.exe that gets launched.
I'm using AutoHotKey for this.
Loop,10
{
FileAppend,External program spawned by Node.js says hi!,*
Sleep,1000
}
ExitApp
This works one way. Node.js captures AHK's output. Now, the problem i'm having is, how do i SEND data from node.js? And how do i recieve it in AHK?
I do belive i need to read about stdio. But i'm new to the std's.
My current solution is using Node.js to write a .txt file to disk with the command i want to give, and a seperate .exe file that reads the .txt and executes the command and exits. This works, but it is slow-ish, and just feels wrong to do it this way.
Also, I want it to be asynchronous, or synchronous? The one where i can do other stuff with node.js while the executable does its thing. Now i have to wait to be sure the file can be overwritten.
Any input on how to this is greatly appreciated.
There is the option to pass arguments to the spawned app. Maybe that's already what you are looking for. How to handle the arguments in your exe is another question related to some other programming languages. In node, child processes run asynchronous and non-blocking, that's the one you are looking for.
However, if you want to communicate between the programs, you can write to the stdin of the childprocess like so:
child.stdin.write("here you go");
Your exe must be able to wait for stdin in order to process the incoming data.
I'm using node.JS in VirtualBox on a TurnkeyLinux hosted by Windows. I was following the instructions here. node.JS is living in /root/node. Although I can run simple examples successfully I'm having a hard time figuring out certain things, cause I'm not a Linux-guy normally. I wanted to use socket.io.
I managed installing node.JS itself using git and tried that with Express and Socket.IO too. (e.g. git clone git://github.com/LearnBoost/Socket.IO.git). It seems to work, but I can't find that stuff anywhere! Was in /root/node when calling git, expecting changes in the lib-folder...
node.JS is using the CommonJS module system. In the Socket.IO example io = require('../') is used to import Socket.IO which looks pretty strange to me. Other expamples on the web are referring to './Socket.IO-node'. As a module is just a JS-file following certain rules I would expect sth like a path to such a file, as I found http.js in /root/node/lib.
By the way, looking at the server.js example: Is there a certain reason using var for http, but not for the rest of the variables (url, fs, io, sys)?
On clientside the first line on "How to use" Socket.IO is: io.setPath(...). setPath is not mentioned anywhere else on the page. Where should it point to, relative to what?
I've found no information about stoping/restarting node using the shell. Probably it's so obvious, that it's never mentioned anywhere ;)
Thanks for helping out.
The git-version that comes with the Turnkey-Core these days is quite outdated. Maybe this is causing problems. I worked around using my git on windows and WinSCP ;)
There is an inbuild automatism that index.js is used by default like index.html is used by default on webservers. So '../' is pointing to index.js in the parent folder, which then exports the listener of socket.io. Guillermo Rauch has put an index.js in the socket.io-folder now, so sth like './lib/socket.io/' is working. Note that there are examples out there with sth like './socket.io/socket.io.js', but socket.io.js doesn't exist anymore for some good reasons.
Of course the var is used for all variables. I've seen the commas as semicolons. Maybe I should change my screen-resolution ;)
It comes clear when looking at the example. setPath points to the folder where socket.io.js and it's lib-directory lives, relative to the html-file that uses it. This is needed for the flash-sockets to work.
Well, it's not that simple. You may look up the PID usind 'ps ux' and then 'kill' the process using the PID. A better way is using upstart. Or you do it by code using autorestart.