How to get pkill on git bash - bash

background
years ago I made a Minecraft spigot server and found a bash script to turn it on, in the script, it has a restart function that makes use of pkill to kill the old server and start a new one, I tried using it on my new spigot server to find that bash doesn't contain pkill, any google search I use relating to "how to get pkill on git bash windows 10" doesn't give me anything useful, the script has the following in it, the file is a .sh if that matters:
# /bin/sh
while true
do
java -Xms4G -Xmx6G -jar spigot-1.16.3.jar nogui
pkill -f "spigot" #Not really needed, just there in case of anything.
echo Its off..sleeping for 5
sleep 5
done
My question is,
what can I use to kill the process by name like pkill would, or could I somehow install pkill to just work?
any help is appreciated and I apologise in advance if this is a stupid question.

If the shell is to be executed in a Windows Git bash session, then you do have access to Windows commands, like taskkill, in said bash session.
For example, in my bash session, I can type:
vonc#voncav MINGW64 /d/git/gtarsum (master)
$ taskkill
ERROR: Invalid syntax. Neither /FI nor /PID nor /IM were specified.
Type "TASKKILL /?" for usage.
In your case, a taskkill /F /IM spigot should be enough.

On Windows I use the Git bash in combination with PsTools. So in my opinion you can do a lot of cool things with Windows, too. I will give an easy example. In Linux you can run ps aux | grep spigot. With your Git bash it is ps aux -W | grep spigot. If you would know the <pid> you could kill it by using the pid. In Linux this would be kill -f <pid>. With Git bash it is kill -f -W <pid>. So you need the -W argument to access processes running in Windows. The Git bash sadly does not support a pkill command. An alternative to taskkill /F /IM spigot could be using PsKill from PsTools. You could simple run PsKill -t spigot.

Related

Open several terminals from shell script and then close them

I am new to shell scripting and I am not sure how to approach this problem.
I have looked all over Google but I couldn't find a relative answer.
My problem:
I have a shell script that executes two gnome terminals and then do some work.
When the work is done I would like to close both gnome terminals that were opened at the beginning of the shell script.
#!/bin/sh
gnome-terminal -x sh -c "./manage.py runserver; bash"
gnome-terminal -x sh -c "yarn start; bash"
...
Some other work
...
kill gnome-terminal
kill gnome-terminal
kill shell script
I have tried looking for the child processes id of the shell script and kill them but it did not work.
Any help would be appreciated
Note: it needs to be done only with the default Linux packages since this is part of a project that many people use and I cannot enforce installation of different libraries.
If you insist on using Gnome Terminal for this and not some sub-shell or multiplexer as suggested in comments, you can keep the PID of each executed terminal by saving the value of $! (last command's PID), and then kill them by PID:
#!/bin/sh
gnome-terminal -x sh -c "./manage.py runserver; bash" &
TERM1_PID=$!
gnome-terminal -x sh -c "yarn start; bash" &
TERM2_PID=$!
#...
#Some other work
#...
kill $TERM1_PID
kill $TERM2_PID
I have been given a green light to drop the usage of gnome terminal and have implemented #tripleee's suggestion:
python manage.py runserver > ../log 2>&1 &
some other commands here
yarn start:3030 > ../log2 2>&1 &
It seems to work and the output from each of the 2 commands it directed to log and log2 respectively.
For killing those 2 applications I am using:
fuser -k <app1 port number>/tcp
fuser -k <app2 port number>/tcp

Running multiple commands in a batch script for wsl

I have a batch script which is used to launch 2 bots in WSL at Windows login.
C:\Windows\System32\bash.exe -c "/home/roughnecks/go/bin/irchuu"
C:\Windows\System32\bash.exe -c "node /home/roughnecks/bots/Birba/birba.js"
My problem is that only the first line runs, I guess because it outputs stuff in the terminal and "birba.js" doesn't fire unless I "ctrl-c" in terminal, exiting irchuu.
I already tried different combinations, like using "nohup command &" or "command 2>&1 &" but nothing is working as expected and I need help.
Thanks
Windows bash does not seem to support & to fork a command and continue.
From this Windows-related question START /B was suggested which seems to work:
START /B C:\Windows\System32\bash.exe -c "/home/roughnecks/go/bin/irchuu"
C:\Windows\System32\bash.exe -c "node /home/roughnecks/bots/Birba/birba.js"

Git Bash closes shell window on Ctrl+C. How to enable standard behavior - exit running operation

On Windows 8.1.
I have written a simple .sh script to start up my dev environment. I know, I can use Windows native batch script (and it works fine, has no given problem), but I prefer Git Bash. The problem is that every Git Bash window opened by my script is closed on Ctrl+C. And I don't want them to get closed but only exit running processes.
Here is my script. It opens four Git Bash windows and starts processes within them. And when I strike Ctrl+C in one of those four windows, the window just closes. Kills the process (except nginx; nginx continues working) and closes. And I only want to stop the process, not terminate the window:
#!/bin/bash
cd /c/nginx
start sh.exe --login -i -c "nginx"
cd /c/Users/user/app
start sh.exe --login -i -c "NODE_ENV='development' nodemon"
start sh.exe --login -i -c "NODE_ENV='development' gulp mytask"
start sh.exe --login -i -c "NODE_ENV='development' compass watch"
How to do it?
If you use a wrapper like Console2 or ConsoleZ around git bash with it's shell pointed to "C:\Program Files\Git\usr\bin\sh.exe" --login -i or "C:\Windows\SysWOW64\cmd.exe /c ""C:\Program Files\Git\usr\bin\sh.exe" --login -i"" it works fine. I'm not sure how to do it without having that wrapper, but it's pretty cool to use one anyway so you could try it out!

Start background process from .bashrc

I realize that this might be a stupid issue but I can't solve it for hours even if digged stackoverflow site and google throughly.
Here is the base code in .bashrc to start gkrellm once I am logged into shell
if ps ax | grep -v grep | grep gkrellm > /dev/null
then
echo "gkrellm is already running"
else
command gkrellm &
fi
I already used to try
...
else
nohup gkrellm &
fi
...
and
...
else
gkrellm
$GK_PID=`pidof gkrellm`
disown -h $GK_PID
fi
...
gkrellm is properly put as background job and I can use shell (as expected). BUT I still have gkrellm exiting once I press Ctrl-c even if I start other apps from that same shell. How do I prevent gkrellm from closing on Ctrl-c press?
Just in case. I am using PuTTY clone called KiTTY but believe that's not it's issue.
Thanks for help!
Almost forgot about this issue and answering my own question after found a working solution long ago ;) Follow works great in my .bashrc for years
mygkrellm()
{
if pidof -x "gkrellm" >/dev/null; then
echo "Gkrellm is already running. Go to shell!"
else
nohup "/usr/bin/gkrellm" &
fi
}
Try replacing "nohup gkrellm &" with this:
screen -S gkrellm -d -U -m gkrellm
This will start a detached screen session running gkrellm, and it won't care about the current shell session. I'm not sure if starting it from .bashrc is the best solution though, it would make more sense to use your window manager's autostart features.
Edit: Not sure if I read the question correctly, are you using KiTTY to connect to a linux host and running gkrell remotely through X forwarding? If that is the case, you obviously can't use window manager features. :)
using bash (disown and &>/dev/null)
you need to run the app in the bg (gkrellm &) and then disown it
if ps -C gkrellm -o user | grep "$LOGNAME" &>/dev/null
then echo "gkrellm is already running"
else gkrellm & disown
fi
if you want to be more portable and use posix sh
you will need to use nohup (part of coreutils and POSIX)
and also background it (nohup cmd &)
you'd also use .profile instead of .bashrc
if ps -C gkrellm -o user | grep "$LOGNAME" 2>/dev/null 1>&2
then echo "gkrellm is already running"
else nohup gkrellm &
fi
other approaches would include - as #Pontus answered - using tools like dtach, screen or tmux, where the command is executed in a detached environment.
by Pontus:
it would make more sense to use your window manager's autostart features
indeed :) as afaik gkrellm is a GUI app, it's better to autostart it, either using .xinitrc (if your login manager supports it), or your window manager's autostart facilities.

Output PID to file from process executed in bash script?

I've got this simple bash script that starts a server process. I want to output the pid of the server process to a file, pid.txt. After some quick searching on SO, I came up with this approach, but it seems to give me the pid of the bash script, not the server process executed from the script. Note: the --fork is required for my server process to run as a daemon to output data to a separate log file, and I suspect that's causing the issue here based on this previous SO question, hoping there's a way around this.
#! /bin/bash
./mongo-linux64-202/mongod --fork &
pid=$!
printf "%s\n" "$pid" > pid.txt
Might I suggest:
#! /bin/bash
./mongo-linux64-202/mongod --pidfilepath ./pid.txt --fork &
derived from Mongo help:
mongod --help
./mongo-linux64-202/mongod --fork &
pid=$(jobs -p | tail -n 1)
Though look first whether mongod is willing to report its pid somehow.

Resources