Reconnecting to a console output after system restart - linux

For a running script on a Linux VM with a regular console output: If I disconnect from the VM the output window disappears. If I restart the VM, the script is still running but how do I get back to the output-screen?

Easy solution: use GNU screen, or an alternative like tmux to run your scripts in a persistent session. Thus, if you accidentally disconnect from your SSH session (or must shut down your computer), you can still reattach to the screen session later.
Tutorial: Using GNU Screen to Manage Persistent Terminal Sessions
Another great feature is that screen can also log the console output to a file. I use it all the time for cron jobs or other unattended tasks. I also use screen for updates (using yum, dnf or whatever), because updates can take a lot of time, and sometimes may even have to restart the network service, which would terminate your SSH session.

Related

Is it possible to attach to a session that is being used by a service similar to the screen command?

I am running a game server as a service using systemctl to start and stop a script that runs the whole thing. I tried to modify the script to let me use a screen so I could attach to the process that the server is being run on, and issue commands. But so far I've not had much luck. Is it possible to attach to services that are running on a server?
This question belongs on the Unix/Linux StackExchange.
See e.g:
https://unix.stackexchange.com/questions/453998/systemd-connect-to-stdin-stdout-after-service-has-started
If you want to solve it via programming, you could consider writing a small web application as the interface instead of the console.
Not in systemd, but you can start the service using
screen -D -m yourservice
which will create a detached screen session that will wait for the process to exit (so systemd does not see the service terminating immediately if you use this in an ExecStart line). You can then attach to that session normally.

Google VM - process persistence

I have a Google VM, and i can start a web server. The command i issue is: python server.py.
My plan is to have the application running.
Since i will eventually close my pc (and thus the terminal), will the application continue running normally?
Or, do i have to start the server and then use disown, to make the app run in the background?
NOTE: If the second option is the case, does this mean that when i re-log in, and i want to shut down the server, the only way to do it is with pkill?

Attaching to the the output of a running process

A process has been started remotely through a SSH session. The output stream (text) is displayed OK thru SSH. I would like to display the results locally without interrupting the running process.
Is there a way to attach to a running process and 'piggyback' a stream?
A Linux-only solution is acceptable.
Thanks!
Use reptyr:
reptyr is a utility for taking an existing running program and
attaching it to a new terminal. Started a long-running process over
ssh, but have to leave and don't want to interrupt it? Just start a
screen, use reptyr to grab it, and then kill the ssh session and head
on home.
Or retty:
retty is a tiny tool that lets you attach processes running on other
terminals.

Run node app forever with standard i/o?

I'm really new to node.js. My friend helped me set up a node app to run a java process I need running on a server at all times. It works perfectly, except the only way I can see the standard i/o is if I use node app.js. I've looked into both forever and pm2, however neither of these use standard i/o, which I really need for this server to run commands. Could somebody help me out please? Thanks!
Assuming you have a *nix-based server:
You could use GNU Screen.
Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.
In plain words, you would have access to always-running processes on server and their input-output from your local command line.
After logging in to your server, all you need to do is this:
Start new screen screen -S <name>
Run you java process
Detach from screen screen -d <name>
That's it! Your java process keeps running and you could interact with it by reattaching to the screen session like this: screen -r <name>
Useful Link: GNU Screen Quick Reference
Even cooler would be creating your own service using an Upstart script, which you could then call directly from your local machine with:
Create your own service using Upstart script.

How to run Derby as a background process in Linux

I am using derby on a remote Ubuntu 12.04 server. The standard derby commands are all working correctly and I am able to open my databases and access them via ij. I need to be able to start and stop the server from the terminal while logging in and out between commands. The problem is that I can not find a way to run the server as a background process. The closest I have come is: nohup java -jar $DERBY_HOME/lib/derbyrun.jar server start & > ~/dblog.txt which works except that it requires I hit [enter] before returning to the command line. I am aware of the daemon package but I am uncertain of whether it will allow me to then stop the server. What would be helpful is a explanation of how tomcat manages it since that is my app server.
Derby is just a Java application. Any technique you wish to use to run Java applications in the background (/etc/init.d, job control in your shell, etc.) will work fine for Derby.
You can use commands like "kill" or "killall" to kill your background process. Use "jobs" command to see list of running process you've sent to background. Also you can put them back in foreground by doing - "fg %n" (where n is the job number) and kill it using CTRL-C.

Resources