Running process in the background

前端 未结 5 916
日久生厌
日久生厌 2020-12-17 03:24

I am finding hard to run a process at background using paramiko. I used :

stdin, stdout, stderr = ssh.exec_command(\'executefile.py &\') 
相关标签:
5条回答
  • 2020-12-17 03:54

    You could try using screen

    screen -d -m ping 8.8.8.8
    

    This is would start a screen and ping 8.8.8.8. You can view this screen by using

    screen -ls
    

    and attach using

    screen -D <<screen_name>>
    

    Note that the screen will terminate after the command has finished executing.

    0 讨论(0)
  • 2020-12-17 04:00

    exec_command isn't executing the command in an interactive shell, so "running a process in the background" doesn't really make sense.

    If you really want to do this, you could use the command nohup to start your process, and keep it alive when the session exits. Remember that you can't get stdin, stdout, or stderr when you do this, since you are detaching the process from the shell, so redirect them accordingly.

    0 讨论(0)
  • 2020-12-17 04:05

    I've tried all the methods described here and here without success, and finally realized that you need to use channels instead of using the SSHClient directly for calling exec_command (this does not work in background):

        client = paramiko.SSHClient()
        client.connect(
            ip_address, username='root', pkey=paramiko_key, timeout=5)
        client.exec_command('python script.py > /dev/null 2>&1 &')
    

    You should create and use a channel, this works in background:

        client = paramiko.SSHClient()
        client.connect(
            ip_address, username='root', pkey=paramiko_key, timeout=5)
        transport = client.get_transport()
        channel = transport.open_session()
        channel.exec_command('python script.py > /dev/null 2>&1 &')
    

    So nohup, dtach, screen, etc, are actually not necessary.

    0 讨论(0)
  • 2020-12-17 04:13

    I tried transport class and it was really great. Here's the code I used:

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname = "host_ip", username = "un"], password = "up")
    channel = ssh.get_transport().open_session()
    pty = channel.get_pty()
    shell = ssh.invoke_shell()
    shell.send("cd /my/directory/; nohup ./exec_name > /dev/null 2>&1 &\n")
    

    But I still don't know how to kill it using python scripts; I have an open question about it here.

    EDIT 1:

    I have solved my problem about killing the process somehow; you can check it.

    0 讨论(0)
  • 2020-12-17 04:17

    You can try:

    stdin, stdout, stderr = ssh.exec_command('nohup python executefile.py >/dev/null 2>&1 &') 
    
    0 讨论(0)
提交回复
热议问题