0

I'm using an automated SSH script to copy/run/log hardware tests to a few computers via SSH, and everything works fine except one thing. The test file is supposed to run indefintely every 30 minutes and collect data, then write it to a file until killed. For lack of a better example:

NOTE: Neither of these files are the actual code. I don't have it in front of me to copy it.

file.py:

#!/usr/bin/env python
import os

idleUsage = []
sleepTime = 1800

while(True):
    holder = os.popen('mpstat | awk \'{printf("%s\n", $9)}\'')
    idleUsage.append(100.0 - float(holder[1]))

    f = open("output.log", 'w')
    f.write(%idleUsage)
    f.close()

    sleep(sleepTime)

automatic-ssh.sh:

#!/bin/bash

autossh uname1 password1 ip1 command <----gets stuck after ssh runs
autossh uname2 password2 ip2 command
autossh uname3 password2 ip3 command

Without fail it gets stuck on running the command. I've tried 'command &' as well as putting an ampersand at the end of the entire line of code. Anyone out there have some advice?

4
  • I think you need to do os.system("sar | grep kb") ... not sure but I dont think thats a valid line of python... Commented Jul 13, 2012 at 16:25
  • 7
    That doesn't look like Python syntax. Commented Jul 13, 2012 at 16:25
  • I know it's incorrect syntax, but it's just an example and not even in the code. My concern is getting the while loop to run without stopping my auto-ssh script. Commented Jul 13, 2012 at 17:18
  • There. I fixed it up some. I'm pretty sure that the problems with SSH and not the python code itself. Commented Jul 13, 2012 at 17:34

2 Answers 2

1

Not sure of your current context but I would recommend using subprocess:

from subprocess import Popen

p1 = Popen(["sar"], stdout=PIPE)
p2 = Popen(["grep", "kb"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try this in a bit. As of right now I found a temporary solution of the script killing all SSHD and then starting SSHD back up before it goes into the while true loop.
0

So, your shell script connects to a remote machine via ssh and runs an endless python command, and you want that ssh connection to go into the background?

#!/bin/sh
ssh thingie 1 > out.1 &
ssh thingie 2 > out.2 &
ssh thingie 3 > out.3 &
wait

That'll kick off three ssh commands in the background logging to individual files, and then the script will wait until they all exit (wait, if not given a pid as an argument, waits for all children to exit). If you kill the script, the child ssh processes should terminate as well. I'm not sure if that's what you're asking or not, but maybe it helps something? :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.