Exiting an ongoing Node.js server in linux - linux

When I run my node server node server.js through SSH, the server starts and operates properly, however, I am left at the command prompt on a blank line, and no matter what I try to do, I can't "escape" out of that back to root /]#. Is it necessary for me to terminate the session completely and rerun a new SSH session to be able to operate on the prompt again?
I'm sure this is a completely basic problem with a completely basic solution, I just can't for the life of me seem to "escape" out of this back to the command line. It would seem unlikely that it would be necessary to completely exit out of PuTTy and start a new session just to terminate it..

Going to post as an answer, have you tried pressing Ctrl + C or as Gabi mentioned, Ctrl + D, that usually exits most servers I start via the CLI.

You can use GNU Screen (quick reference here) to simultaneously work with multiple terminal interfaces within existing PuTTy session.

Related

What is the correct way to start and stop simple Node.js scripts

I'm trying to create a simple Twitter bot to learn some Node.js skills.
It works fine on my local computer. I start the script with node bot.js and then close it with Ctrl + C.
I've uploaded the files to a server (Krystal hosting). I've ssh'd into the server and then used $ source /home/[username]/nodevenv/twitterbot/10/bin/activate. Which I think puts me into a Node environment (I'm not really clear what is happening here).
From here I can run node bot.js. My Twitter bot runs fine and I can leave the terminal. What I've realised now is that I don't know how to stop this script.
Can someone explain how I should be doing this? Is there a command I can enter to stop the original bot.js process? Since looking into this it looks like perhaps I should have used something like pm2 process manager. Is this correct?
Any pointers would be much appreciated.
Thanks,
B
You can kill it externally by nuking the process from an OS command line or in an OS GUI (exact procedure varies by OS). Ctrl-C from the shell is one version of this, but it can be done without the command shell that it was started in too by nuking the process directly.
Or, you can add a control port (a simple little http server running on a port only accessible locally) that accepts commands that let you do all sorts of things with the server such as extract statistics, shut it down, change the configuration, tell it to clear caches so content updates take effect immediately, etc... Shutting down the server this way allows for a more orderly shut-down from code within the server. You can even stop accepting incoming connections, but wait for existing http connections to complete before shutting down, close databases normally, etc...
Or, you can use a monitoring program such as PM2 or forever that in addition to restarting the server automatically if it should ever crash, they also offer commands for shutting it down too (which will just send it certain signals kind of like Ctrl-C does).

Trying to launch a process via screen from within ansible

I'm having a slightly weird, repeatable, but unexplainable problem with screen.
I'm using ansible/vagrant to build a consistent dev environment for my company, and as a slightly showy finishing touch it starts the dev server running in a screen session so the frontend devs don't need to bother logging in and manually starting the process, but backend devs can log in and take control.
However, one of the systems - despite being built from scratch - ends up with an immediately dead screen (it doesn't log anything to screenlog). Running the command manually works fine.
(the command being)
screen -L -d -m bash -c /home/vagrant/run_screen_server.sh
I've even gone to the point of nuking everything vagrant/virtualbox related on the system, making sure it's installing a clean, nightly box. Exactly the same source box works all the other machines.
Are there any other debugging steps I can be taking or is there something I'm missing?
I'm right now trying to do the same with my setup and hit the same problem.
Further testing has shown, that sleep 1 right after calling the screen helped. It seems the ssh script that ansible calls exits before the screen call is fully detached (or something else, that would explain that the sleep 1 helps)
I've also found Can't get Fabric's detached screen session example to work with the same suggestion.

How to enter and leave an existing screen through script?

I'm fairly new with Linux shell scripting but have several years experience using Linux(non-hardcore).
There is an application running on my server(accessing it through SSH/putty) which is a console one. In order to ensure that it will run even even if I close my ssh client(putty), I made it run though screen.
In my script, there is a part where I want to enter this specific screen and "leave a message", then leave the screen to proceed with other things it have to perform.
How to do it exactly? Thanks!
(the console application is actually a vanilla Minecraft server)
check out 'screen' window manager http://www.gnu.org/software/screen/ with session names, see the following article:
http://www.mattcutts.com/blog/a-quick-tutorial-on-screen/

Close and reopen ssh connection without losing current process

If I open an ssh connection and start a long-running process, is there any way to close the ssh connection, and not only keep the process running, but be able to later ssh back in again, and "reattach" the process to the terminal?
I am able to do the following:
Ctrl-z
bg
disown
And that lets me keep the process running after I leave my ssh session, but I am not able to "reown" the job later; is there a way to do this? The real-world scenario is that I'd like to start a process at work, drive home, then log back in and check on it/interact with it.
I know that tmux is able to handle things like this, but I am often forgetful, or I just don't know ahead of time what process will be long-running and what won't, so I don't always remember to start the process from within tmux.
There are several ways to accomplish this. I used to use screen and that was a round about way of doing it. But check out mosh, built just for this: http://mosh.mit.edu/

Loss of Vim keybindings after screen detach and SSH logout

On one of the servers I commonly work with (Ubuntu 11.04), I get a hanging logout whenever I do following:
Log in via SSH
Start screen
Run Vim
{edit files, etc}
Detach screen session
Log out of server ("exit")
After doing, it hangs. (says "logout", never seems to actually log out). Furthermore, the next time I log in and reattach my screen session, it seems that all of my key bindings are gone in Vim. This includes the entire functionality of some plugins, like NERDTree.
I tried a few different combinations of these actions. For example, if I exit Vim before detaching my screen, it doesn't do this. It also doesn't do this if I don't run Vim at all, or if I don't run screen at all.
Any idea what could be going on here?
I found out what the issue is. I had my SSH client configured to forward X for that server. Disabling that fixed the problem. I don't really understand the details of why, but that's what it is.

Resources