通过 crontab 创建 tmux 会话时,在 `tmux new session -d` 命令中指定 .bash_profile 源路径不起作用

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

我想达到什么目的? 我正在尝试让 crontab 终止之前的 tmux 会话并创建一个新的 tmux 会话(具有特定的 teamocil 设置)。

crontab 运行的简单 bash 脚本:

#!/bin/bash
tmux kill-session;
tmux new-session -d "source /home/qa/.bash_profile;teamocil settings;";

我有什么问题? 运行 这个脚本手动工作正常,但是当 运行 通过 crontab 时,它只会在至少 2 个其他 tmux 会话预先存在的情况下工作,即它会杀死一个会话作为脚本的一部分,如果有的话没有会话离开 crontab 不会创建第一个会话。如果在终止一个会话后还有另一个会话可用,则该脚本有效。

到目前为止的调查结果? 我发现如果我将源声明为 bash 脚本的一部分,而不是在 tmux new sessions 命令中,那么它就可以正常工作。为什么会这样?请参阅下面修改后的有效脚本:

#!/bin/bash
source /home/qa/.bash_profile
tmux kill-session;
tmux new-session -d "teamocil settings;";

理解为什么这会有所不同,帮助我更新其他脚本并且不再犯这个错误真的很有帮助。任何可以阐明这一点的光都表示赞赏。

直接在 crontab 行中将配置文件的路径放在脚本的路径之前,这将使脚本在将来的使用中更加灵活。

说明https://serverfault.com/questions/337631/crontab-execution-doesnt-have-the-same-environment-variables-as-executing-user

很好的回答https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables

另一个很好的答案https://unix.stackexchange.com/questions/6790/executing-a-sh-script-from-the-cron

推荐: 某处可能有你问题的答案。只需搜索一下即可。