bash process running in the background - bash

I'm connecting to a remote server running Ubuntu 16.04 using ssh. I'm running nodemon in a bash session but my network connection drops out if I walk away from my computer or close my laptop. This locks up my sessions and I have to close the terminal window. When I reconnect I'm unable to restart nodemon because it's running as a process in the background.
Is there a way to re-open the bash window that locked up? What I've been doing is killing the nodemon process or restarting the system. I'm hoping there's a simpler way.

Use GNU screen. It runs a virtual terminal that stays open even if you loose your connection.
On the server, enter screen -S myscreen and do your normal command line work. When you disconnect, just open a new ssh connection to the server an execute screen -r myscreen. Your old session will be there as if you never left it.
You can manually leave the screen whithout killing it, by pressing ctrla and then d.

I would recommend installing the utility 'screen.'
You can install it in Ubuntu with:
apt-get install screen
Or in Red Hat / CentOS with:
yum install screen
Then you can just enter the command screen to start a session. Then you can start whatever script you need to stay running regardless of disconnects. Ctrl-A+Ctrl-D detaches you from the session. Then screen -r reconnects to it. You can also preceed a command with screen -d -m and that process will start in a screen session.

Related

Running wsl results in starting a Jupyter Notebook

I have installed Anaconda under Windows and Ubuntu under WSL. I have not used this Ubuntu installation for a long time. When I tried to run Ubuntu now, I saw the following:
That is, WSL starts a Jupyter Notebook server, which I have to kill by hitting Ctrl-C to get the Ubuntu prompt. What could be causing this behavior?
There are several ways for something to "autostart" when running WSL:
First, you (or an application) may have modified your startup scripts to start the Jupyter Notebook server. To see if this is the case, try starting WSL from PowerShell by running:
wsl -e bash --noprofile --norc
This will run bash without the startup scripts. If this brings you to the prompt without running the notebook sesrver, then the problem is in your ~/.bashrc or ~/.bash_profile (assuming bash is your default shell).
Given the symptoms you are seeing, this is the most likely cause. If that's the case, look in those files to see if you can find the line that is starting the server, and comment it out.
Second, and related to the first, do you recall trying to enable Systemd at any point using something like Genie or WSL2Hacks? If so, then I believe they modify your start scripts to run Systemd, which can be used to start other services. However, I would expect those Systemd-executed services to start daemonized, in the background, and not interrupt your shell's startup.
Under Windows 11, services can also be autostarted via /etc/wsl.conf, but again, they would be started by the root user in this case, and in the background. I can't think of a non-pathological way that this could be used to interrupt the user's startup shell experience.

XQuartz $DISPLAY problems, cannot open xclock locally, cannot ssh -X

I am trying to solve a cascading series of bugs that started with me not able to copy to my macOS clipboard from remote ssh and has lead me to realize my X11 situation is seriously messed up. I have read a few other stackoverflow threads and they do not address my particular problems.
First my setup is macOS Mojave 10.14.5. I have xquartz 2.7.11 installed from the website. When I run echo $DISPLAY locally (on macOS) I get /private/tmp/com.apple.launchd.waagOnO6Qm/org.macosforge.xquartz:0.
Since I don't know where the error actually is I will list two problems I can identify currently.
Two problems:
If I run xclock locally nothing happens inside my terminal. I do notice that an "active" dot appears under the XQuartz dock icon for a second and then disappears. But after this happens my terminal still just hangs at xclock as though it is running.
If I try to ssh -X remote into a remote machine my terminal is locked out. I cannot keyboard interrupt. I ran this with -vvv to try to debug and I see that it hangs with xauth:
debug2: client_x11_get_proto: /opt/X11/bin/xauth -f /var/folders/jw/ltyk9x9n0_xb61jhdnct27fr0000gn/T//ssh-vcqwT7qh5yk2/xauthfile generate /private/tmp/com.apple.launchd.waagOnO6Qm/org.macosforge.xquartz:0 MIT-MAGIC-COOKIE-1 untrusted timeout 1260 2>/dev/null
Attempts to Solve
Other related stack threads have suggested reinstalling XQuartz, which I have done, both manually and with Homebrew. I have logged back out and in following reinstallation.
This thread suggested I solve my xauth problem by deleting .XAuthority file and recreating it. However, when I
xauth generate :0 . trusted
My XQuartz pops up a window saying XQuartz quit unexpectedly which I can provide the Report for if it helps. Then in the terminal it says
xauth: (argv):1: unable to open display ":0". Also I'm not sure this is the problem anyway because my .XAuthority file already contained an entry that it looks like this is trying to produce:
$HOST/unix:0 MIT-MAGIC-COOKIE-1 db7738324ca3662767b20b97b4a68680
Though it is concerning that running xauth is causing my xquartz to repeatedly quit unexpectedly (this dialog box is appearing multiple times).
This has been very frustrating to debug because I am not sure where the problem is, with xauth or xquartz somehow even though it is newly installed. Further, existing StackOverflow threads I have found detail the problem only with ssh -X but clearly I'm having problems locally, given that I can't even run xclock.
Any help is greatly appreciated.
The dot appearing and disappearing quickly indicates that the server process is terminating. This means that either the server crashed, the managing client (eg ~/.xinitrc) terminated, or we failed to even start xinit.
Almost every case I have ever seen of this has been due to someone doing something wrong in their init scripts (eg: ~/.bash*, ~/.profile, ~/.xinitrc).
Remove those and try again, then bisect to figure out the underlying issue.
However, the crash dialog from your "ssh" case indicates that it is likely the server crashing. You will need to look at the crash log for more information (or provide it here if you want help with that).

OSX X11 Error cannot open display

I am trying to run any GUI container I can on MacOS. With every container I try (firefox, chrome, tor, spotify, etc) I always get the error Error: cannot open display. And it's not specific to the docker run command where I pass the environment flag with my $DISPLAY. When I try to run xhost + I get the same error.
I have a fresh XQuartz installation. It is up and running. I have turned on "allow connections from network" under security. I've tried building my own images and pulling jessie frazelle's images. I do not suspect it is a docker issue or the Dockerfiles. It is something on the host, my laptop. I can't seem to figure out what it is.
MacOS Sierra 10.12.5
Docker 17.12.0 Stable
XQuartz 2.7.11 (xorg-server 1.18.4)
My local's $DISPLAY is set to :0.0
So I finally got this to work. And it seems it was pretty simple. I am not certain how this actually fixes the issue, but now the containers work.
How I fixed it was opening up XQuartz and then opening up the "Terminal" app from the "Applications" menu. Then running the command export DISPLAY=192.168.1.X:0, then xhost +. It outputted something like this "access control disabled, clients can connect from any host". After that I was able to run my docker run commands to launch the desired GUI containers.
I am still uncertain how this works and not running them from my laptops Terminal app, but it worked. It must be something I have set in my local env. Hopefully this helps someone else out who may be running into the same issues.
Based on #Byron's answer, I've found out that I could get it to work by running these 2 commands in the normal terminal:
export DISPLAY=:0
/opt/X11/bin/xhost +

Vagrant on Windows - any way to stop provisioning with Ctrl-C?

Very often when running ansible-playbook on the Vagrant VM from Windows, I need to stop in the middle of something by pressing Ctrl+C. This happens if ansible becomes unresponsive or there is some bug we need to fix asap, so there is no point of waiting until provisioner completes.
The probem is that Ctrl+C does not work, some 2 ruby.exe processes get stuck in process tree. Any subsequent vagrant commands fail until you manually kill these ruby processes.
I also use to kill all stucked python ansible processes on the VM before running new provision.
Any way to handle it more jently?
I found this problem as well on Windows and using Puppet Apply. The only way I can happily kill it by opening another terminal/cmd and then vagrant ssh -- sudo pkill puppet. That gracefully terminates the process, and allows me to regain control of my first terminal again.
In short the solution is:
Take a terminal that works.
I find one working gitbash v2.32.0.windows..
The latest available gitbash currently is v2.38.1. But only the old one is working correctly with Vagrant(Oracle VM). The strange thing is that the latest one (gitbash v2.38.1) is working fine with SSH connections to AWS EC2 instances.
Alternatively. Windows PowerShell is working fine with Vagrant(Oracle VM).
If someone needs my bad experience, here it is.
The following terminals DON'T WORK
gitbash v2.38.1 (latest for now)
gitbash V2.36.0
ConEmu v220807 Alhpa (latest for now)
cmder v1.3.20.1282 (latest for now)
I propose using vagrant halt.

What is the command/configuration to run NodeJS/Express app as a daemon?

In SSH if I run:
$ node server
My web app works fine until I close the SSH session. What is the command or configuration needed so that it runs all the time without an active SSH session?
you can run it like:
nohup node server
Or you can install forever.
npm -g install forever
forever start server
If you have multiple nodejs, you can use pm2 to manage them.
Or, use screen or tmux to keep the nodejs running while detach the session.

Resources