# Does anybody know the correct command line for sshpass to SSH in, start a script and log out again?

**URL:** <https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974>\
**Category:** General\
**Tags:** docker\
**Created:** [17 November 2023 18:20 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974 "2023-11-17T18:20:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [17 November 2023 18:20 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/1 "2023-11-17T18:20:14Z")

</div>

I want to run the script sikkerhetskopiering.sh that copies the data from Node-RED, Home Assistant and a few other things, from an exec node (impossible directly because I have Node-RED in a Docker container, and I want to run it on the Docker host). Normally that should be quite easy:

sshpass -p PASSWORD ssh [pi@192.168.1.101](mailto:pi@192.168.1.101) -o StrictHostKeyChecking=no /home/pi/sikkerhetskopiering.sh

The StricktHostKeyChecking is necessary to run shell scripts, it seems.

The problem is that the script stops Node-RED (which it's supposed to do) and then stops executing, which it's not supposed to do. This should contain the necessary parts, but I can't get it to work.

sshpass -p PASSWORD ssh [pi@192.168.1.101](mailto:pi@192.168.1.101) -o StrictHostKeyChecking=no -tt nohup "/home/pi/sikkerhetskopiering.sh &"

Can anybody help me out with this?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [17 November 2023 20:44 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/2 "2023-11-17T20:44:46Z")

</div>

> [@Mastiff](#):
>
> The problem is that the script stops Node-RED (which it's supposed to do)

So that confirms that it is executing then.

How does sikkerhetskopiering.sh stop Node-red?  
What does it do next?  
Do you have any feedback from the script at all, echo or print statements?

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [18 November 2023 08:08 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/3 "2023-11-18T08:08:18Z")

</div>

Thanks for answering! Here's the script. I know it stops executing before the date for the backup is created, because that doesn't happen, so stopping Docker (and in that way stopping Node-RED) is the last thing that actually happens:

```auto
#!/bin/bash
echo "Monterer Sikkerhetskopier"
sudo mount -a

echo "Stopper Driftskontrollskript"
sudo systemctl stop driftskontroll.timer
sudo systemctl stop driftskontroll.service

echo "Stopper Docker med alle imager"
sudo systemctl stop docker.socket
sudo systemctl stop docker

echo "Lage katalog til sikkerhetskopiering"
now=$(date +"%Y-%m-%d,%R")
mkdir "/home/pi/Sikkerhetskopier/${now}"

echo "Kopiere Docker-Compose-katalogen"
cp -r "/home/pi/Docker-Compose" "/home/pi/Sikkerhetskopier/${now}"

echo "Kopiere Driftskontroll-katalogen"
cp -r "/home/pi/Driftskontroll" "/home/pi/Sikkerhetskopier/${now}"

echo "Starter Docker"
sudo systemctl start docker.socket
sudo systemctl start docker

echo "Starter Glances"
docker compose -f /home/pi/Docker-Compose/Glances/docker-compose.yml up -d

echo "Starter Node-RED"
docker compose -f /home/pi/Docker-Compose/Node-RED/docker-compose.yml up -d

echo "Starter Z-Wave JS UI"
docker compose -f /home/pi/Docker-Compose/Z-Wave-JS-UI/docker-compose.yml up -d

echo "Starter Home Assistant"
docker compose -f /home/pi/Docker-Compose/Homeassistant/docker-compose.yml up -d

echo "Starter Driftskontrollskript"
sudo systemctl start driftskontroll.timer

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 November 2023 10:02 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/4 "2023-11-18T10:02:29Z")

</div>

> [@Mastiff](#):
>
> The problem is that the script stops Node-RED (which it's supposed to do) and then stops executing, which it's not supposed to do

The script is being run by node-red, so when node red is stopped the script is killed.

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [18 November 2023 10:20 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/5 "2023-11-18T10:20:35Z")

</div>

No, as I say, it's run with a SSH login to the host, so Node-RED does not directly run this script. nohup (no hang up) means that after Node-RED has started the script on the Docker host and logged off the SSH session, the script should still go on, if I have not misunderstood what I've found on the web. If I'm mistaken, maybe the nohup should be in the script?

> **[Keep Commands Running After SSH Log Out With Nohup Linux Command](https://linuxhandbook.com/nohup-command/)**
>
> This article will show you how you can start a process and keep it running even after you have logged out using the nohup command.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 November 2023 10:30 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/6 "2023-11-18T10:30:00Z")

</div>

Is 192.168.1.101 the same machine that the node red with the exec node is running on? So you are using ssh to communicate with the host machine of the docker container?

---

<div class="post-metadata">

**Author:** ![jodelkoenig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jodelkoenig/32/44645_2.png) [@jodelkoenig](https://discourse.nodered.org/u/jodelkoenig)\
**Post date:** [18 November 2023 10:33 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/7 "2023-11-18T10:33:44Z")

</div>

Hey there,

this is the payload I attach to the login command:

`msg.payload = "'screen -S nrrestart -d -m ./services/iq/restartstack.sh'"`

And this is my restartstack.sh

```auto
#!/bin/bash
cd /home/pi/services/iq
docker compose down
docker compose up -d

```

After docker compose down, nodered is no longer running. However, it comes up again, because docker compose up -d brings it back 🙂

I had to install screen on my linux machine first.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 November 2023 11:23 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/8 "2023-11-18T11:23:24Z")

</div>

> [@Mastiff](#):
>
> sshpass -p PASSWORD ssh [pi@192.168.1.101](mailto:pi@192.168.1.101) -o StrictHostKeyChecking=no -tt nohup "/home/pi/sikkerhetskopiering.sh &"

Should the nohup be inside the quotes?  
`"nohup /home/pi/sikkerhetskopiering.sh &"`

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [18 November 2023 11:51 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/9 "2023-11-18T11:51:14Z")

</div>

I don't know nearly enough about docker to give useful advice but if the script terminates when docker goes down does that mean you are sshing into the container?  
Is it possible to connect to the host operating system and restart docker from there?

---

<div class="post-metadata">

**Author:** ![jodelkoenig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jodelkoenig/32/44645_2.png) [@jodelkoenig](https://discourse.nodered.org/u/jodelkoenig)\
**Post date:** [18 November 2023 13:31 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/10 "2023-11-18T13:31:25Z")

</div>

I just tested nohup in my setup, and it shutdown nodered but doesn't bring it back again. The script ends, when nodered is stopped.

I vaguely recall testing nohup in the past as well but ended up with above mentioned solution with "screen"

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [19 November 2023 09:34 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/11 "2023-11-19T09:34:43Z")

</div>

It didn't throw an error now, but nothing happened with the script either. ☹

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [19 November 2023 09:36 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/12 "2023-11-19T09:36:53Z")

</div>

No, I'm SSHing **out** of the container to the host to run the script there. The script, if I can get it to run when Node-RED goes down, starts Docker and then the containers again when the backup is done.

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [19 November 2023 09:52 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/13 "2023-11-19T09:52:12Z")

</div>

The Screen way worked for me too, thank you very much! This is what I'm using SSH-ing from the container to the host to run the script:

```auto
sshpass -p MY-PASSWORD ssh pi@192.168.1.101 -o StrictHostKeyChecking=no "screen -S backup -d -m /home/pi/backup.sh"

```

Tested it now, and it stopped all containers, did the copy and then started the containers again! Problem solved. 😄

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [3 December 2023 09:52 UTC](https://discourse.nodered.org/t/does-anybody-know-the-correct-command-line-for-sshpass-to-ssh-in-start-a-script-and-log-out-again/82974/14 "2023-12-03T09:52:24Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
