Stop Docker Container via SSH and/or if other machine does not respond


Recommended Posts

I have two servers running Unraid. Server B runs a docker container that monitors the files on Server A via NFS remote mount. If Server A goes offline for a prolonged period of time, the container on Server B can't see the remote mount. This results in the container having to rescan all files on Server A once it comes back up, which can take several days.

 

What I'd like to do:

Server A should stop the container on Server B when the array is stopped, and start it again when the array is started.

 

I've tried configuring a user script on Server A to SSH into Server B and start/stop the container on array start/stop, but I can't figure out how to make SSH commands work in a user script.

 

Secondary use:

Server B should frequently ping Server A and, if it does not respond, stop the container.

 

The above script should be sufficient most of the time, but if Server A hangs or crashes, it won't be able to execute the array stop script, so this should serve as appropriate backup. I am not an experienced bash user, so I do not know how to accomplish this.

 

 

If you've done something similar, or otherwise know how it might be accomplished, please let me know. Cheers!

Link to comment

we can start an array  with:


#!/bin/bash emcmd cmdStart=Start
and stop an array with 
#!/bin/bash emcmd cmdStop=Stop

(source) https://forums.unraid.net/topic/79924-startstop-array-via-cli/

i dont know how you can see the current status. if you can figure out the current status of the array. 
perhaps it you can find it here. https://wiki.unraid.net/Console 

beside that, i done something remotely similair just now. 

image.thumb.png.e5f4c04921003644a4756e9576b82968.png
 

ive made an node-red automation in home assistant that checks if a VM is active, and if its active using Virsh domstate it sets the state. (bottom part)
Or when i manually change the state the VM will turn itself on or off (top 2 smaller parts) 

Ive exported what i made, you can directly import this in Node-red, also install these pallets

"node-red-contrib-bigssh"

"node-red-contrib-bigline" 
then you can import this module to set it up. 

[{"id":"143dd69542798222","type":"tab","label":"Set state of VM workstation","disabled":false,"info":"","env":[]},{"id":"d3b7983c.c97908","type":"bigssh","z":"143dd69542798222","name":"virsh domstate 'Work Vm\"","commandLine":"virsh domstate 'Work Vm'","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":true,"myssh":"3a26c7d9be25c952","x":270,"y":1240,"wires":[["477828b0.0ecb68"],[],[]]},{"id":"7a7fdea4.2ec13","type":"inject","z":"143dd69542798222","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"120","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"checkedstatus","payloadType":"flow","x":200,"y":1120,"wires":[["d3b7983c.c97908"]]},{"id":"477828b0.0ecb68","type":"bigline","z":"143dd69542798222","name":"","filename":"","format":"utf8","keepEmptyLines":false,"x":480,"y":1240,"wires":[["57e4f6a28403bb1c"],[]]},{"id":"57d1bc03d78f8cc6","type":"debug","z":"143dd69542798222","name":"debug 1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1240,"y":1220,"wires":[]},{"id":"c0f2ca54619b43a6","type":"api-call-service","z":"143dd69542798222","name":"","server":"424d958a.550f6c","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_on","areaId":[],"deviceId":[],"entityId":["input_boolean.work_vm_unraid"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":1220,"wires":[["57d1bc03d78f8cc6"]]},{"id":"69e280abb4bb2501","type":"api-call-service","z":"143dd69542798222","name":"","server":"424d958a.550f6c","version":5,"debugenabled":false,"domain":"input_boolean","service":"turn_off","areaId":[],"deviceId":[],"entityId":["input_boolean.work_vm_unraid"],"data":"","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1040,"y":1280,"wires":[["2918880847aead4b"]]},{"id":"2918880847aead4b","type":"debug","z":"143dd69542798222","name":"debug 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1240,"y":1280,"wires":[]},{"id":"57e4f6a28403bb1c","type":"function","z":"143dd69542798222","name":"function 1","func":"var state = msg.payload.trim(); // trim the output to remove any extra whitespaces\nif (state === 'running') {\n    msg.payload = 'Running';\n} else if (state === 'shut off') {\n    msg.payload = 'Shut off';\n} else {\n    msg.payload = 'Unknown';\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":1240,"wires":[["074f53b1651d9286","a7a8ce0300c31706"]]},{"id":"074f53b1651d9286","type":"debug","z":"143dd69542798222","name":"debug 3","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":860,"y":1140,"wires":[]},{"id":"a7a8ce0300c31706","type":"switch","z":"143dd69542798222","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Running","vt":"str"},{"t":"eq","v":"Shut off","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":830,"y":1260,"wires":[["c0f2ca54619b43a6"],["69e280abb4bb2501"]]},{"id":"ed1f89c48c5cd42c","type":"bigssh","z":"143dd69542798222","name":"virsh start 'Work Vm'","commandLine":"virsh start 'Work Vm'","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":true,"myssh":"3a26c7d9be25c952","x":960,"y":800,"wires":[[],[],[]]},{"id":"aa22a57b149a4984","type":"bigssh","z":"143dd69542798222","name":"virsh shutdown 'Work Vm'","commandLine":"virsh shutdown 'Work Vm'","commandArgs":"","minError":1,"minWarning":1,"noStdin":false,"format":"","payloadIsArg":true,"myssh":"3a26c7d9be25c952","x":970,"y":920,"wires":[[],[],[]]},{"id":"ba95c1fa3e19dec9","type":"server-state-changed","z":"143dd69542798222","name":"","server":"424d958a.550f6c","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.work_vm_unraid","entityidfiltertype":"exact","outputinitially":true,"state_type":"habool","haltifstate":"false","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":520,"y":920,"wires":[["aa22a57b149a4984"],[]]},{"id":"42e9f73e317e6bbf","type":"server-state-changed","z":"143dd69542798222","name":"","server":"424d958a.550f6c","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_boolean.work_vm_unraid","entityidfiltertype":"exact","outputinitially":true,"state_type":"habool","haltifstate":"true","halt_if_type":"bool","halt_if_compare":"is","outputs":2,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":520,"y":800,"wires":[["ed1f89c48c5cd42c"],[]]},{"id":"663084b0f96e0085","type":"comment","z":"143dd69542798222","name":"Check every minute if the actual state is changed","info":"","x":480,"y":1080,"wires":[]},{"id":"948684c228ff774f","type":"comment","z":"143dd69542798222","name":"crrect the state if required","info":"","x":1070,"y":1140,"wires":[]},{"id":"f13982d83493ba22","type":"comment","z":"143dd69542798222","name":"turn on boolean","info":"","x":260,"y":800,"wires":[]},{"id":"983a68fed78f959e","type":"comment","z":"143dd69542798222","name":"turn off boolean","info":"","x":260,"y":920,"wires":[]},{"id":"3a26c7d9be25c952","type":"SSH_Credentials","host":"192.168.30.14","port":"22","userlabel":"[email protected]"},{"id":"424d958a.550f6c","type":"server","name":"Home Assistant","addon":true}]



 

 

Link to comment

to find your docker container id

docker ps


to kill or stop the container

docker stop container <container number>
docker kill container <container number>

 

remotely run the script using SSH

ssh root@<remoteserverip> "docker stop container <containerID>"

Sause: https://www.cyberciti.biz/faq/linux-unix-osx-bsd-ssh-run-command-on-remote-machine-server/

you have to setup ssh to login without the password tough, forgot how to do that, but google/ chatgpt is your friend:
for windows:
 

ssh-keygen 
type C:\Users\username\.ssh\id_rsa.pub | ssh user@hostname 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

 

Edited by okkies
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.