Score:0

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

co flag

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.

vanadium avatar
cn flag
Why would you want/need this? Tell us what you really want, and we may be able to come up with a much better solution than what you currently think is the solution to your problem.
Alobar avatar
co flag
Hello, i think the problem solved with Tmux script. And you're right, i should have told clearly what i want. I want this script works on startup with non-root user, but also have ability to restart on session or bot itself terminates.
Score:1
vn flag

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
Alobar avatar
co flag
So much thank you for your interest. Could you helping with making this script to run at startup with non-root user and find a way to keep eye on it to restart when it terminates?
Score:0
in flag

I guess your first option works fine, but screen will run in detached mode, so you'll see no output outside screen.

Check screen -ls to see running screen sessions and screen -r ID to attach to them.

Anyways, you could better use absolute paths: /bot/bot.sh instead of cd /bot/ && ./bot.sh:

#!/bin/sh
screen -S Bot -d -m /bot/bot.sh

You should be able to attach to the screen with screen -r Bot.


Your second script is quite wrong:

screen -S Bot -d -m -c "cd bot" && ./bot.sh
  • -c would expect a file:

    -c file override the default configuration file from "$HOME/.screenrc" to file.

  • It runs ./bot.sh outside the session because && will separate your commands. So, you're still in your initial directory where bot.sh does not exist. Hence the error.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.