3

I want to run a script on a remote host with sudo privilege, someone suggests that I should use nohup and ssh -t, like the following command:

The script ls; sleep 10; echo finish is crafted here to resemble what I want to do.

ssh -t esolve@remote_host \
'sudo nohup bash -c "ls;sleep 100;echo finish" < /dev/null 2>&1 >> ssh.log' 

I want to run this script in background on local host because in my script, after this command there are some other commands, like

my script.sh:

ssh -t esolve@remote_host \
'sudo nohup bash -c "ls;sleep 100;echo finish" < /dev/null 2>&1 >> ssh.log' 
... some other commands

the following two commands don't work

ssh -t esolve@remote_host \
'sudo nohup bash -c "ls;sleep 100;echo finish" < /dev/null 2>&1 >> ssh.log' &

ssh -t esolve@remote_host \
'sudo nohup bash -c "ls;sleep 100;echo finish" < /dev/null 2>&1 >> ssh.log &' 

Why?

And how can I make this command run in background of local host? Besides, I don't need to input password for sudo on remote host.

  • Please add some more backround information. Do you want to run it in background of the localhost or remotehost? How are you authenticating with the remotehost, via public key or password? – soc1c May 9 '13 at 10:37
  • The main problem seems to be how to run this command as superuser in backround without setting a password. Have a look at this: stackoverflow.com/a/4327123/1260906 – soc1c May 9 '13 at 10:42
  • You can also use screen to background stuff: screen -d -m -S mybackgroundjob /usr/local/bin/somescript.sh – jkj May 26 '13 at 1:27
2

Add the -b flag to sudo, which prompts for password if needed and then goes to the background. (The redirect also needs to be done within bash) (Curly brackets are needed to get the redirection to apply to everything)

ssh -t esolve@remote_host 'sudo -b nohup bash -c "{ ls;sleep 100;echo finish; } < /dev/null 2>&1 >> ssh.log"'

You might also want to try screen instead...

ssh -t esolve@remote_host 'sudo -b screen -m -d bash -c "{ ls;sleep 100;echo finish; } < /dev/null 2>&1 >> ssh.log"'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.