Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When creating tmux session through crontab, specifying the .bash_profile source path inside `tmux new session -d` command doesn't work

Tags:

bash

cron

tmux

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.

like image 704
SCoyle Avatar asked Mar 13 '18 16:03

SCoyle


1 Answers

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.

like image 88
iuooip Avatar answered Oct 27 '22 03:10

iuooip