How to know PID of the process ran by remote ssh - linux

If I run a process at the remote site by using ssh as follows:
nohup ssh remote sleep 100 &
Is there a way to know the PID of sleep at remote?
Trying echo $! just returns the PID of ssh at local.
And, greping won't work since there are multiple sleep processes at remote.

You could pass a string to ssh so try
nohup ssh remote 'sleep 100 &; echo $!'

Try the following
nohup ssh remote 'sleep 100 > out 2> err < /dev/null & echo $!'

Related

SSH direct command execution with nohup

I can directly exec the command with this.
ssh ubuntu#myroot.com ls -la
However I want to use command like nohup sh heavytask.sh &
Even after quitting connection this task continues works.
So,what I try this
ssh ubuntu#myroot.com nohup sh heavytask.sh &
However it needs to wait task finished.
Is there any solution for this purpose?
I/O redirection should fix your issue, e.g.:
ssh ubuntu#myroot.com "nohup sh heavytask.sh > /dev/null 2> /dev/null < /dev/null &"

How to execute a local bash script on remote server via ssh with nohup

I can run a local script on a remote server using the -s option, like so:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "bash -s" < myLocalScript.sh;
And I can run a remote script using nohup, like so:
# run a script on server with nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash myRemoteScript.sh > results.out 2>&1 &"
But can I run my local script with nohup on the remote server? I expect the script to take many hours to complete so I need something like nohup. I know I can copy the script to the server and execute it but then I have to make sure I delete it once the script is complete, would rather not have to do that if possible.
I've tried the following but it's not working:
# run a local script without nohup...
ssh $SSH_USER#$SSH_HOST "nohup bash -s > results.out 2>&1 &" < myLocalScript.sh;
You shouldn't have to do anything special - Once you kick off a script on another machine, it should finish running even if you terminate the connection:
For example
ssh $SSH_USER#$SSH_HOST "bash -s > results.out 2>&1" < myLocalScript.sh &
# Please wait a few seconds for the connection to be established
kill $! # Optional: Kill the last process
If you want to test it, try a simple script like this
# myLocalScript.sh
echo 'File created - sleeping'
sleep 30
echo 'Finally done!'
The results.out file should immediately be created on the other machine with "File created - sleeping" in it. You can actually kill the local ssh process with kill <your_pid>, and it will still keep running on the other machine, and after 30 seconds, print "Finally done!" into the file, and exit.

How to kill port forwarding once you have have finished using it

In order to sync my home and work file systems I need to go via an intermediary computer and use port forwarding. Let us call the home computer A, the intermediate one B and the work computer C. From the command line I do this
ssh -N -f -L 2025:C:22 me_B#B && unison foo ssh://me_C#localhost:2025/foo
I would like to put this one-liner into a bash script. How can I make it quit gracefully at the end and not leave any port forwarding still set up?
ssh -N -f -L 2025:C:22 me_B#B &
pid=$! # ssh PID
rc=$? # ssh return code
# set up to kill ssh when this script finishes
function finish {
kill $pid
}
trap finish EXIT
[ $rc -eq 0 ] && unison foo ssh://me_C#localhost:2025/foo

Execute remote bash with ssh together with a local script, that, when ends, will kill the remote execution

I logged with ssh on the server with public key (no password).
This are the command that I would like to automatize with a bash:
ssh user#ip
cd path
./bash0.sh parameter1 paramter2 & <-- this is a loop and is working on remote server in background
exit <-- exit form ssh
./bash1.sh <-- starting local bash
ssh user#ip pkill bash0.sh <-- kill the process at the end of the bash1.sh. From terminal is ok, but from bash?
The problem is to execute the cd path and immediately after the ./bash0.sh paramter1 parameter2 & and then exit from ssh without waiting the ./bash0.sh to finish.
I cannot do ssh user#ip ./path/bash0.sh paramter1 paramter2 because the bash file contains relative paths.
Simple approach:
./bash1.sh && ssh user#ip pkill bash0.sh
Found the solution:
ssh user#ip "cd path && ./bash0.sh parameter1 paramter2" &
/bash1.sh <-- starting local bash
ssh user#ip pkill bash0.sh
The "" are very important.
You can send mutliple commands over ssh by seperating them with a semicolon:
ssh user#ip "cd path;./bash0.sh parameter1 parameter2 & exit"
(the exit is not needed but only there to show how to append more commands after the &)
And ssh user#ip pkill bash0.sh should work from a bash script. Your terminal is probably also just bash.
EDIT: See http://www.skorks.com/2010/05/executing-multiple-commands-a-bash-productivity-tip/ for a detailed explanation of the ;, & and && operator.

Running Process as Unprivileged User

On Linux system, how does one run a process as a different, unprivileged user (like how lighttpd is run by www-data in the default setup)?
I've been using su $user; $command & over ssh, but those processes get killed when I logout.
If it makes any difference, I'm using a default Ubuntu setup on EC2.
su $other_user -c 'nohup sleep 600 &'
nohup $command </dev/null >/dev/null 2>/dev/null &
or
command </dev/null >/dev/null 2>/dev/null &; disown

Resources