Executing a background script using ssh keys


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Executing a background script using ssh keys
# 1  
Old 10-23-2013
Executing a background script using ssh keys

Greetings, i've been working with a user-friendly menu on ksh to allow users execute scripts located on a remote server, so they wont have to login and manually launch those scripts every single time.

This is a HP-UX box and currently on a /usr/bin/ksh shell.

I've setup ssh keys on both servers, so i'm able to execute commands remotely using ssh (ej, ssh vic@targetserver ls -l). My script basically gathers several variables from user input, then assambles the whole command and tries to execute it with ssh to the other server.

What i'm currently unable to do, is to finally get that command executed AND give the prompt back to the user so he can continue using the menu (even when using & and nohup).

Code:
 scriptname="/home/myuser/script" -> After user input, this variable is set with the remote script name.
                                                  
finalcommand="ssh user@server nohup $scriptname >/dev/null 2>&1 &"

$finalcommand

OR

Code:
 finalcommand="ssh user@server nohup $scriptname >/dev/null 2>&1 &"

$finalcommand $

I've managed to get two results: Either the command does not gets executed, or it does but will not give prompt back.

If i try to manually run the full command, it will work nicelly:

Code:
 ssh user@server nohup /home/myuser/script >/dev/null 2>&1 &

I'm quite sure this is a syntax/variable issue, but i've been breaking my head for two days and i'm out of ideas. My apologies in advance if more information is needed to understand the issue, i'm not used to post messages here.

Regards

Last edited by nbriozzo; 10-23-2013 at 02:19 PM..
# 2  
Old 10-23-2013
Don't put shell syntax in a shell variable and expect it to get run, the shell does not do that kind of doublethink.

Just do ssh user@server nohup $scriptname >/dev/null 2>&1 & without putting it in a variable.

Does the ssh connection really need to stay alive here? Could it be put into the background on the remote server instead of your own?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find active SSH servers w/ ssh keys on LAN

Hi, I am trying to complete my bash script in order to find which SSH servers on LAN are still active with the ssh keys, but i am frozen at this step: #!/bin/bash # LAN SSH KEYS DISCOVERY SCRIPT </etc/passwd \ grep /bin/bash | cut -d: -f6 | sudo xargs -i -- sh -c ' && cat... (11 Replies)
Discussion started by: syrius
11 Replies

2. Shell Programming and Scripting

Remote script via SSH not executing

I have worked on multiple scenarios to execute remote script via ssh. This problem I am not able to resolve. 2 linux hosts. Server1, Server2 on Server1 I have script called ~/scripts/start_standalone.sh XXXX cd $JBOSS_HOME NODENAME=xyz; IP_ADDR=`hostname`; MGMT_IPADDR=`hostname`;... (3 Replies)
Discussion started by: oraclermanpt
3 Replies

3. Shell Programming and Scripting

SFTP or scp with password in a batch script without using SSH keys and expect script

Dear All, I have a requirement where I have to SFTP or SCP a file in a batch script. Unfortunately, the destination server setup is such that it doesn't allow for shell command line login. So, I am not able to set up SSH keys. My source server is having issues with Expect. So, unable to use... (5 Replies)
Discussion started by: ss112233
5 Replies

4. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script test.sh which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

5. Shell Programming and Scripting

expect script pushing ssh keys w/ tar extract

ok, I'm new to the forum and I did a few searches and didn't find much on this so here goes. I have a ksh I use to call an expect script that opens a ssh session and then extracts a tar file. The tar file has my prefered .profile and my ssh keys. I want to run this script 1 time on new servers and... (2 Replies)
Discussion started by: gtsonoma
2 Replies

6. UNIX for Advanced & Expert Users

stopped(sigttou) error while executing a script in background

Hi All, I have an issue where in when i execute a script in the background using '&', it exits with stopped(SIGTTOU) signal. There are two servers, where the Compute server is HP-Unix and Data server is Linux. I tried using the "stty - tostop" command to disable the SIGTTOU for background... (1 Reply)
Discussion started by: vmenon
1 Replies

7. Shell Programming and Scripting

Using ssh to execute a remote script in the background

Help please!! I want to use ssh to execute a remote exe and while it's running I want to query for the process ID of the exe (2 different ssh commands) 1. sshpass -p "<passwd>" ssh -f -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no root@<ipaddress> nohup /tmp/mmds_asyn & 2.... (0 Replies)
Discussion started by: rvompoluTMW
0 Replies

8. UNIX for Dummies Questions & Answers

Error Executing a script(SSH problem)

Hi i m writing a script which will fetch data from different machines and display it on single terminal. I have created a file named SERVERNAMES containing ip address of machines i need to monitor. Then for every IP i m issuing ssh command to get date on that machine. However When i m trying... (3 Replies)
Discussion started by: pinga123
3 Replies

9. Shell Programming and Scripting

Executing a script on a remote system via SSH

Hello all, I have a relatively simple script I wrote to generate a count of errors broken down. What I would like to do is execute this script from another server so that I don't actually have to log in to the server to run the check. The script on what we'll call "Server A" is: ... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

10. Shell Programming and Scripting

Problem with executing script using ssh

I'm writing a script which is meant to ssh into a remote machine, sed a file there, and write the output to a new file on the remote machine. Obviously the easy way to do this is to write and execute the script on the remote machine, but I'm trying to do this all via ssh since I like to keep and... (4 Replies)
Discussion started by: maethlin
4 Replies
Login or Register to Ask a Question