0

I'm trying to use a screen command to stack multiple commands one after another. First I run a python script and then I connect to an ssh host like this.

$HOSTNAME is slave-node-1 $arg is arg2

screen -dmS 1 sh -c "python3 pyscript1.py arg && \
ssh -i ~/.ssh/my-ssh-key user@remote-node \
"sed -i '/$HOSTNAME-pyscript1-$arg/s/$/1/' ~/test.txt"\
;exec bash"

The reason I am using screen is because this isn't the only command I need to run and I need to be able to monitor the output of pyscript1.py as it runs.

The command is supposed to create new screen named "1", run the script pyscript1.py with the arg "arg", and only when that command finishes, I want to connect to the remote-host using that command to make a change in a file.

exec bash is meant to keep the screen open even after the commands finish.

The sed command works when I type it separately.

I am also able to create a screen named "1" using screen -dmS 1.

I am also able to connect to the server using SSH.

However, when I run the above command, nothing happens. A new screen isn't even created.

when i run

ssh -i ~/.ssh/my-ssh-key user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$ 1/' test.txt"

I get the desired effect

but when i try

screen -dmS 1 sh -c "ssh -i ~/.ssh/my-ssh-key user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$ 1/' test.txt";exec bash"

nothing happens

What am I doing wrong?

1
  • It is unclear to me on which system $HOSTNAME is to be expanded. – Hauke Laging May 22 '20 at 1:43
1

There are several quoting problems.

Original:

screen -dmS 1 sh -c "python3 pyscript1.py arg && ssh -i ~/.ssh/my-ssh-key user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$/1/ ~/test.txt;exec bash"

readable version with line breaks

screen -dmS 1 sh -c "python3 pyscript1.py arg && ssh -i ~/.ssh/my-ssh-key "\
"user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$/1/ ~/test.txt;exec bash"

I guess that

"sed -i '/$HOSTNAME-pyscript1-$arg/s/$/1/ ~/test.txt"

is supposed to be the argument for ssh.

Corrected code:

screen -dmS 1 sh -c "python3 pyscript1.py arg && ssh -i ~/.ssh/my-ssh-key "\
"user@remote-node \"sed -i '/$HOSTNAME-pyscript1-$arg/s/$/1/' ~/test.txt\";"\
"exec bash"
6
  • exec bash is separate, it's meant to keep the screen open. sorry I forgot to put the quotation marks, please have another look at the question. – anarchy May 22 '20 at 1:46
  • when i run ssh -i ~/.ssh/my-ssh-key user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$ 1/' test.txt" the correct thing happens – anarchy May 22 '20 at 1:57
  • but when i try screen -dmS 1 sh -c "ssh -i ~/.ssh/my-ssh-key user@remote-node "sed -i '/$HOSTNAME-pyscript1-$arg/s/$ 1/' test.txt";exec bash" nothing happens – anarchy May 22 '20 at 1:59
  • @anarchy Of course. You are not supposed to use your code any more but mine instead. – Hauke Laging May 22 '20 at 2:02
  • i just realised your backslashes were used for escape, my bad, I thought they were meant to show new lines, anyway now i get -bash "exec bash: command not found – anarchy May 22 '20 at 2:10

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.