0

I have a shell script that executes on remote server through ssh. It needs to execute Ctrl+Z and background the shell session to temporarily be back to my local terminal.

I tried for the following commands. (Current shell's pid in $$)

  1. suspend
  2. kill -s STOP $$
  3. killall -s STOP $$
  4. kill -s TSTP $$
  5. killall -s TSTP $$

However, none of them worked as expected as follows:

Command 1, 2, 3:
Successfully suspended the session, but I could do nothing afterwards as command prompt wasn't appeared again.

Command 4,5:
Didn't seem to suspend session, but command prompt was appeared after execution.

Is there any way to work it out?

2
  • What you ask for isn't possible. Perhaps if you explained what you are trying to do, an alternate method could be suggested, such as running ssh in a pipe.
    – user10489
    Jan 21 at 6:04
  • The local shell (i.e. the remote shell in your case) will handle ^Z, so the originating shell won't receive it (and thus cannot handle it). Please read about "-e escape_char" in the ssh manual page.
    – U. Windl
    Jan 22 at 23:27

1 Answer 1

1

Command 1-3 did not suspend your ssh session, they suspended your remote shell, from which there is probably no recovery.

The shell on the remote server can not suspend your ssh session. That has to be done from the local end using the keyboard by typing enter ~ ctrl+z. This key sequence is read by the local ssh directly and it commands it to suspend itself.

I'm not sure why you would want to suspend your ssh session to get a shell prompt when you could just open another window on the local system instead.

5
  • Thank you for your reply. To be honest, I'd like to suspend remote shell on the server connected through netcat reverse shell, not SSH. I thought it would be easy to understand that way. Sorry to confuse you... What I'd like to do is upgrade netcat shell to stabilize. To make it work, I need to suspend remote shell using Ctrl+Z. However, I'd like to do it with command, not through keyboard so that I can automate the process in shell script. (I think it'd be difficult to automate the whole process though)
    – tipotto
    Jan 21 at 7:21
  • According to this blog post(method 3), the author backgrounds remote shell with Ctrl+Z.
    – tipotto
    Jan 21 at 7:23
  • He/she backgrounds a netcat process, not an ssh process. It's unclear now if you're using ssh or netcat.
    – Panki
    Jan 21 at 8:14
  • @tipotto check about expect, script or python's pty.spawn() (that's your link: python does the magic, not netcat) , this could send you in the right direction. Also use socat instead of netcat, you'd have way better control. And finally: you get an answer about SSH then state your problem doesn't use SSH but netcat... why not having stated that initially?
    – A.B
    Jan 21 at 12:29
  • I agree with @A.B, you are describing your problem wrong, and asking for the wrong solution. If we had a better idea of what you really want to do, we could suggest something better than suspending the local end of the ssh connection.
    – user10489
    Jan 22 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.