How to start a tmux within bash right after reboot
I have created a script to run upon reboot of an Ubuntu instance.
crontab -e
Then added this to the script:
@reboot /home/ubuntu/startup.sh
This is how the script looks like:
#!/bin/bash
tmux new-session -d -s my_server
tmux send-keys -t my_server:0 ". /home/ubuntu/venv/bin/activate" C-m
tmux send-keys -t my_server:0 "cd ~/canonicaliser_api" C-m
tmux send-keys -t my_server:0 "git checkout new-schema" C-m
tmux send-keys -t my_server:0 "git pull" C-m
tmux send-keys -t my_server:0 "cd ~/canonicaliser_api/canonicaliser/workers" C-m
tmux send-keys -t my_server:0 "python person_worker.py" C-m
After reboot I do a tmux attach
to see the results.
$ . /home/ubuntu/venv/bin/activate
cd ~/canonicaliser_api
(venv)$ (venv)$ git checkout new-schema
git pull
cd ~/canonicaliser_api/canonicaliser/workers
python person_worker.py
Already on 'new-schema'
Your branch is up-to-date with 'origin/new-schema'.
...
7 files changed, 157 insertions(+), 20 deletions(-)
(venv)$ (venv)$ Traceback (most recent call last):
File "person_worker.py", line 4, in <module>
from app import create_app, log
ImportError: No module named app
(venv)$
Strangely, the virtualenv seems activate and yet doesn't seem to have worked.
The environment seems also strange. If I run the script manually (instead of reboot) and do a tmux attach
, I see (venv)ubuntu@ip-172-xx-xx-xxx:
instead of just (venv)$
. Could that be the problem?
Solution 1:
I finally found the reason.
All I had to do was to add the Python path like this:
tmux send-keys -t my_server:0 "export PYTHONPATH=$PYTHONPATH:/home/ubuntu/canonicaliser_api" C-m
Now it works. Hope this helps somebody else with similar requirements.