permanently running script on remote machine in Unix

execute script on remote machine linux
shell script to execute command on remote server
linux run local script on remote server
execute command on remote server without ssh
shell script to execute command on remote server with password
how to run shell script from one server to another server
run script remotely
how to execute local script on remote machine in linux

I am having a problem working with SSH in Unix to run a script on a remote machine. The script is meant to run infinitely until an event happens, in which case it will then break out of its loop. Unfortunatly it seems that after a short peroid of afew secounds the script stops running. I believe this is related to SSH as the script when ran localy works as intended.

Is there a way I could trigger my remote machine to run that script as if it was being ran locally, so that I could close the SSH conneciton and it would still be running?

The script that calls the script located on the remote machine is this.:

 #!/usr/bin/expect set add [lindex $argv 0] set add2 [lindex $argv 1]
spawn ssh -l root2 ${add} /home/jason/localCopy/selfMonitor &
expect *assword: send "BIGMASSIVESECRET\r"
expect "]$ "

Contained in the selfMonitor script is basically a do while true statement that will run forever until broken.

Any help is appreciated, thanks.

How can I execute local script on remote machine and include , You were pretty close with your example. It works just fine when you use it with arguments such as these. Sample script: $ more ex.bash #!/bin/bash echo $1 $2. Running script on remote machine. , This was an interview question " There is a clean-up shell-script in one UNIX machine and it is connected to 100 other UNIX

Use nohup to have your script ignore SIGHUP, which is sent to backgrounded processes when the parent shell exits.

spawn ssh -l root2 ${add} nohup /home/jason/localCopy/selfMonitor &

Also, ssh intentionally makes it difficult to script the password entry and you are encouraged not to work around this behavior. The right way to allow password-less ssh access is to use pre-authorized keys.

  1. Run ssh-keygen on the client. You can press enter at all the prompts to use a blank password.
  2. Add the contents of ~/.ssh/id_rsa.pub to the end of a file called authorized_keys on the server: root2@server:~/.ssh/authorized_keys.

Creating a key and then adding it to authorized_keys allows the client to connect to the server without using a password, and it lets you do it without hard coding a password into a plaintext script.

Running Commands on a Remote Linux / UNIX Host, However, for security reason you should always use the ssh and NOT rsh. Please note that Syntax for running command on a remote host: Hi sanvel, When you are using ssh to execute commands in remote machine, the commands which you are using is in the remote machine, not your local machine. So, if you put a shell script's name in the place of commands to be executed, it won't work, because this is your local script.

To make your script run forever you need to do two things

  1. Make sure you don't write to the terminal. Close it or redirect it stdout and stderr, and close stdin. If you don't they'll keep the SSH open, and / or cause your script to terminate on a broken pipe.
  2. You need to trap SIGHUP which is sent to your process when the terminal disconnects, and normally results in the script terminating.

This is done differently in different languages, so for your language of choice, you need to look it up. In bash you use exec >logfile 2>&1 to redirect both stdout and stderr to logfile, and trap "" HUP to ignore the HUP signal. If you can, start a new process group as well, to reset the parent pid to init (officially making it a daemon process).

If you need to access the terminal output from the script (and can't redirect for whatever reason), may I recommend screen.

5 Ways to Keep Remote SSH Sessions and Processes Running , Using screen Command to Keep SSH Sessions Running Unix distributions and even it has been included in the base system of OpenBSD. So it is conceivable that you could remote into a machine using native Windows protocols (possibly also with or including winexe), run a powershell or net use command to map a file share, execute your shell script via win-bash, then unmap the share and terminate the connection without having to copy even a single file to the remote host.

Another approach is to demonize the script so that it detaches itself from the IO streams connected to SSH and starts a new session on startup.

Here there is a nice HOWTO.

And if your script is programmed in some popular language as Perl or Python, you will be able to find modules to do it.

How to run a shell script on a remote host using ftp, There is another way you can do it: test permanently on host if a certain file exists, and when it does run the command once. So you re-upload the run.txt file again� H ow do I execute a script on remote server called server2.example.com from my PC? How to run a shell/python/perl script on remote Unix-like systems? You can use the ssh client which is a part of most *BSD/Linux/OS X/Linux distros/Unix-like oses.

Although solutions here are valid, I've started using tmux extensively for such tasks.

You will be able to start a session, detach and reattach anytime you want. You can disconnect from your remote machine and it will still be running. Sessions can even have multiple windows, etc.

If you have 10 minutes, try this simple guide to tmux: A Quick and Easy Guide to tmux

After going through the guide, I've written a small cheatsheet that was in front of me for a day or two, but it's now almost a second nature to me. I've been using tmux for other tasks such as running a backend server in one pane, frontend in the other while having another pane just for usual terminal business. When you're done with working on a certain project, you can just detach and attach to another session to continue where you left off.

Remote execution of applications, Combined with an X server, remote graphical applications can be displayed locally. machine access to your display (to your X server!) using the xhost command, Most UNIX and Linux systems now run Secure SHell in order to leave out the want to continue connecting (yes/no)? yes Warning: Permanently added 'blob� All you have to do is build the logic that needs the methods, not in your control script on the local computer but in the script that runs on the remote machine. In one of my next posts, I will talk about running PowerShell commands remotely in disconnected sessions.

How to Remotely Run Commands on an EC2 Instance with AWS , Learn how to remotely run commands on an EC2 instance with AWS Systems Manager in 10 minutes. Using the run command, one of the automation features of Systems Manager, you In our example scenario, as a System Administrator, you need to update the AWS Systems Manager is an always free tier product. Run it as follows on a remote server called nas02nixcrafthomeserver: $ cat remote-box-commands.bash | ssh user@nas02nixcrafthomeserver OR cat remote-box-commands.bash | ssh -t vivek@nas02nixcrafthomeserver. How to ssh to multiple servers and run commands. One can use simple bash for loop as follows. In this example, run uptime command on three

Running Jupyter Notebooks on Remote Servers, To execute commands on the remote server, we run the combined command nohup ssh -f username:password@remote_server_ip "cd� 5 Ways to Keep SSH Sessions Running After Disconnection. When we log out of the session or the session times out after being idle for quite some time, the SIGHUP signal is send to the pseudo-terminal and all the jobs that have been run on that terminal, even the jobs that have their parent jobs being initiated on the pseudo-terminal are also sent the SIGHUP signal and are forced to terminate.

How to run a remote command in PuTTY after login & keep the shell , Background: I use emacs under windows and often edit files on remote Unix machines using tramp & plink. I want to make a hotkey that opens a PuTTY session for� The equally common situation, when there is some Bash script on a Linux machine and it needs to connect from it over SSH to another Linux machine and run this script there. The idea is to connect to a remote Linux server over SSH, let the script do the required operations and return back to local, without need not to upload this script to a

Comments
  • stop using expect, and just pass the password directly to ssh? or better yet, use keys. Anyways, this feels more like a superuser.com/serverfault.com type of question.
  • I think this answer is spot on (+1). A couple comments, (1) I usually redirect all output to a file using >&. If you don't then nohup writes it to some default file. This gets confusing if you have two or more processes running this way. (2) I use expect, a tcl package, to enter passwords in ssh when I can't use a pre-authorized key. This is rare, but worth knowing it works.