Best way to restart a golang server on file saves - linux

I have API code written that exists in my $GOPATH but the main file is elsewhere on the system. I'm trying to get my main file to exit and start and again whenever certain files are saved. The closest I've gotten is by using a combination of find and entr:
find $GOPATH/github.com/example/example -path $GOPATH/example/example/vendor -prune -o -name '*.go' -print | entr -r go run /vagrant/script/api/main.go
But for some reason entr fails to shut the service down before starting it again resulting in the error message:
ListenAndServe: listen tcp 127.0.0.1:1456: bind: address already in use
Open to any solution that allows live reloading of the go server, but the less configuration/setup required the better as I'd like to reuse the solution in multiple projects.
Not sure this is an issue, but I should also note that I'm using vagrant-fsnotify to touch changed files in my Vagrant guest machine when saved on the host machine.

Per the comments, you're using an old version of entr which is killing only the go run process, leaving your Go program still running. Running version 3.1 or newer of entr will also send the termination signal to your Go executable, which should resolve the issue.
If at all possible, upgrade entr to the current version (3.6), or at least 3.1+. If that's not possible, one solution would be to write a wrapper program that handles the termination signal for you. That program would run go run and watch for the termination signal. Upon receiving that signal, your wrapper would kill both go run and your Go program.

Related

Make chosen version of Elasticsearch run as a service in Linux

I have an issue with later versions of ES, so have to use 7.10.2 currently.
This means that the previous method I used to install ES as a service, i.e. apt-get, doesn't work You can't choose an older version this way: it currently installs 7.16.3.
So I followed the procedure on this page for 7.10: everything worked: I was able to run ES as an app and also as a "daemon". Clearly I could simply put the "daemon" startup line in a script which runs on boot.
But what's the optimum way of turning this "daemon arrangement" into a service which you can control with systemctl, and which starts automatically when the machine boots?
PS I don't want to get involved with Docker. I'm sure that's a useful thing but I'm convinced there is a simpler way of doing it, using available Linux sys tools.
I found a workaround... this doesn't in fact create a service of the "systemd" type which can be controlled by systemctl. There seem to be one or two problems which make this non-trivial.
1) You can't start ES as root! I assume (not sure) that most services are being run by root. Anyway this was something I couldn't find a solution to.
2) I am not sure whether a shell script file called by a service is allowed to end... or should continue endlessly: initially I thought this would be sufficient. This is a shell script (run_es_daemon.sh) which does indeed start up ES (as a daemon process) when run by manually in a terminal. There is no issue to do with the fact that the script ends and you then close the terminal: the daemon process continues to run:
#!/bin/bash
# start ES as a daemon...
cd /home/mike/Elasticsearch/elasticsearch-7.10.2
./bin/elasticsearch -d -p pid
... but it never worked using a xxx.service file in /etc/systemd/system/ (maybe because of 1) above). So I also tried adding these lines under the above ones:
while true
do
echo "bubbles"
sleep 60
done
... didn't work either.
In the end I found a simple workaround solution was to start up the daemon process by using crontab:
#reboot /home/mike/sysadmin/run_es_daemon.sh
... but I'd still like to know how to set it up as a true service, which starts at boot...

How to set up a bash script to run in the background anytime the linux ubuntu server is running

I have written up a simple bash script that will copy the newest image from my ip camera into a directory, rename the file and delete the old file. The script loops every 10 seconds.
I want to have this script start running in the background and run continuously all the time that the server is up.
I understand the part about adding a & to the end of the command will cause it to run in the background.
Is init.d the best place to execute this?
I am running ubuntu server.
This sort of thing is normally done by service scripts, which you would find under /etc/init.d. Depending on the version, that might be a "System V init script", or one of the systemd scripts.
A simple service script of the sort you are asking about would start automatically (based on comments in the script's header that tell what run-levels it would use), create a file under /var/run telling what process-id the script uses (to allow killing it), and run the copying in a loop, calling sleep 10 to space the timing as indicated.
A typical service script should implement "start", "stop", "restart" and "status". Not all do, but there is rarely a good reason to not do this.
On my (Debian) system, there is a README file in the directory which is a good introduction to the topic. There are several tutorials available for the topic. Here are a few:
Linux: How to write a System V init script to start, stop, and restart my own application or service
Writing a Linux Startup Script
Manage System Startup and Boot Processes on Linux with Upstart

Linux Invoke custom script during OS shutdown

I wrote a simple JAVA application which runs as a service. When my application is up and running, I maintain the PID in a file which will be used while stopping the application.
Issue:
When I restart the OS the stop script is not called (not sure how to make this happen) and the old PID is left as it is in the PID file. Now, after reboot (which start my app) when I stop the app using stop script now my stop script will try to clean up all the PID listed in the file. Most of the time, I will get "No such process". But there are chance the same PID might have been used for some other process
Question:
How I can make sure my stop script will be invoked when I shutdown ore reboot the OS? I am looking a solution for RHEL environment.
I think your are looking for a init script. (startup/shutdown services at different run levels)
This is a good reference
http://blog.rimuhosting.com/2009/09/30/one-java-init-script/
this has a good refernce to Linux init.d scripts
http://coreymaynard.com/blog/creating-a-custom-initd-script-on-fedora/
Hope it helps
If you are looking for scripts that run after reboot, I guess you can write the script in /etc/rc.local and then you can start your service.
This script will run after all your init scripts have run while your machine starts. Using this you can delete the old PID file.

Starting a process when Linux starts (Ubuntu)

I have a process (Spark chat client) which needs to be run when my Ubuntu boots up. For this I have done followings.
I created a run.sh file which will fire up my application (and I check it's working)
I created a symbolic link from both /etc/rc5.d/ and /etc/rc3.d/ to my run.sh file. (A symbolic link is also working fine)
But my processes don't start up when my machine boots. (Is this the way to do it or am I doing the wrong thing here?)
I'm running on Ubuntu 10.04 LTS (Lucid Lynx).
Your solution would've worked in most Linux distributions. However, Ubuntu never goes past runlevel 2.
Just in case, this means the contents of rc?.d with ? > 2 are not used unless you manually raise the runlevel as root. Use rc2.d :)
The symlinks you created in /etc/rc5.d/ and /etc/rc3.d/ should be named S##name. S is for start, and the number ## gives an order in which the scripts are run.
Note also that the symlinks in these directories usually points to the actual script located in /etc/init.d/.
It looks like you want to run an X program when a user logs in, not a service on startup. Remember, in Linux there is no GUI; X is a program that runs to display graphics on the screen.
You likely want to set up a program to start on KDE/Gnome login. Each has their own way to do it, but is generally boils down to pointing at a script and saying "Run this."
Put the command to run that script in the /etc/rc.local file. I think it will run each time you log in to the system.

Compiling C++ on remote Linux machine - "clock skew detected" warning

I'm connected to my university's small Linux cluster via PuTTY and WinSCP, transferring files using the latter and compiling and running them with the former. My work so far has been performed in the university's labs, but today I have been doing some work at home that generated an interesting warning.
I uploaded an entire folder of stuff and, upon running the make command, I get this as the last line of output:
make: warning: Clock skew detected. Your build may be incomplete.
The resulting binary works correctly, and there doesn't seem to be any other unexpected errors in the build process.
I seem to be able to trigger the error by building after uploading some new / replacement files (I edit everything locally then upload the new version), so I'm wondering if it's something just as simple as mismatched file modification times? Or something more concerning?
So, should I be worried? How do I fix/prevent this?
That message is usually an indication that some of your files have modification times later than the current system time. Since make decides which files to compile when performing an incremental build by checking if a source files has been modified more recently than its object file, this situation can cause unnecessary files to be built, or worse, necessary files to not be built.
However, if you are building from scratch (not doing an incremental build) you can likely ignore this warning without consequence.
Typically this occurs when building in a NFS mounted directory, and the clocks on the client and the NFS server are out of sync.
The solution is to run an NTP client on both the NFS server and all clients.
Install the Network Time Protocol
This also happened to me when running make on a Samba SMB CIFS share on a server.
A durable solution consists in installing the ntp daemon on both the server and the client.
(Please, note that this problem is not solved by running ntpdate. This would resolve the time difference only temporarily, but not in the future.)
For Ubuntu and Debian-derived systems, simply type the following line at the command line:
$ sudo apt install ntp
Moreover, one will still need to issue the command touch * once (and only once) in the affected directory to correct the file modification times once and for all.
$ touch *
For more information about the differences between ntp and ntpdate, please refer to:
Time Synchronisation with NTP
How To Set Up Time Synchronization on Ubuntu 16.04
Simple solution:
# touch filename
will do all OK.
For more info:
http://embeddedbuzz.blogspot.in/2012/03/make-warning-clock-skew-detected-your.html
The other answers here do a good job of explaining the issue, so I won't repeat that here. But there is one solution that can resolve it that isn't listed yet: simply run make clean, then rerun make.
Having make remove any already compiled files will prevent make from having any files to compare the timestamps of, resolving the warning.
type in the terminal and it will solve the issue:
find . -type f | xargs -n 5 touch
make clean
clean
According to user m9dhatter on LinuxQuestions.org:
"make" uses the time stamp of the file to determine if the file
it is trying to compile is old or new. if your clock is bonked, it may have problems compiling.
if you try to modify files at another machine with a clock time ahead by a few minutes and transfer them to your machine and then try to compile it may cough up a warning that says the file was modified from the future. clock may be skewed or something to that effect ( cant really remember ). you could just ls to the offending file and do this:
#touch <filename of offending file>
I have had this in the past - due to the clocks being out on the machines. Consider setting up NTP so that all machines have the same time.
This is usually simply due to mismatching times between your host and client machines. You can try to synchronize the times on your machines using ntp.
The solution is to run an NTP client , just run the command as below
#ntpdate 172.16.12.100
172.16.12.100 is the ntp server
Replace the watch battery in your computer. I have seen this error message when the coin looking battery on the motherboard was in need of replacement.
(Just in case anyone lands here)
If you have sudo rights one option is to synchronize the system time
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
Make checks if the result of the compilation, e.g. somefile.o, is older than the source, e.g. somefile.c. The warning above means that something about the timestaps of the files is strange. Probably the system clocks of the University server differs from your clock and you e.g. push at 1 pm a file with modification date 2 pm. You can see the time at the console by typing date.
This happened to me. It's because I ran make -j 4 and some jobs finished out of order. This warning should be expected when using the -j option.

Resources