Run a Bash Script in tmux at Login with Auto-Restart & Logging
🇬🇧 Guide to automatically running, restarting, and logging Bash scripts inside tmux on Arch Linux.
You can start a Bash script automatically in tmux at login on Arch Linux without systemd, and now also log its output for easier monitoring. This guide shows:
- Auto-Attach - attach immediately.
- Background - run detached.
- Auto-Restart - restart if the script crashes.
- Logging - save output to a file.
Simple (No Logging)
Step 1: Create ~/tmux_simple.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
SESSION="fbses"
# Check if the session already exists
tmux has-session -t $SESSION 2>/dev/null
if [ $? != 0 ]; then
# Session doesn't exist, create it and run the Python script
tmux new-session -d -s $SESSION
tmux send-keys -t $SESSION "python3 /home/fb/autostart/myprogram/run.py" Enter
fi
# Attach to the session
tmux attach -t $SESSION
Make it executable:
1
chmod +x ~/tmux_simple.sh
Step 2: Call it from ~/.bash_profile
1
2
[[ -f ~/.bashrc ]] && . ~/.bashrc
/home/fb/tmux_simple.sh
Auto-Attach with Auto-Restart and Logging
Step 1: Create ~/tmux.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
SESSION="fbses"
SCRIPT="/home/fb/autostart/myprogram/run.py"
LOG="/home/fb/autostart/myprogram/run.log"
# Check if session exists
tmux has-session -t $SESSION 2>/dev/null
if [ $? != 0 ]; then
# Create session and run Python script with restart loop and logging
tmux new-session -d -s $SESSION "while true; do python3 $SCRIPT >> $LOG 2>&1; echo 'Script crashed. Restarting in 5s...' >> $LOG; sleep 5; done"
fi
# Attach automatically
tmux attach -t $SESSION
Make it executable:
1
chmod +x ~/tmux.sh
Step 2: Call it from ~/.bash_profile
1
2
[[ -f ~/.bashrc ]] && . ~/.bashrc
/home/fb/tmux.sh
Background Only with Auto-Restart and Logging
Step 1: Create ~/tmux_bg.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
SESSION="fbses"
SCRIPT="/home/fb/autostart/myprogram/run.py"
LOG="/home/fb/autostart/myprogram/run.log"
# Check if session exists
tmux has-session -t $SESSION 2>/dev/null
if [ $? != 0 ]; then
# Create session and run Python script in restart loop with logging
tmux new-session -d -s $SESSION "while true; do python3 $SCRIPT >> $LOG 2>&1; echo 'Script crashed. Restarting in 5s...' >> $LOG; sleep 5; done"
fi
# Inform the user
echo "tmux session '$SESSION' is running in the background."
echo "Use 'tmux attach -t $SESSION' to view it."
echo "Script output is logged to $LOG"
Make it executable:
1
chmod +x ~/tmux_bg.sh
Step 2: Call it from ~/.bash_profile
1
2
[[ -f ~/.bashrc ]] && . ~/.bashrc
/home/fb/tmux_bg.sh
Manage tmux Sessions
- List sessions:
1
tmux ls
- Attach manually:
1
tmux attach -t fbses
- Detach (leave it running):
1
Ctrl + b, then d
- Kill session if needed:
1
tmux kill-session -t fbses
- Check the log:
1
tail -f /home/fb/autostart/myprogram/run.log
Which Version Should You Use?
-
Simple (No Logging) Use this for quick testing or small scripts. It’s good when you just want the script to run and see it immediately, without crash recovery or logs.
-
Auto-Attach with Auto-Restart & Logging Best for long-running scripts you want to monitor continuously. Automatically restarts if it crashes and keeps a full log of everything.
-
Background Only with Auto-Restart & Logging Ideal for scripts that should run without popping up a tmux window at login. Perfect for headless servers or background tasks, while still restartable and logged.
Tip: All versions create a tmux session under your user. You can use:
1
tmux ls
to see all sessions, including background-only ones, and:
1
tmux attach -t fbses
to attach manually whenever needed.
This way you can safely manage scripts without creating multiple sessions on repeated logins.