You are right, if your computer goes to sleep and your ssh session closes, your script will stop.
To prevent this, you can use & and then disown to push your script to the background and remove the process from the shell's job control, but it still leaves it connected to the terminal.
For example:
- I ran my script
./script.sh &
- We will obtain the JobID, because it will start running in background:
[1] 7812
- With this JobID I can run the
disown command to remove the process from the shell's job control:
disown 7812
At this point, you can close your ssh session and the script will continue working.
Alternatively, you also can use Screen. When screen is called, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would.
Screen will keep running even if your session gets disconnected. When you reconnect to the server you can reconnect to the screen session and everything will be as if nothing happened, this is a good option if you want to see the results of your script in the standard output.
You can consult the official documentation on the following link.
Also, you can use the following guide to know how to use screen.
Additionally, you can see the following post, where the community explains the difference between nohup, disown and & .
I hope you find this information useful.