0

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.

2
  • 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.
    – vanadium
    Sep 10, 2021 at 6:54
  • 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.
    – Alobar
    Sep 11, 2021 at 6:48

2 Answers 2

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
1
  • 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?
    – Alobar
    Sep 11, 2021 at 6:51
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.