32752

How can I exec an interactive shell from Jython?

Question:

I need some example Jython, or Java, code to exec an interactive OS shell (/bin/sh). Our development environment is Java on Linux. What I would like to do is provide a means for developers to write Jython scripts, and then drop into a shell, which allows them to inspect the system. The reason I need to exec this from Jython or Java--as opposed to just wrapping all this in a shell script--is that we use Java load some libraries which I need to keep loaded while the interactive shell is running. If the JVM were to terminate before the interactive shell runs, some of our custom hardware would be reset.

I have looked at the Jython subprocess module. However, most of what I have seen will only run and wait on a subrocess to terminate (ie non-interactive). None of the examples show how to the shell can be invoked interactively. The same is true of most of the Java java.lang.Runtime.exec() examples I've seen.

Answer1:

What is wrong with this simple jython code, is that the stdin, stdout do not work as we expect

import subprocess import os subprocess.call([os.environ.get('SHELL', '/bin/sh'), '-i'])

Assuming that we want to spawn the shell on the tty, the following seems to give better results:

import os import subprocess subprocess.call([os.environ.get('SHELL', '/bin/sh') + ' -i < /dev/tty > /dev/tty 2>&1'], shell=True)

This will run the shell within another subshell, that replaces the crippled stdin, stdout streams with the active tty.

Recommend

  • What is the difference between a CPU and a GPU oriented code?
  • Custom thread pool supporting async actions
  • Why a class has only one destructor? [duplicate]
  • Cannot terminate threads
  • selectInput can't populate duplicate values (using uiOutput and renderUI) in Shiny
  • Why == is different for Integer and String?
  • How does `std::terminate` know to treat `std::exception`s specially?
  • Modify a Google App Engine entity id?
  • Build Matrix of Comparisons in SQl Server
  • c++ using primitive types as a base class
  • Find VMID for running instance
  • include dlls in visual studio c++ 2008
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Creating NSCollectionView with datasource programatically
  • Excel VBA How to populate a multi-dimensional (3d) array with values from multiple excel ranges?
  • react split panel resize
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • What is the correct way to synchronize a shared, static object in Java?
  • Trying to get the char code of ENTER key
  • Using Sax parsing to edit and write XML in VB6
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Loading .coffee files via a view in Rails
  • Debug.DrawLine not showing in the GameView
  • Create DicomImage from scratch using Dcmtk
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Yii2: Config params vs. const/define
  • Ajax Loaded meta Tags
  • DomPDF {PAGE_NUM} not on first page
  • Javascript simulate pressing enter in input box
  • ActionScript 2 vs ActionScript 3 performance
  • Importing jscolor library in angular 2
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How can I estimate amount of memory left with calling System.gc()?
  • Apache 2.4 - remove | delete | uninstall
  • File upload with ng-file-upload throwing error
  • A cron job substitute?
  • Free memory of cv::Mat loaded using FileStorage API
  • Java static initializers and reflection
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • How to load view controller without button in storyboard?