Cannot kill a windows process in cygwin - windows

I can kill a windows process in Cygwin with the command:
$ echo "4128" | xargs kill -f
but cannot do it with the following command:
$ kill -f 4128
-bash: kill: f: invalid signal specification
According to documentation of kill the syntax should be correct https://cygwin.com/cygwin-ug-net/kill.html
You can find there:
$ kill -f 123
Why doesn't the second command work?

Cygwin has 2 kills:
$ type -a kill
kill is a shell builtin
kill is /usr/bin/kill
The shell builtin does not support the -f option. You need to use the kill
executable:
/bin/kill -f
Or:
env kill -f

Related

How to get PID of terminal inside script which is created gnome-terminal command, but keeping terminal active for later use

Here is my command
gnome-terminal --working-directory ~ -- bash -c " echo 'Execution started' && ./test && echo 'Execution finished' ; /bin/bash"&
#!/bin/bash
echo "PPID ="
echo $PPID
I use in the end of my command /bin/bash to create new terminal and keep it opened for later use, but I need to store PID of terminal which is runs my script and not close it, gnome-terminal command always closed terminal which executes my ./test script
I understand, you want the PID of the terminal where you lauch a new terminal.
So:
gnome-terminal --working-directory ~ -- bash -c "FATHER_TERM_PROC=$PPID /bin/bash" &
In the new terminal:
$ echo PID=$$, PPID=$PPID, FATHER_TERM_PROC=$FATHER_TERM_PROC
PID=5166, PPID=5159, FATHER_TERM_PROC=4417
$ ps -o pid,ppid,args -p $$ -p $PPID $FATHER_TERM_PROC
PID PPID COMMAND
4417 2043 /usr/bin/xfce4-terminal --geometry=125x40
5159 1600 /usr/libexec/gnome-terminal-server
5166 5159 /bin/bash
Now, you known, my first terminal is a XFCE terminal!
Is this what you wanted?

Kill a process in ruby

I use this code to kill a process with a PID file:
Process.kill 15, File.read('/tmp/pidfile').to_i
But the following two examples never work, when i try:
system "kill `cat /tmp/file.pid`"
or
`kill \`cat /tmp/pidfile\``
output is:
sh: 1: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
Is there a problem with the backstick ? because in bash this works perfectly:
kill `cat /tmp/file.pid`
The string is not being interpolated. This does not run a cat command:
system "kill `cat /tmp/file.pid`"
Instead, you could write this as:
system "kill #{`cat /tmp/file.pid`}"
However, I'm unclear why you'd choose to do this over your original (working) method.

pgrep command doesn't show bash script

When I run a simple bash script, say myscript.sh
#!/bin/bash
sleep 30
from the terminal, and then do pgrep myscript.sh I don't get any result. Why?
You're probably doing this:
pgrep myscript.sh
This won't show the process you're running because it is /bin/bash that is running your script.
You should be doing:
pgrep -fl myscript.sh
To list your process.
As per man pgrep:
-f Match the pattern anywhere in the full argument string of the process instead of just the executable name.
Your just running your bash script, u need to use -f flag, please check man page
Check pgrep is installed in your machine. Just do man pgrep, if you get command not found that install the utility.
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout. All the criteria have to match.
Example usage:
pgrep name | xargs kill
If you use pgrep name | kill, the ouput of pgrep name is feed to stdin of kill. Because kill does not read arguments from stdin, so this will not work.
Using xargs, it will build arguments for kill from stdin. Example:
$ pgrep bash | xargs echo
5514 22298 23079

how to extract the PID of a process by command line

I want to get the PID of a process namely "cron" by command line.
I tried the following script.
ps ax|grep 'cron'
but I am getting a part of a table,
1427 ? Ss 0:00 /usr/sbin/cron -f
24160 pts/5 S+ 0:00 grep --color=auto cron
How I extract the pid from this ?
The pgrep utility will return the process IDs for the currently running processes matching its argument:
$ pgrep cron
228
It may also be used to "grep for" things on the command line:
$ pgrep -f uerfale
69749
69752
$ pgrep -l -f uerfale
69749 slogin uerfale
69752 slogin: /home/kk/.ssh/sockets/uerfale-9022-kk.sock [mux] m
To kill a process by name, use pkill. It works in the same way as pgrep but will send a signal to the matched processes instead of outputting a process ID.
Just use pidof, rather to use other commands and apply post-processing actions on them.
$ pidof cron
22434
To make the command return only one PID pertaining to to the process, use the -s flag
-s
Single shot - this instructs the program to only return one pid.
Like this, for example:
ps -ef|grep 'cron'|grep -v grep|awk '{print $2}'
You can try this;
ps -o pid,sess,cmd afx | egrep "( |/)cron( -f)?$"
or
pstree -pas <cronPID>

Bash Script for Automation - Command not found

I have the following in my .bash_profile:
k9 () { for A in $(ps -A | grep $* | sed 's/^\([A-Za-z0-9]*\).*/\1/' ; ) ; do "kill -9 $A"; done }
The script supposed to grep the var inputed in the command line S* get the PIDs those and execute a kill -9 on each of them. But instead I am getting this:
Machine:~ mach$ k1 Chromium
-bash: kill -9 81922: command not found
-bash: kill -9 82009: command not found
-bash: kill -9 82423: command not found
-bash: kill -9 82424: command not found
-bash: kill -9 82560: command not found
-bash: kill -9 82561: command not found
-bash: kill -9 82563: command not found
-bash: kill -9 82608: command not found
-bash: kill -9 85243: command not found
-bash: kill -9 85248: command not found
-bash: kill -9 85321: command not found
Thanks in advance!
You don't need to quote the entire command.
k9 () {
for A in $(ps -A | grep $* | sed 's/^\([A-Za-z0-9]*\).*/\1/' ; ) ; do
kill -9 "$A"
done
}

Resources