Sample code for 30+ languages & platforms
Android™

Send a Signal to a Remote SSH Process

See more SSH Examples

Demonstrates the Chilkat Ssh.SendReqSignal method, which requests delivery of a signal to the remote process running on a channel. The first argument is the channel number and the second is the signal name. Send it after the process has started.

Background: This is the SSH equivalent of pressing Ctrl+C or running kill — it lets a client interrupt or terminate a long-running remote command without tearing down the connection. Signal names are given without the SIG prefix (INT, TERM, KILL, and so on). Note that some servers, including common OpenSSH builds, do not implement signal delivery.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Demonstrates the Ssh.SendReqSignal method, which requests delivery of a signal to the remote
    //  process running on a channel.  The 1st argument is the channel number and the 2nd is the
    //  signal name.  Send it after the process has started.

    CkSsh ssh = new CkSsh();

    int sshPort = 22;
    success = ssh.Connect("ssh.example.com",sshPort);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    String password = "mySshPassword";

    success = ssh.AuthenticatePw("mySshLogin",password);
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Open a session channel.  A negative return value indicates failure.
    int channelNum = ssh.OpenSessionChannel();
    if (channelNum < 0) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Start a long-running command.
    success = ssh.SendReqExec(channelNum,"sleep 60");
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    //  Send an interrupt signal to the remote process.  The name omits the "SIG" prefix.
    success = ssh.SendReqSignal(channelNum,"INT");
    if (success == false) {
        Log.i(TAG, ssh.lastErrorText());
        return;
        }

    Log.i(TAG, "Signal sent.");

    ssh.Disconnect();

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}