java jsch example,使用JSch执行命令

I'm using JSch to automate remotely launching a python script from a background Java process.

Modifying the Shell.java example included in the JSch package, I've successfully installed JSch, connected to my Pi, and even commented out the user/domain/password/host key checking prompt boxes in favor of storing these values directly in my Java code.

After my java code logs into the remote Pi, I'd like it to send something like

sudo nohup python2 myFoo.py & disown

to the terminal.

In the Shell.java example I'm modifying, I see lines of code redirecting the input and output streams of the channel object to System.in and System.out but I'd like to simply manually inject that above line into the remote terminal and disconnect.

Why/my goal:

I have a small mesh network of Pi's running a script for most of the day.

I'd like to eliminate downtime, but the code sometimes stops working after looping for 3-4 days straight, (sometimes as long as a week straight before the code bugs out and stops).

The script running on each node updates a mySQL database with a "last check in" field.

I'm hoping to write a small background program in Java that will run indefinitely on my server, checking the "last check in" for each station every now and then, and if it notices a node go down, remotely ssh into it and sudo reboot now, wait about 60-100 seconds, then sudo nohup python2 myFoo.py & disown

解决方案

You have picked a wrong example. The "shell" channel is for implementing an interactive shell session, not for automating a command execution.

Use the "exec" channel, see the Exec.java example.

Channel channel=session.openChannel("exec");

((ChannelExec)channel).setCommand(command);

channel.connect();

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值