Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:45:43+00:00 2026-05-23T17:45:43+00:00

I’m trying to write a program that will run a program on a remote

  • 0

I’m trying to write a program that will run a program on a remote machine using ssh and pass it SIGINT signals without killing my ssh connection. If I was running on a terminal, then this would be easy with something like

ssh -t -t host "command to run"

I tried this with the following script:

#!/bin/bash

ssh -t -t host "program $@"

and when I run that from the terminal it works fine, but when proofgeneral runs this script and sends it SIGINT it just ends up killing the program (I guess it can’t allocate a terminal?).

I’ve got the following mess of code:

CTRL_C = "<CTRL-C>"

def endpoint(proc, handler):
    signal.signal(2, signal.SIG_IGN)

    inp = ""
    out = ""
    err = ""

    inp_from_fd = sys.stdin.fileno()
    inp_to_fd   = proc.stdin.fileno()
    out_from_fd = proc.stdout.fileno()
    out_to_fd   = sys.stdout.fileno()
    err_from_fd = proc.stderr.fileno()
    err_to_fd   = sys.stderr.fileno()

    while True:
       try:
           ins,outs,_ = select.select([inp_from_fd, out_from_fd, err_from_fd],
                                      [inp_to_fd, out_to_fd, err_to_fd],
                                      [])
           for fd in ins:
               if fd == inp_from_fd:
                   k   = os.read(fd, 1024)
                   if k == "":
                       os.close(proc.stdin.fileno())
                       return
                   inp = inp + k
               elif fd == out_from_fd:
                   k   = os.read(fd, 1024)
                   out = out + k
               elif fd == err_from_fd:
                   k   = os.read(fd, 1024)
                   err = err + k
               else:
                   assert False
           for fd in outs:
               if fd == inp_to_fd:
                   while CTRL_C in inp:
                       proc.send_signal(2)
                       p = inp.find(CTRL_C)
                       inp = inp[0:p] + inp[p+len(CTRL_C):]
                   k = os.write(fd, inp)
                   inp = inp[k:]
               elif fd == out_to_fd:
                   k = os.write(fd, out)
                   out = out[k:]
               elif fd == err_to_fd:
                   k = os.write(fd, err)
                   err = err[k:]
               else:
                   assert False
       except select.error:
           pass
       except KeyboardInterrupt:
           handler()
       except IOError, e:
           pass

def usage(args):
    print "must specify --client or --server" 

if __name__ == '__main__':
    if len(sys.argv) == 1:
        usage(sys.argv)
    elif sys.argv[1] == '--server':
        proc = subprocess.Popen(sys.argv[2:],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        def INT():
            proc.stdin.write(CTRL_C)
            proc.stdin.flush()
        endpoint(proc, INT)
    elif '--client' in sys.argv:
        proc = subprocess.Popen(sys.argv[2:],
                                stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        import time
        time.sleep(1)
        def INT():
            pass
        endpoint(proc, INT)
    else:
        usage(sys.argv)

which I’m invoking using something like:

remote.py --client ssh -t -t host "remote.py --server <program-to-run> <args>"

Is there something that I’m doing wrong here to handle the signal? I’ve tried putting a print in the signal 2 handler and it does print it, but it is also killing ssh (I’m getting “Killed by signal 2.” printed on the console). Is python forwarding the signal to it’s children? Is there a way to get around this? Is there an easier way to do this?

Thanks for any pointers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:45:43+00:00Added an answer on May 23, 2026 at 5:45 pm

    os.setpgrp (look at setpgrp manpage) otherwise the signal is propagated to children

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.