Need a good way to ssh into a pc and run tmux - python-3.x

I want to SSH into a pc from python code and start tmux and attach to the session and run my programs.
I tryd useing paramiko, if i run tmux it crasched beacause it could not attach to session. Only way i could create a session in paramiko is to run
"tmux new-session -s <pannel name> -d"
but problem is still that i cant attach to the session useing
tmux a
The solution i have atm is autopygui python library and useing putty to start the process this solution works, but i need a better solution.

Found an solution on stackexchange since i cant find it on stack overflow i will post it here.
as part of the question with SSH i used paramiko, template here for the paramiko SSH code. In commands array just enter the tmux commands below.
for terminal commands i used
Create a detached session:
tmux new -d -s mySession
Execute a command in the detached session:
tmux send-keys -t mySession.0 "echo 'Hello World'" ENTER
3.1 Attach to the session
`tmux a -t mySession`
3.2 To exit SSH terminal
exit
Solution explanation:
Paramiko can't attach/jump to the new session with paramiko so solution is to use sendkeys in detached mode to execute commands.

Related

Using ssh to login to linux terminal from windows and run command in a logged in shell

First of all, this may seem like a duplicate question but I have searched stack overflow/various other forum sites and still haven't managed to find a solution.
A few example forum posts I have reviewed to prove I've done my research before asking a question:
https://superuser.com/questions/130443/remotely-run-script-on-unix-get-output-locally
https://linuxconfig.org/executing-commands-remotely-with-ssh-and-output-redirection
https://zaiste.net/posts/few-ways-to-execute-commands-remotely-ssh/
https://unix.stackexchange.com/questions/474533/get-output-of-this-command-from-another-server-via-ssh
Run ssh and immediately execute command
https://www.cyberciti.biz/faq/unix-linux-execute-command-using-ssh/
There's hundreds more but I won't include them all.
I essentially need a shell script to open a command prompt on windows, login to a remote linux system and run a command.
I am aware this can be done with the following:
start cmd /k ssh user#host ls
But the problem with the above is that the ssh connection is closed upon completion of the task.
I am also aware I can keep the ssh connection open by adding:
bash -l
in some cases.
For my use case, I need to run a launch file for ROS (robot operating system) and for this I need to see the output from the command.
And when attempting to run roslaunch launchFile.launch (in place of ls above):
start cmd /k ssh user#host "roslaunch launchFile.launch"
the command prompt returns
bash: roslaunch: command not found
I've obviously sanitised the specific name of my launch file but
roslaunch launchFile.launch
runs perfectly if I login to the linux PC first:
ssh user#host
then run the command.
I have achieved this exact use case on MacOS but I now need reimplement the same solution on windows:
osascript -e 'tell app "Terminal"
do script "ssh quantum#172.23.199.1 \n
roslaunch launchFile.launch"
end tell'
Thanks in advance for any help or advice.
Try this :
start cmd /k ssh user#host "/full/path/to/roslaunch launchFile.launch; exec /bin/bash"

Run a shell script in Terminal through a keyboard/desktop shortcut

I'm using Linux (Mint 20.3) to run a simple Minecraft server and I want to be able to start the server with a keyboard or desktop shortcut. I also want to be able to interact with the server in a terminal after it starts. I'm currently using the server software supplied by Mojang. I wrote a little program to get things started:
#!/bin/bash
cd /home/trevor/Minecraft_Server
LD_LIBRARY_PATH=. ./bedrock_server
exec $SHELL
I can get the server to run but I have no clue how to get it to open a terminal window so I can interact with the server. I'm relatively new to Linux so any input would be greatly appreciated.
you can use screen to detach and attach to run commands into the minecraft terminal.
to install screen:
apt-get install -y screen
To launch, update your script with something like:
screen -S mcs ./bedrock_server
to reattach, run the following in a terminal:
screen -r mcs
Use screen in your script to reattach to bedrock process.
Install screen:
apt-get install screen
Define your script as:
#!/bin/bash
export LD_LIBRARY_PATH=.
cd /home/trevor/Minecraft_Server
screen -d -m -S bedrock ./bedrock_server
After invoke your script, screen creates a socket that can be used to reattach to your script terminal. You can show screen sockets available with:
screen -ls
Parameter -S defined 'bedrock' as the socket name. So you can open another terminal as you like and reattach to the bedrock process with:
screen -r bedrock
If you detach the screen with CTRL+C the screen will be closed and so the minecraft bedrock server. To deattach without close the process you must use CTRL+A and CTRL+D.

Start tmux session which not closing after running script

How can I create an tmux session with script (e.g.)
tmux new-session python3 --version
If I need the session not to end?
You can achieve this by using the tmux send-keys command which sends commands to an existing session from the command-line (or a script):
tmux new-session -s test -d # Creates a session named 'test' and stays detached.
tmux send-keys -t test "python3 --version" Enter # Sends the command you wanted to the session.
# Here, the command was executed and the session is still alive.
tmux attach -t test # (Optional) If you want to attach to your session after the command was executed.
If the commands you want to send are in a script, you can send as a command the execution of this script:
If the script is python_ver.sh (make sure the script is executable):
#!/bin/sh
python3 --version
Then you'd go:
tmux new-session -s test -d # Creates a session named 'test' and stays detached.
tmux send-keys -t test "./python_ver.sh" Enter # Sends the command you wanted to the session.
# Here, the command was executed and the session is still alive.
tmux attach -t test # (Optional) If you want to attach to your session after the command was executed.

Run Linux command in background and keep runing after closing SSH [duplicate]

This question already has answers here:
How to make a program continue to run after log out from ssh? [duplicate]
(6 answers)
Closed 3 years ago.
I need to run a Perl script for several days processing something. On a linux Centos server, from the SSH terminal I run this command:
nohup perl script.cgi 2>&1 &
This runs the script in the background and writes the output to nohup.out.
The problem when I close the SSH terminal or even my internet connection disconnects the script terminates.
I need to keep this command running in the background on the server after I close
the SSH terminal.
You can use Terminal multiplexer tools like screen, byobu or tmux.
I personally use screen. so install it on remote server via sudo apt-get install screen.
ssh into server
open screen session by screen -S sessionname
Now run your command (background/foreground both works)
now detach to your session by command ctrl+a then press d.
Now shut your pc and enjoy
now come back ssh into server then use command screen -x sessionname to reconnect the detached session.
Hurray! script is still running.
you can either use screen or run the command using supervisor in linux systems.
you can install screen using sudo apt install screen
then use following command to run it.
screen -S test_command
nohup perl script.cgi 2>&1 &
Then press ctrl+a and ctrl+d to leave that session running for whatever amount of time required until your server reboots.
If you want to stop the command use screen -x test_command, then ctrl+c and use ctrl+a and ctrl+d to close screen or ctrl+a and ctrl+d to leave the screen session as it is.
The only way I was able to run the command and exit the shell and keep the command running is using the "at" tool to schedule the job like this using the full script path:
echo "perl /home/username/www/script.cgi" | at now + 1 minute

Having problems running python script even when putty is closed

So basically, I have a bot I'm running, and I would like for it to keep running even when I exit putty.
I've tried using nohup python bot.py & but it still ends the python bot when I close the putty program. I've also tried using a run.sh file with /usr/bin/nohup bot.py & inside it. but it won't work :( is there something else I'm missing?
I have also made sure the run.sh is a executable as some other forums have suggested, and I still get can't open run
I'm kinda new to the linux terminal.
if you guys could help me out that would be awsome :)
You need to detach the terminal so that when you exit, it is still running. You can use screen or tmux or some other multiplexer.
Here is how to do with screen:
screen -S mybot -m -d /usr/bin/python /path/to/bot.py
-S give the session a name (this is useful if you want to attach later. screen -D -R mybot)
-m always create a new session
-d detach (launch the program, but then detach the terminal returning you to the prompt)

Resources