Vagrant shell incompatible with foreground "server boot" commands - shell

I'm running a shell script on vagrant up via the inline shell config of a Vagrantfile. One of the commands starts up a tomcat web server which normally runs in the foreground.
My dilemma is that a) the commands in the vagrant shell script should exit or run in the background so that the prompt returns to the user correct, and b) if I send the output to the background with & the output isn't visible and the user has no idea when the web server has finished booting.
I either need a way to send output to the background and tell the user when the server has booted, or a way to send to the background once the server has booted. Without messing with the maven/tomcat side I don't see a way to do it.
$script = <<-SCRIPT
# other commands here
mvn tomcat7:run &
SCRIPT
config.vm.provision "shell", inline: $script, privileged: false, run: "always"

I use nohup command for this and redirect the output of the command in the specific log file - It does not fully answer the and tell the user when the server has booted
here's an example of a command I run
nohup java -jar /test/selenium-server-standalone-$1.jar -role hub &> /home/vagrant/nohup.grid.out&
If from the provisioning shell you'd want to give as much as possible information to the user, you could use sleep like 5/10 seconds (depending your deployment) then run tail -20 <log_file> so that would give users a good status of the progress of the task

Related

Is there a way to get my laptop to beep from within a bash script running on a remote server via SSH?

I have a bash script that I have to regularly run on a remote server. Part of the script includes running a backup which takes a while, and after it has run, I have to hit "Y" to confirm that the backup worked before the script will continue.
I would like to know if there is a way to get my laptop to make a beep (or some sort of sound) when that happens. I know that echo -e '\a' makes a beep, but if I run it from within a script on the remote server, the beep happens on the remote server.
I have control of the script that is being run, so I could easily change it to do something special.
You could send the command through ssh back to your computer like:
ssh user#host "echo -e '\a'"
Just make sure you have ssh key authentication from your server to your computer so the command can run smoothly
In my case the offered solutions with echo didn't work. I'm using a macbook and connect to an ubuntu system. I keep the terminal open and I'd like to be informed when a long running bash script is ready.
What I did notice is that if I shutdown the remote system then it will beep the macbook and show an alarm icon on the relevant tab. So I have now implemented a bit of dirty workaround:
sudo shutdown 1440 && shutdown -c
This will initiate the system to shutdown and will immediately cancel the request. And I do get the alarm beep + icon. You will need to setup sudo to allow the user to permit shutdown. As it was my own remote server it was no problem but could limit the usability for others.

Script executed in EC2 User Data with nohup doesn't run until manually SSHing in

I have the following command in my EC2 User Data:
setsid nohup /root/go/src/prometheus-to-cloudwatch-master/dist/bin/prometheus-to-cloudwatch --cloudwatch_namespace TestBox/Prometheus --cloudwatch_region eu-west-1 --cloudwatch_publish_timeout 5 --prometheus_scrape_interval 30 --prometheus_scrape_url MyScrapeUrl &
This script will take metrics published at the prometheus_scrape_url and push them to CloudWatch. By default this script runs in the foreground and every 30 seconds will output the number of metrics pushed to CloudWatch. I have added setsid nohup to run the script in the background in a new session.
The issue here is that the script doesn't seem to run until I SSH into the box following initialisation and su to the root user (it's like it's queued to be run when I next SSH as the root user).
My expected behaviour is that the script runs as part of the user data and I should never need to SSH into the box.
The script in question is: https://github.com/cloudposse/prometheus-to-cloudwatch

How to tell Bash to not stop the simulations when ssh disconnects?

I am running some simulations on another machine via ssh. Here is what I do
ssh username#ipp.ip.ip.ip
Go to the right directory
cd path/to/folder
And then I just call my executable
.\myexecutable.exe
The issue is that every time the ssh disconnect, the simulations stops. How can I make sure the simulations doesn't stop on the other machine? Will I somehow receive potential error messages (assuming the code will crash) once I reconnect (ssh)?
You should launch a screen or tmux to create a terminal from which you can detach, leave running in the background and later reattach.
Further reading:
http://ss64.com/osx/screen.html
https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/screen.1.html
You may also want to try out Byobu:
http://byobu.co
run your command as follows : nohup ./myexecutable.exe >nohup.out 2>&1 &
The & is to run the command in the background
The >nohup.out 2>&1 sends your stdout and stderr to nohup.out)
Note the '/' as opposed to '\' - which won't work on osx

decouple process remotely launched from local machine

I need to ssh to a machine and launch a bash script running some hour-long tests which require no human interaction for their entire execution.
Is there any way I can decouple my running script from my shell, so that I can close the terminal and shut down my local computer as I like?
Sure, use nohup:
nohup ./program &
Alternatively, start your program inside screen or tmux and then detach.

Terminal Command: start or stop process if it's running or not

I have a glassfish install and I would like to have a simple terminal command checking if it's running. If it is, it will shutdown the process. Or if it not running, it will start the server.
To start the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
To stop the server I can enter:
/Applications/NetBeans/glassfish-3.1.1/bin/.asadmin stop-domain domain
I will would like to make a simple Alfred.app script than can start this domain if it's not running, or stop it if it's running.
One way to do this is to note in a file the process ID of the server when it starts, and have another script check to see if that process is running.
For example, script A (to run the server unconditionally):
#!/bin/sh
/Applications/NetBeans/glassfish-3.1.1/bin/./asadmin start-domain domain
# file name is arbitrary
pgrep whateverTheProcessNameIs > ~/.glassfish-server.pid
And in script B:
#!/bin/sh
pid=`pgrep -F ~/.glassfish-server.pid` # file chosen in script A
if [ "x$pid" = "x" ] ; then
# process has died; restart by running script A
/path/to/scriptA
fi
Note that only Mac OS X 10.8 (Mountain Lion) installs pgrep by default; otherwise you'd have to use some other method (like parsing ps output) to see what processes are running.
As far as how to run this check periodically, there are various ways. I'm assuming Alfred will run any script that is executable (chmod +x scriptA scriptB) but I don't know for sure.

Resources