0

This solution requires me to run Python on the same machine as the process I am trying to terminate.

However, I'm running Python locally and have the process running over SSH in Terminal. How do I send the terminate command in this situation?

Community
  • 1
  • 1
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
  • If you just want send ctrl-C, ssh and kill with the process id – PasteBT Nov 23 '13 at 21:12
  • Ah. SSH within the Python script? A little more involved than I was hoping, since it requires authentication. I can send Terminal commands using Applescript. I wonder if there is an Applescript Terminal command for ctrl-C. – Chris Redford Nov 23 '13 at 21:14
  • SSH in python you can check module pexpect – PasteBT Nov 23 '13 at 21:39
  • I am trying to figure what possible reason you would want to do this. Can you explain why you are trying to control a remote process over ssh using python? Is there any reason you can't run the python script on the remote server? – William Denman Nov 23 '13 at 23:30
  • I'm editing programs remotely using Cyberduck for SFTP and Sublime Text 2 for editing. I have a hotkey on Alfred to send `make` then `make run` to Terminal (cmd+M) so that I don't have to cmd+tab to Terminal. I'm refactoring code and just checking that new code didn't break anything. The `make run` starts the program running a long string of output and as long as it is doing that, I know it is not broken. I would like to be able to terminate the program using another simple Alfred hotkey (e.g. cmd+shift+C) so I don't have to switch focus to Terminal to stop it and can keep programming. – Chris Redford Nov 24 '13 at 06:58

1 Answers1

0

SSH using pexpect after setting up ssh-keygen with the server so that it doesn't require a password from your machine:

import pexpect

ssh_command = 'ssh user@your.server.com'
child = pexpect.spawn(ssh_command)

default_prompt = 'user@server:~/# '
child.expect(default_prompt)

kill_command = 'killall process_name'
child.sendline(kill_command) 

If you don't use ssh-keygen, you will need to work your password login into the pexpect script before the default_prompt line.

Just attach this script to a hotkey (e.g. ctrl+alt+c) using Alfred.

Community
  • 1
  • 1
Chris Redford
  • 16,982
  • 21
  • 89
  • 109