Auto Restart and Linux Logs

Tutorial explaining how to create a script.sh by the ssh terminal. To auto restart the server and save all the logs that happens on the distro, this script will start the server every time it crashes, closes or on the save server.

1 - Let's see if the nano is installed on your Linux machine, run on the terminal:

sudo apt install nano

2 - Let's go in the way of your server folder everything will be done here:

cd canary

3 - Now let's create the script.sh:

sudo nano canary.sh
  • Then enter:

#!/bin/bash
ulimit -c unlimited
while true; do ./canary 2>&1 | awk '{ print strftime("%F %T - "), 
$0; fflush(); }' | tee "logs/$(date +"%F %H-%M-%S.log")"; done
  • Save with CTRL+X after Y and Enter:

4 - Let's give permission to the created script:

sudo chmod 775 -R canary.sh

5 - Now let's create the folder where the server logs will be saved:

sudo mkdir logs

6 - Now just start in the terminal the script ./canary.sh, you need to have the screen installed:

  • Install the screen:

sudo apt install screen

6.1 - Basic screen commands:

Open a screen:

screen

Exit a screen:

CTRL + A, D

Back to the last open screen:

screen -r

Close a screen:

CTRL + K, Y/N

Info:

With the screen open use ./canary in the project root directory to start the server.

Credits:

  • Beats.