Process dies, if it is run via paramiko ssh session and with "&" in the end

Asked
Active3 hr before
Viewed126 times

7 Answers

process
90%

Connect and share knowledge within a single location that is structured and easy to search.,I just want to run tcpdump in background using paramiko.,Please be sure to answer the question. Provide details and share your research!,Find centralized, trusted content and collaborate around the technologies you use most.

Since exec_command is going to spawn a new thread for your command, you can just leave it open and proceed with other tasks. But make sure to empty buffers every now and then (for verbose remote commands) to prevent paramiko from stalling.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = login, password = password)
transport = ssh.get_transport()
channel_tcpdump = transport.open_session()
channel_tcpdump.get_pty()
channel_tcpdump.set_combine_stderr(True)

cmd = "tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap"
# command will never exit
channel_tcpdump.exec_command(cmd) # will
return instantly due to new thread being spawned.
# do something
else
   time.sleep(15) # wait 15 seconds
_, stdout, _ = ssh.exec_command("pgrep tcpdump") # or explicitly pkill tcpdump
print stdout.read() # other command, different shell
channel_tcpdump.close() # close channel and
let remote side terminate your proc.
time.sleep(10)
load more v
88%

The text was updated successfully, but these errors were encountered: ,By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.,There is a script on the remote server(10.201.143.183). The script a.sh reads as follows:,Since exec_command is going to spawn a new thread for your command, you can just leave it open and proceed with other tasks. But make sure to empty buffers every now and then (for verbose remote commands) to prevent paramiko from stalling.

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username = login, password = password)
transport = ssh.get_transport()
channel_tcpdump = transport.open_session()
channel_tcpdump.get_pty()
channel_tcpdump.set_combine_stderr(True)

cmd = "tcpdump -i eth1  port 443 -w /tmp/dump20150317183305940107.pcap"
# command will never exit
channel_tcpdump.exec_command(cmd) # will
return instantly due to new thread being spawned.
# do something
else
   time.sleep(15) # wait 15 seconds
_, stdout, _ = ssh.exec_command("pgrep tcpdump") # or explicitly pkill tcpdump
print stdout.read() # other command, different shell
channel_tcpdump.close() # close channel and
let remote side terminate your proc.
time.sleep(10)
load more v
72%

https://www.tecmint.com/keep-remote-ssh-sessions-running-after-disconnection/,The process will continue if the ssh session is interrupted but you cannot reconnect to see its output.,If the connection terminated then how to reconnect it to see the processes running in the tmux session in the background:,Run the command to start long-running processes in the tmux session.

Use nohup to make your process ignore the hangup signal:

$ nohup long - running - process &
   $ exit
load more v
65%

shell_exec — Execute command via shell and return the complete output as a string, This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required. , The command that will be executed. , A string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.

load more v
75%

If you want to have multiple sessions running side-by-side, you should name each session using Ctrl+b and $. You can get a list of the currently running sessions using tmux list-sessions, now attach to a running session with command tmux attach-session -t <session-name>.,You can now safely log off from the remote machine, your process will keep running inside tmux. When you come back again and want to check the status of your process you can use tmux attach to attach to your tmux session.,C-a C-d detachs the current screen session and exits screens. You can then use screen -r to resume that session. You can have several detached screen sessions at once, in this case you'll be displayed a list of available sessions.,I was stuck in a large mv so I wasn't in a position to stop the process, setup screen and then start it again. I managed to exit the SSH session with the process running by essentially doing the following steps:

The best way is often the simplest.

nohup long - running - command &

It was made specifically for this, it even logs stdout to nohup.log.

man nohup

Option 2: bg + disown

ctrl + z
bg
disown - h
load more v
40%

From an SSH session to the QRadar Console.,Use SSH to log in to the QRadar Console as the root user.,Log in to the QRadar Console as the root user.,Use SSH to log in to the QRadar Console.


# chmod 775 / opt / qradar / conf / localization
load more v
22%

A special signal is SIGKILL since it cannot be captured by the process and it will always terminate it immediately. However, it can have bad side effects such as leaving orphaned children processes.,(Challenge) Look into what the -N and -f flags do in ssh and figure out a command to achieve background port forwarding.,This idea is also useful if you want different programs to share some configurations. For instance, if you want both bash and zsh to share the same set of aliases you can write them under .aliases and have the following block in both:,<C-b> [ Start scrollback. You can then press <space> to start a selection and <enter> to copy that selection.

#!/usr/bin/env python

import signal, time

def handler(signum, time):
   print("\nI got a SIGINT, but I am not stopping")

signal.signal(signal.SIGINT, handler)
i = 0
while True:
   time.sleep(.1)
print("\r{}".format(i), end = "")
i += 1
load more v

Other "process-undefined" queries related to "Process dies, if it is run via paramiko ssh session and with "&" in the end"