Kill ListenAndServe in GoLang - go

I am trying to use Sublime Text + GoSublime to do Golang development.
I am working with a webserver in Go (ListenAndServe("8000", &handler)).
The problem I have is that when I run the Go process (while another one is running), the second process doesn't work (since the port is already in use).
What I want to happen is that when I go run web.go, if one was already running, shut that down before running this one.
Sublime Text 3's build system doesn't seem to keep track of the previous execution - so I don't know the best way to terminate. When I run CTRL+B it doesn't block, it executes the build system in another thread - meaning that I can't CTRL+C before executing the next. So what I'm having to do is terminate via the command line the existing process before starting the next one.
So I'm looking for either a solution with Sublime Text's build system configuration, or perhaps even having the Go process itself check to see if one is running and killing the process.
Ideas? It's sort of annoying to swap back and forth from terminal.

Not sure if I understand the question right.
But in sublime text build console, there is tskill command which can kill active tasks initiated from the console. So you can type Ctrl+B to invoke out console and type tskill to kill the running web.go task. There are also other alias tskill <task ID>, task replay or task go. Reference is here. After that you can run other commands including go run.
What's more there is a replay command. "It is like run but attempts to cancel any active executions before running". I feel it looks like what you want.
Further, Gosublime command shell has sh command mode. It's much flexible for example type sh killall web;go run web.go or run a shell script to run that.
Hopefully it helps.

Related

Bash shell is locked after running script

I made my own script to make testing a little faster to execute on my .NET projects.
The name of the script is TestCoverage and is located in /home/user/bin folder with executable permissions and also added to my PATH for convenience.
#!/bin/bash
echo "Collecting TestCoverage..."
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
The script runs as expected but after it's done the terminal stops taking input. I can only press the Enter key and the cursor is blinking as usual.
Bash version 4.4.20 running on Ubuntu 18.04
I've made some testing and it seems to take input but it isn't showed to the console. The problem is not consistent, it comes and goes through different terminal sessions.
Your script (and likely the dotnet process it invokes, because echo is a shell intrinsic that usually returns within milliseconds) is probably still running and hasn't returned.
You can open another console and type ps aux -H to view the running processes - look out for some adjacent lines with TestCoverage, bash, and dotnet.
In those lines, you can see what state the process(es) are in
(RTM for further detail).
If you are running a console under X, you can first look around whether you accidentally pushed some running dotnet test ... window behind your console window. :-)

Mac Terminal: "Initializing jdb ..." on start up

As soon as I start the terminal on my Mac, I get the message "Initializing jdb ..." and it remains in this state until I terminate the process manually with a Ctrl+C. The other option is to start a JVM at on particular port and attach the jdb to that port. But subsequent windows of the terminal still results in the same "Initializing jdb ...". This has been occurring only recently on my machine.
Can someone throw some light on how I can stop this?
You must be (directly or indirectly) invoking jdb in one of your shell startup scripts, like ~/.bashrc or ~/.bash_profile, which run every time you create a new terminal. Don't do that. Remove that command (or commands) from your shell startup file(s) and invoke jdb manually when you want to start using it.
If there's some non-trivial code for starting jdb, wrap it in a shell function to make it convenient to invoke.
If it's your goal to have jdb always running, use a launchd job to start it at login instead of using Terminal to start it.

In Bash, how can I tell if I am currently in a terminal

I want to create my own personal logfile that logs not only when I log in and out, but also when I lock/unlock my screen. Kindof like /var/log/wtmp on steroids.
To do this, I decided to run a script when I log into Ubuntu that runs in the background until I quit. My plan to do this is to add the script to .bashrc, using ./startlogging.sh & and in the script I will use trap to catch signals. That's great, except .bashrc gets run every time I open a new terminal, which is not what I want for the logger.
Is there a way to tell in Bash that the current login is a gnome login? Alternatively, is there some sort of .gnomerc I can use to run my script?
Edit: Here is my script:
Edit 2: Removed the script, since it's not related to the question. I will repost my other question, rather than repurpose this one.
Are you looking for a way to detect what type of terminal it is?
Try:
echo $TERM
From Wikipedia:
TERM (Unix-like) - specifies the type of computer terminal or terminal
emulator being used (e.g., vt100 or dumb).
See also: List of Terminal Emulators
for bash use : ~/.bash_logout
that will get executed when you logout, which sounds like what you are trying to do.
Well, for just bash, what you want are .bash_login/.bash_logout in your home directory (rather than .bashrc) These are run whenever a LOGIN shell starts/finishes, which happens any time you log in to a shell (on a tty or console, or via ssh or other network login). These are NOT run for bash processes created to run in terminal windows that you create (as those are not login shells) so won't get run any time you open a new terminal.
The problem is that if you log in with some mechanism that does not involve a terminal (such as gdm running on the console to start a gnome or kde or unity session), then there's no login shell so .bash_login/logout never get run. For that case, the easiest is probably to put something in your .xsessionrc, which will get run every time you start an X session (which happens for any of those GUI environments, regardless of which one you run). Unfortunately, there's no standard script that runs when an X session finishes.

how to close geany terminal

Whenever a program finishes to run when executing it with geany, the terminal doesn't close.
geany terminal waits for a keystroke to close. How do I change this behavior and make it close automatically when the program reaches it's end? Is there a script I can write?
This is not really an answer, but more like an alternative suggestion. In newer versions of Geany you can tell it to run programs in the Vitual Terminal Emulation, i.e. the terminal tab at the bottom (Settings -> Terminal -> Run Programs in VTE).
This way, the program starts more quickly (without first starting a terminal), you can still see the output generated by the program (exception stacktraces!), and the focus/cursor stays in the editor window, too.
I actually found a way of doing what you want to do in the terminal instead of using Geany's VTE. The perk of that is that you can run multiple scripts at the same time (or a single one multiple times), while the VTE only executes one script at the time (at least that's the case for me).
Just go to Build -> Set Build Commands. The last type of options says "Execute commands". The standard option is python "%f".
Just add && exit so that it reads
python "%f" && exit.
That way the terminal window will close instantly once it's finished if the programm doesn't give you an error code. If it does, the error message will be displayed and the window is kept open.
A variant of pohly's answer worked for me:
In Build -> Set Build Commands -> Execute commands
I replaced "./%e" by "./%e" && exit
better this way, let the terminal to stay for a moment so that we could see the results in case you want to
python "%f" && sleep 20 && exit

Running a command as a background process/service

I have a Shell command that I'd like to run in the background and I've read that this can be done by suffixing an & to the command which causes it to run as a background process but I need some more functionality and was wondering how to go about it:
I'd like the command to start and run in the background every time the system restarts.
I'd like to be able to able to start and stop it as and when needed just like one can do service apache2 start.
How can I go about this? Is there a tool that allows me to run a command as a service?
I'm a little lost with this.
Thanks
UNIX systems can handle as many processes as you need simultaneously (just open new shell windows if you're in a GUI), so running a process in the background is only necessary if you need to carry on using the current shell window for other things once you've run an application or process that keeps running.
To run a command called command in background mode, you'd use:
command &
This is a special character that returns you to the command prompt once the process is started. There are other special characters that do other things, more info is available here.
Take a look at the daemon command, which can turn arbitrary processes into daemons. This will allow your script to act as a daemon without requiring you to do a lot of extra work. The next step is to invoke it automatically at boot. To know the correct way to do that, you'll need to provide your OS (or, for Linux, your distribution).
Based on this article:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/
...another good way is with screen eg:
screen -d -m -s "my session name" <command to run>
from the screen manual:
-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
i.e. you can close your terminal, the process will continue running (unlike with &)
with screen you can also reattach to the session later
Use nohup while directing the output to /dev/null
nohup command &>/dev/null &
For advanced job control with bash, you should look into the commands jobs bg and fg.
However, it seems like you're not really interested in running the command in the background. What you want to do is launch the command at startup. The way to do this varies depending on the Unix system you use, but try to look into the rc family of files (/etc/rc.local for example on Ubuntu). They contain scripts that will be executed after the init script.

Resources