How to create an sh script to run another sh script with Screen session or Tmux session

All I want is create a bash script that runs another bash script in a different folder, but with a detached screen session. Also it should be executed as non-root user. Here's what I've done:

#!/bin/sh
cd /bot/ && screen -S Bot -d -m ./bot.sh
chmod +x script.sh

After running it with ./script.sh, it doesn't give me an error, but no response.

Also I tried with:

#!/bin/sh
screen -S Bot -d -m -c "cd bot" && ./bot.sh

And come up with this error: ./script.sh: line 1: bot.sh: No such file or directory

I've a little question too: how can I make a script to track on this session to run it back if session or bot terminates? Thanks in advance.


Solution 1:

This script is based on a number of presumptions:

  1. I'm using tmux instead of screen

  2. The script you want to run inside tmux is /bot/bot.sh

So the ./script.sh I would suggest is the following:

#!/bin/sh
sessname="Bot"

# Create a new session named "$sessname"
tmux new-session -d -s "$sessname"

# Run command in the session "$sessname"
tmux send-keys -t "$sessname" "/bot/bot.sh" Enter

# Attach to the session "$sessname"
#tmux attach -t "$sessname"

A couple of remarks:

  1. You should always use absolute paths in a script (/bot/bot.sh, not ./bot.sh)
  2. If you also want to attach to the session, uncomment the last line