1

I have a bot which I run inside tmux session to keep it alive always. Sometimes, I server reboots and if I am not home or with the internet, my bot remains offline. I can add a cron job to start the bot. I have a script like this

tmux new -s bot
bash '/home/user/bot_start.sh'

but what it does is that it starts a new tmux session but runs

bot_start.sh

outside of tmux session. I want it to run that script inside the tmux session it opens up i.e., "bot"

1 Answer 1

2

The tmux command takes the command that it should execute within the session as an argument on the command line. If no argument is given, an interactive shell is started. Once the command (or the shell) terminates, the tmux session exits too.

Therefore, you could use

tmux new-session -s bot bash "$HOME/bot_start.sh"

This would start a new tmux session and run the given script in it.

To test whether there is already a session and avoid starting a new one if there is:

! tmux has-session -t bot 2>/dev/null && tmux new-session -s bot bash "$HOME/bot_start.sh"

or, more verbosely,

if ! tmux has-session -t bot 2>/dev/null; then
    tmux new-session -s bot bash "$HOME/bot_start.sh"
fi
2
  • Can I run my commands instead of executing the script? like this cd ~/bot && python3 app.py if it is possible, then what would be the of doing it?
    – Ali Hasan
    Nov 24, 2018 at 13:17
  • @AliHasan Well, you could do cd ~/bot && tmux new -s bot python3 app.py, sure.
    – Kusalananda
    Nov 24, 2018 at 14:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .