What am I trying to achieve? I'm trying to get crontab to kill the previous tmux session and create a new tmux session (with particular teamocil settings).
Simple bash script that the crontab runs:
#!/bin/bash
tmux kill-session;
tmux new-session -d "source /home/qa/.bash_profile;teamocil settings;";
Issue I'm having? Running this script manually works fine, but when running through crontab it will only work if at least 2 other tmux sessions pre-exist, i.e. it kills a session as part of the script, if there are then no sessions left the crontab won't create the 1st session. If after killing a session there is still another session available, then the script works.
Findings so far? I've found that if I declare the source as part of the bash script, not in the tmux new sessions command, then it works fine. Why would this be? See modified script below that works:
#!/bin/bash
source /home/qa/.bash_profile
tmux kill-session;
tmux new-session -d "teamocil settings;";
It would be really helpful to understand why this made a difference, to help me update other scripts and not make this mistake again. Any light that can be shed on this is appreciated.
Put path to profile before path to script directly in crontab line, this will make script more flexible for future usage.
Explanation https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user
Very good answer https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables
another very good answer https://unix.stackexchange.com/questions/6790/executing-a-sh-script-from-the-cron
Recomendation: There is probably answer to you question somewhere. One needs just to search for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With