Is there a Terminal command that I can use to shut down a mongod process in OS X? - macos

mongod --shutdown is not available on OS X. The only good way seems to be using the mongo interface. Can I shut down a mongod process that was started in the background using the --fork flag using the command line? Any way to use kill with ps?
I'm trying to shut down MongoDB using an npm posttest script (just a command that's run after my unit tests).

Either find out the pid using ps | grep mongod or echo $! after starting it up (the variable $! holds the pid of the last started process) and use kill pid.
Or, simply use mongo admin --eval "db.shutdownServer()", which throws an error but seems to work anyway. You can always pipe undesired output to /dev/null.

Related

ROS/Linux: What exactly does '&' in the linux terminal?

I am working with ROS. And for starting ros-packages you need to have the ROS Master run in the background. Now when I want to start the ROS-package rviz, instead of opening two terminals:
Terminal1:
$ roscore
Terminal2:
$ rviz
I can do the follwing in one Terminal:
$ roscore& rviz
But what exactly is happening here? Because when I end that terminal with Str+C it only closes rivz, but roscore is kept running in the background? Why and how can I close it?
in case using single & the left side will run in the background, while the right side will run normally in the terminal.
Now to close the first process you need to find PID (Process ID) and do the termination command, so first of all you need to find PID and you can use pgrep (in your case PROCESS_NAME can be roscore):
pgrep -f PROCESS_NAME
Now to kill the process you can easily do:
kill -9 PID_HERE
Or you can do it by single command:
pgrep -f PROCESS_NAME | xargs kill -9

how to stop http-server on a specific port?

I am using http-server in order to load http://localhost:8484 on a specific folder. (For testing purposes)
the os commands I run in my code are:
http-server -p 8484 test/
and after I finish downloading whatever I run:
http-server stop
However, after the test is done, I see that the http-server with port 8484 is still alive!
by running ps aux | grep http
What command should I run in order to stop it?
I am using Mac OSX (El Capitan version)
I write the code in erlang (though I don't think it matters since I am running shell commands from the code).
http-server: https://www.npmjs.com/package/http-server
My code in erlang:
my_test_() ->
Pid = spawn(fun() ->
Info = os:cmd("http-server -p 8484 test/resources"),
io:format(user,"*** Server: ~p~n",[Info])
end),
%%% Do some job %%%
Pid2 = spawn(fun() ->
Info = os:cmd("http-server stop"),
timer:sleep(200),
io:format(user,"*** Server stop: ~p~n",[Info])
end),
timer:sleep(200),
exit(Pid2, kill),
exit(Pid, kill).
Use:
kill -9 {pid}
Also, out of interest, if you want to see all processes running on a specific port, use:
lsof -i :{port}
EDIT: Using kill -9... is a bit harsh, I know, there is probably a more graceful way of doing it, but it does get the job done ;-)
For Windows users use the command prompt (cmd):
Method 1:
Just do Control+c on the same console where the http-server is running
Method 2: Find and Kill the process:
i. Find the process-id which uses the particular port number (say 8484)
netstat -ano | findstr 8484
Result: TCP 0.0.0.0:8484 0.0.0.0:0 LISTENING 21816
ii. Kill the process using the found process-id (say 21816)
taskkill /F /PID 21816
You can use perc - since Node.js http-server is not Erlang process, but Unix process, you need to use the module (or dig it's code to see the implementation ;) ) .
Alternatively, from Erlang os:cmd("kill -9 5607"). (where 5607 is your unix pid);

How to hide/quit terminal while program is still running in OS X?

I'm doing my project and I need to log keystrokes system wide in macOS. So, I turned over to GitHub and found Swift-Keylogger. The only problem is I can't quit the Terminal while the program is still running.
Is there any way to not to run this in terminal or closing the terminal window instead of creating a mac app like here.
Note: There are some mac app style executables in github but they are not providing the output I want and some need additional permissions.
Instead of running the executable like ./Keylogger use nohup ./Keylogger &.
You can quit the Terminal after executing the command.
To kill the Keylogger, run ps -e | grep "Keylogger" to get pid and kill -9 pid.
P.S. I thought of adding it as well as running at startup but I'm too lazy to update the repository.

Terminal Command: start or stop process if it's running or not

I have a glassfish install and I would like to have a simple terminal command checking if it's running. If it is, it will shutdown the process. Or if it not running, it will start the server.
To start the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
To stop the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/.asadmin stop-domain domain
I will would like to make a simple Alfred.app script than can start this domain if it's not running, or stop it if it's running.
One way to do this is to note in a file the process ID of the server when it starts, and have another script check to see if that process is running.
For example, script A (to run the server unconditionally):
#!/bin/sh
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
# file name is arbitrary
pgrep whateverTheProcessNameIs > ~/.glassfish-server.pid
And in script B:
#!/bin/sh
pid=`pgrep -F ~/.glassfish-server.pid` # file chosen in script A
if [ "x$pid" = "x" ] ; then
# process has died; restart by running script A
/path/to/scriptA
fi
Note that only Mac OS X 10.8 (Mountain Lion) installs pgrep by default; otherwise you'd have to use some other method (like parsing ps output) to see what processes are running.
As far as how to run this check periodically, there are various ways. I'm assuming Alfred will run any script that is executable (chmod +x scriptA scriptB) but I don't know for sure.

How can I find out what a command is executing in Terminal on MacOs

After I run a shell script (which call a bunch a other scripts depend on conditions. which is too complicated to understand), I can execute a command 'gdbclient' at my MacOS terminal.
But when I do 'which gdbclient' and 'alias gdbclient', it shows nothing.
Is there anyway for me to find out what 'gdbclient' is actually doing?
You can open up another terminal window and type: ps
That will list all the running processes.
If your script is running as a different user than the current one, you can use ps -ef to list all running processes.
If you know the PID of the process that ran your script, you can find all child processes via parent PID using ps -f | grep [pid]
You can use the Activity Monitor to check things out pretty thoroughly. To get the right privileges to see everything going on you can do:
sudo open /Applications/Utilities/Activity\ Monitor.app/
Dtrace can give you some helpful information: dtrace
to find process 'gdbclient':
ps aux | grep gdbclient
That wont tell you what it's "doing" but that it's running

Resources