How to get the process id (PID) of an app launched using `open` command on OSX? - macos

I've launched an application via the open command on the OSX command line like so:
open -a "/Applications/Adobe After Effects CC/Adobe After Effects CC.app"
I want to get the process id of that launched application. Is there any way to do this reliably on OSX? It doesn't seem open returns anything, so I'm not sure I can even pipe its result into something like ps to perform a grep operation. I thought that maybe since the app is launched via the terminal I would know which app is the frontmost, but am doubting the reliability of that solution. Any ideas?

After executing open -a, you can execute ps command. Doing a grep on the output of ps command gives info on process ID.
ps aux | grep -v grep |grep -i <application name> | awk '{print $2;}'
The one given below gives the elapsed time for the process.
ps aux -o etime,command | grep -v grep |grep -i <application name> | awk '{print $2, $12,$13}'
We can compare the elapsed time to know the pid of the recently launched one.

These days, pgrep -n $APPLICATION_NAME seems like the easiest way to accomplish this. From the man page:
-n Select only the newest (most recently started) of the matching processes.
-o Select only the oldest (least recently started) of the matching processes.

Activity Moniter
You can use Activity Moniter. Located in /System/Applications/Utilities/Activity\ Monitor.app
Open the app, hit the search bar on the top and search for the app you want to get the PID of.

Related

kthreaddi process is causing a high CPU usage

I'm using REDHAT 7.6 as an OS on my server and top command shows high CPU usage from kthreaddi process without obvious reason, now my solution is down, and also my Fusion middleware (I'm using ORACLE as database )
any solutions
I experienced this problem a few days ago, the quickest solution I could get was the following:
Use the following command:
ls -l /proc/<PID_of_process>/exe
This command shows the position where the process is running.
Create this folder.
Remove all the permissions for all users:
chmod o-rwx /tmp/<name_of_folder>
Then kill the process:
kill -9 <PID_of_process>
Kindly run below one liner bash shell script inside crontab every minute and it will kill the virus in a scripted way.
kill -9 $((ps -aux | grep -i 'kdevtmpfsi\|kinsing\|kthreaddi') 2>/dev/null |grep -v grep |awk '{print $2}')

Does this bash script using grep look correct?

I am having some strange rebooting issues and I think it is due to an error in my shell script.
#!/bin/bash
if ps -a | grep -q gridcoin
then
echo nothing
else
sudo reboot -h now
fi
The script is supposed to work like this:
Run the ps -a command so that all processes are listed. Pipe the results to grep and have grep check to see if any of the processes have the word "gridcoin" in them. If gridcoin is running, do nothing. If gridcoin is not one of the running processes, reboot the system.
I have a cron job that runs this script once every five minutes; however, my system keeps rebooting about every five minutes even when I know for a fact that gridcoin is running.
Please take a look at the code and see if it looks right before I start trying to herd other code cats.
Respectfully,
chadrick
I see at least two problems here. First, ps -a will not show processes that don't have a controlling terminal (so, basically, non-interactive processes); you want ps -ax to show all processes.
Second, depending on the timing, the grep -q gridcoin command may be listed as a running process, and of course it finds itself, meaning that it mistakes itself for the gridcoin process. If you have the pgrep program available, replace both ps and grep with it, since it automatically avoids listing itself. Unfortunately, it doesn't have a -q option like grep, so you have to redirect its output to /dev/null:
if pgrep gridcoin >/dev/null
If pgrep is not available, there are two standardish idioms to prevent grep from finding itself:
if ps -ax | grep -q '[g]ridcoin' # Adding brackets prevents it from matching itself
if ps -ax | grep gridcoin | grep -vq grep # Remove grep processes from consideration
Also, as David C. Rankin pointed out, rebooting the entire system to restart one process is overkill; just restart that one process.

xargs and kill not working together to kill specific processes

I have a Docker container running Red Hat 6.8 and I have several Java processes running. On other machines with the same OS, I have used a command similar to the following to find and kill all Java processes:
ps -ef | grep 'java' | grep -v 'grep' | awk '{print $2}' | xargs kill -9
However, on this machine, it gives me the following error:
xargs: kill: No such file or directory
Taking off the "| xargs kill -9" does work and shows me the PIDs of the processes I want to kill but for some reason, the command will not work all together.
Does anyone have any ideas why?
The immediate problem is that xargs can't find the kill command. It needs to be in your PATH, probably in /bin and/or /usr/bin. You wouldn't notice it was missing when you run the kill command directly from the shell, because most shells have a kill builtin.
Also, I agree with the comments by other users about the overall idea. There are less hacky ways to do this (killall, pkill, anything that doesn't involve grep that's relying on luck to avoid matching the wrong part of the `ps output...)
That's the top result in Google about this specific issue, if not the only one, so I just wanted to note it here after two years: kill is bundled in util-linux package (in RHEL) or procps package (in Debian) along with bunch of other utilities. Installing that package in the container solves the issue.

Linux kill process by cmdline (multiple with same name)

I am writing a bash-file (.command #OS X) to easily update a program at my remote server (Linux). I am stuck trying to figure out which PID to kill. I have different processes with the same name, which is mono, since they are executed by mono.
Using pgrep mono I get all the PIDs using mono, however, I only want to kill the process that has the command line SCREEN -dmSL steambot mono SteamBot.exe.
How do I know what PID to kill? Atm, my .command file looks like this:
ssh xxx#xxxx.com
pgrep mono
I am using ssh-keychain to login also.
like this:
ps -ef | grep '[S]CREEN -dmSL steambot mono SteamBot\.exe' |
awk '{print $2}' | xargs -r kill

Using grep with ps -ax to kill numerous applications at once (Mac)

I'm very new to shell scripting. I recently covered a tutorial of using bash with grep.
About 5 minutes ago there was, as it happened, a practical application for this function as my browser appeared to have crashed. I wanted to, for practice, use the shell to find the process and then kill it.
I typed ps -ax | grep Chrome
But many lines appeared with lots of numbers on the left (are these "job numbers" or "process numbers"?)
Is there a way to Kill all "jobs" where Chrome exists in the name of the process? Rather than kill each one individually?
I do not recommend using killall.
And you should NOT use -9 at first.
I recommend a simple pkill:
pkill Chrome
You can use killall:
killall Chrome
Edit: Removed "-9" since it should be used as a last resource (was: killall -9 Chrome).
I like my solution better. Because adds a kind of wildcard to it. I did this:
mkdir ~/.bin
echo "ps -ef | grep -i $1 | grep -v grep | awk '{print $2}' | xargs kill -9" > ~/.bin/k
echo "alias k='/Users/YOUR_USER_HERE/.bin/k $1'" >> ~/.profile
chmod +x ~/.bin/k
Now to kill stuff you type:
k Chrome
or
k chrome
or
k chr
Just be careful with system processes or not to be killed processes.
Like if you have Calculator running and another one called Tabulator also running. If you type
k ulator it will kill'em both.
Also remember that this will kill ALL instances of the program(s) that match the variable after the command k.
Because of this, you can create two aliases inside your profile file and make 2 files inside .bin one with the command grep -i and the other without it. The one without the -i flag will be case and name sensitive.

Resources