When the server is restarted, Tmux sessions are «killed». This script will automatically start the sessions:
#!/bin/bash
# Session for Cors
SESSION_CORS="cors"
WORKING_DIR_CORS="/var/www/cors_boosta__usr/data/www/cors.site.com"
COMMAND_CORS='node server.js'
# Session for echo-dev
SESSION_DEV="echo-dev"
WORKING_DIR_DEV="/var/www/dev_boosta_r_usr/data/www/dev.site.com"
COMMAND_DEV='export PATH=$PATH:$(npm config get prefix)/bin && bash -c "source ~/.bashrc && laravel-echo-server start"'
# Session for echo-prod
SESSION_PROD="echo-prod"
WORKING_DIR_PROD="/var/www/soft_boosta__usr/data/www/soft.site.com"
COMMAND_PROD='export PATH=$PATH:$(npm config get prefix)/bin && bash -c "source ~/.bashrc && laravel-echo-server start"'
# Function to start tmux session
start_tmux_session() {
local session_name=$1
local working_dir=$2
local command=$3
tmux has-session -t "$session_name" 2>/dev/null
if [ $? != 0 ]; then
tmux new-session -s "$session_name" -n 'server' -d
tmux send-keys -t "$session_name" "cd \"$working_dir\"" C-m
tmux send-keys -t "$session_name" "$command" C-m
fi
}
# Start sessions
start_tmux_session "$SESSION_CORS" "$WORKING_DIR_CORS" "$COMMAND_CORS"
start_tmux_session "$SESSION_DEV" "$WORKING_DIR_DEV" "$COMMAND_DEV"
start_tmux_session "$SESSION_PROD" "$WORKING_DIR_PROD" "$COMMAND_PROD"
And set the command to run in Cron:
crontab -e
@reboot /bin/bash /path-to-the-script/start-tmux.sh
In this example, I am starting 2 instances of the laravel-echo server and CORS proxy.