Running shell script in detached screen session. Must kill. How? - linux

I am an Ubuntu Linux user. I am running jobs remotely and started a screen session. During this session I sourced a file containing a long list of command lines arguments to be executed. I was forced off of the connection and now the jobs are still running in this screen and I am unable to kill them.
Does anyone know how to kill all running and future commands this script will execute. Thank you in advance.

If you just want to kill everything there is no need to even reattach to screen.
Just list the offending process(es):
pstree -pla
then kill whatever needs killing. Note that if you kill a process higher up the process tree, its children will (usually) go away as well.

Reattach the screen with
screen -D -r
then you can resume your session.

Use ps to identify the pid of the shell process (bash, tcsh, etc), then kill that...

There are a couple of 'screen' ways to kill a specific screen session ...
1) send a 'quit' command:
screen -X -S "sessionname" quit
2) send a Ctrl-C to a screen session running a script:
screen -X -S "sessionname" stuff "^C"
In both cases, you would need to use 'screen -ls' to find the session name of the screen session you want to kill ... if there is only one screen session running, you won't need to specify the -S "sessionname" parameter.

Related

What is the cleanest way to exit/kill/logout linux session?

I am using tiling window managers and from time to time I want to go back to a normal desktop environment or switch to another tiling window manager.
Usually I use a rofi script with loginctl terminate-user $USER.
Before this I used pkill -9 -u $USER.
I also found other options such as loginctl kill-user $USER and pkill dwm or pkill i3.
With so many options, I started to wonder which is the best and cleanest way to exit a session?
To kill a window manager, you only need to kill the X server, leaving Linux and your login shell running.
For a general command, you can use:
pkill -x X
In the specific case of i3, from i3's documentation:
To exit i3 properly, you can use the exit command, however you don’t need to (simply killing your X session is fine as well).
Example:
bindsym $mod+Shift+e exit

Killing ssh session kills running process

I'm connecting to my ubuntu server using ssh. I start an encoding program using a command. However, it seems that when my ssh session closes (because I started it on a laptop which went to sleep). Is there a way to avoid this (of course preventing my laptop from sleeping is not a permanent solution).
Run your command with nohup or use screen
nohup is better when your program generate some loging output because it's forward to file and then you can check it, but with screen you can detach ssh session and when you log again you can restore your work-space. For encoding I'll use nohup. It is easier and you probably run it in background, so you really don't need detaching
Screen is the best for you.
screen -S some_name
than run it. Detach it with: ctrl+a d
Next time, attach it with:
screen -rd some_name
You can have more runnning screens. To show the list of them:
screen -ls
Install "screen" on your ubuntu server, that way you can start any program in your session, disconnect the output of the program from your current session and exit.
Later when you connect again to your server, you can restore the program which will continue running and see its progress.

how to kill running script within a dead screen session

I have a script started in a screen session, then detached, tried to reattach but now this session is dead.
The script is still running.
How I can kill the script, being unable to reattach the screen session?
Unfortunatly I have killed the screen process shown with ps.
Is there any possibility to stop the script?
I assume you are in a linux enviroment, as you say you used ps. If you have the uuid of the process, you can kill it by using kill < uuid> -9. If you know the process by name, try pkill < name>. Both cases should work on a terminal. If you are unable to open a terminal, try ctrl+alt+f1. You should see a login prompt, followed by a terminal. To get back to the xsession do ctrl+alt+f7
If everything else fails, try killing all processes: kill -9 -1. This should get you back to your display manager.
If you killed your screen process, try xinit from a terminal and then try to go back.
If you can't see the process, try ps aux

How to stop a screen process in linux?

I am running a script on a remote server. I ran the script in screen, however I need to stop it before it completes since I need to update the script. I can easily detach from screen, however, is there a way to kill a screen process?
CTRL+a and then 'k' will kill a screen session.
There are a couple of 'screen' ways to kill a specific screen session from the command line (non-interactively).
1) send a 'quit' command:
screen -X -S "sessionname" quit
2) send a Ctrl-C to a screen session running a script:
screen -X -S "sessionname" stuff "^C"
In both cases, you would need to use 'screen -ls' to find the session name of the screen session you want to kill ... if there is only one screen session running, you won't need to specify the -S "sessionname" parameter.
I used this to quit hundreds of erroneous screen sessions created by a buggy command:
for s in $(screen -ls|grep -o -P "1\d+.tty"); do screen -X -S $s quit; done;
where: the grep -o -P "1\d+.tty" is the command to get session names with Perl-like name regex "1\d+.tty" which captures all sessions start with number 1, has some other numbers (\d) and end with .tty
Warning:
You should test with this command first to see you get the exact list of sessions you want before apply the above command. This is to avoid quitting unwanted sessions:
for s in $(screen -ls|grep -o -P "1\d+.tty"); do echo $s; done;
I always to this echo test whenever the list in for loop is not clear, for example, the one generated by sub-command in $() expansion.
previous answers didn't work for me on a winputty terminal and amazon ssh server connection.. but this one does work:
screen -S yourscreentitlehere -X stuff $'\003'
references:
Sending ctrl-c to specific screen session
$'\003' is ctrl+c http://donsnotes.com/tech/charsets/ascii.html
stuff is https://www.gnu.org/software/screen/manual/screen.html#Paste
I am using putty, and it seems I am already in the screen and couldn't open and close. Every time I do "exit", I just close the putty window. Here is the termimal print
>>screen -r
21063.unlimited (11/08/20 15:45:19) (Attached)
24054.cure6 (11/08/20 09:46:13) (Attached)
There is no screen to be resumed.
and
screen -S 21063.unlimited -X stuff $'\003'
does not do anything.
I found that as simple as the following line works perfect
screen -x 21063.unlimited
it sends me back into the screen and from there "exit" works.
Note that it is lower-case -x

How to start a process that won't end when my ssh session ends?

Somehow this isn't yielded by a google search.
I'm scripting a server in node.js. I start the server by executing its script with the node program:
node myserver.js
But the server staying up is dependent on my ssh session. How can I make this (and all such processes) persistent? Init.d?
Use the nohup command:
From http://en.wikipedia.org/wiki/Nohup
nohup is a POSIX command to ignore the HUP (hangup) signal, enabling the command to keep running after the user who issues the command has logged out. The HUP (hangup) signal is by convention the way a terminal warns depending processes of logout.
Try this:
nohup node myserver.js &
Have you tried GNU screen? Using it, a process can continue to run when you end your ssh session. nohup is a standard *nix command that will allow you to do the same, albeit in a more limited way.
Use screen. Type screen from the terminal and then launch your process. If you disconnect you can reconnect to the ssh session, by typing 'screen -ls' (to see active screens) and 'screen -r' to reconnect.
The program needs to run in a daemonized mode. Here's a good post for doing this in Ubuntu.
nohup is good to run the job under. If the job is already running, you can try disown -h (at least in bash)

Resources