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

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 -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 -o StrictHostKeyChecking=no -tt nohup "/home/pi/sikkerhetskopiering.sh &"

Can anybody help me out with this?

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?

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:

#!/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

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

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?

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?

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

#!/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 :slight_smile:

I had to install screen on my linux machine first.

1 Like

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

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?

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"

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

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.

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:

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. :smile:

1 Like

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