Sudo ssh with command running in background


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sudo ssh with command running in background
# 1  
Old 09-24-2015
Sudo ssh with command running in background

I am trying to run a command. This is one of my attempts:

Code:
for i in fileservera; do ssh -t $i 'sudo ls /';done

This works, and I see the directories. However, what I want to do now is start a process on the remote server such as
Code:
/usr/bin/connectproc -standalone

Code:
sudo /usr/bin/connectproc -standalone

works fine on the remote server once I log in and run it. It even stays running when I logout.

But I can't get it to run when I use the sudo command with ssh. I've tried to use the following:

Code:
for i in fileservera; do ssh -t $i 'sudo/usr/bin/connectproc -standalone';done

and

Code:
for i in fileservera; do ssh -t $i 'sudo/usr/bin/connectproc -standalone &';done

it does not output anything, it appears to login to the remote server but I never see the command running. What would be the best way to get this command to run with the very simple script above?

This is my latest attempt:

Code:
for i in fileservera; do ssh -t $i 'nohup sudo /usr/bin/connectproc -standalone';done

This works, but as soon as I control -C (it gets stuck) the process dies on fileservera

Anyone have any idea?

I now tried to enclose with
Code:
for i in fileservera; do ssh -t $i '$(nohup sudo /usr/bin/connectproc -standalone &)';done

this works but it disconnects me at the end and the process dies in 30 seconds and does not keep alive

Also I have about 100 hosts with different Solaris/Linux so it is hard for me to use screen

Last edited by newbie2010; 09-24-2015 at 04:55 PM..
# 2  
Old 10-07-2015
In two of your examples you have a typo 'sudo/usr/bin/connectproc.

You can leave the ssh session connected by calling ssh with a -f flag to background as soon as it's finished authentication.

If you want to cleanly and reliably start the process running then disconnect the ssh session from it, protect your commandline from inline interpretation by pushing a small script up first, then calling it:
Code:
scp littlescript.sh targethost && ssh -t targethost 'sudo littlescript.sh'

littlescript.sh:
Code:
#!/bin/sh
echo "Starting the thing"
nohup /usr/bin/connectproc -standalone &
echo "Started the thing on pid $!"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Htop - How to detect exact command running at background

Hello, I am running a python file from terminal and I wish to see which code is running at background. When I use htop, I see just a few commands, unable to see entire command. htop > report nano report Output: ^ Following parts of ffmpeg line is not shown by htop. Just showing... (7 Replies)
Discussion started by: baris35
7 Replies

2. Shell Programming and Scripting

Running test command in ssh

I tried to run the following commands in my shell script. This command works fine for me: ssh -n "${username}"@"${hostname}" "grep WARNING ${serverhome}/${serverlog} | wc -l" The result is: 1548 However when i try to run a similar one: ssh -n "${username}"@"${hostname}" "test `grep -q... (2 Replies)
Discussion started by: hys1117
2 Replies

3. Shell Programming and Scripting

Running Local Script from SSH with SUDO

Hello, I know for SSH'ing and running a local script is... ssh -t user@servername < /path/to/localscript.sh and with SSH'ing and SUDO'ing is... ssh -t user@servername "sudo -u username ls -l /home/username" My inquiry is how can I combine both, by SSH'ing and SUDO'ing but running... (4 Replies)
Discussion started by: WPGPoseidon
4 Replies

4. Shell Programming and Scripting

Running command using SSH

Hi All, I am trying to run my script using ssh. Below is the code snippet #!/bin/bash ssh username@useipapd03 'bash -s' #To ensure If JMSdirectory exists or not if then echo "JMS mounts................exist'>>OutputResult.txt else echo "JMS mounts................not... (10 Replies)
Discussion started by: sharsour
10 Replies

5. Solaris

running command in background

hi all i am running following command ufsdump 0ubf 512 /dev/rmt/0cbn /database/backup2/rman_backup/level1 >> /database/backup2/backup_tape/level1_rman_03aug12 2>&1; i want to run it in background how can i do it in this i am generating logs for backup. the problem occurs when i am... (3 Replies)
Discussion started by: nikhil kasar
3 Replies

6. Programming

Background (nohup * &) SSH command block possible?

Hello, I am trying to find a way to send several sequential commands via SSH to a remote box in a single command. Thoughts so far: 1) Can I put them into a function and call the function within the ssh command? e.g. ssh <targetserver> $(functionx) No - then it calls the function in... (4 Replies)
Discussion started by: doonan_79
4 Replies

7. Shell Programming and Scripting

command to see process running at background

Hi , I want to see all the background process that are running in unix box machine...please guide me is there any specific command for that..since I am executing some scripts at background..!!:confused: (1 Reply)
Discussion started by: nks342
1 Replies

8. Shell Programming and Scripting

ssh foo.com sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ foo.com:path/on/remote/machine/ ssh foo.com sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

9. UNIX for Dummies Questions & Answers

kill a command I am running / then put it to background

I have a command running in the foreground (and so my term window is locked up) and I want to kill it, then resume it in the background and go home. It is creating a zip file, and the file will be written to the current directory - no std in / std out issues. How do I do this? Kill it with a... (3 Replies)
Discussion started by: hindesite
3 Replies

10. Shell Programming and Scripting

is running this command via ssh possible? (formatting issues)

Here is the command I want to run: for pkg in `pkginfo | grep -i VRTS | awk '{print $2}'`; do showrev -p | grep $pkg; done | awk '{print $2 "\t" $7}' | uniq It returns the package info in a form such as: 113210-03 VRTSfspro 112392-06 VRTSvmman 113596-03 VRTSvmpro... (1 Reply)
Discussion started by: LordJezo
1 Replies
Login or Register to Ask a Question