arriven / db1000n

MIT License
1.17k stars 201 forks source link

What's the best way to keep the script running on the remote server? #503

Closed iblessedi closed 2 years ago

iblessedi commented 2 years ago

I have a VPS server and I run the script there. However for some reason after some time of running the script stops working with the error message 2022/04/11 19:22:47.306448 main.go:137: Terminating

I use the Ubuntu server and run the script with the command nohup ./db100n > log.log &

What's the best way to keep the script up and running? How to relaunch it if it fails? Thanks.

arriven commented 2 years ago

it depends on your goals and skill. The easiest way would probably be to wrap it in a loop like this https://www.cyberciti.biz/faq/bash-infinite-loop/

more complex approach would use something like custom runit or systemd service

iblessedi commented 2 years ago

Ok, so here is my script, maybe will be useful for somebody:

  1. Create a file called run.sh
  2. Add there the following script:
#!/bin/bash
NUMBER_OF_PROCESSES=$(ps aux | grep db1000n | grep -v grep | wc -l)
if (($NUMBER_OF_PROCESSES < 1)); then
        nohup /path/to/db1000n &>>/dev/null &
fi

In NUMBER_OF_PROCESSES < 1 - you can change 1 to number of processes you'd like to run. You can change /dev/null to any other file if you want to store logs (f.e. ~/log.log) /path/to/db1000n - path to your db1000n executable file

  1. Add write permissions to this file chmod +x run.sh
  2. Edit your crontab and add there (crontab -e): * * * * * /path/to/run.sh

That's all. Cron daemon will check if the script is alive and relaunch it if not. I purchased a VPS server and leave it with running script. I set NUMBER_OF_PROCESSES to 5 and I don't worry, I know that it will be running.