Skip to content

[Bug] $HOME environment variable is empty in the scheduled task #8249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
lmzxtek opened this issue Mar 25, 2025 · 1 comment
Open

[Bug] $HOME environment variable is empty in the scheduled task #8249

lmzxtek opened this issue Mar 25, 2025 · 1 comment
Assignees

Comments

@lmzxtek
Copy link

lmzxtek commented Mar 25, 2025

Contact Information

No response

1Panel Version

v1.10.28-lts

Problem Description

在服务器上使用pyenv搭建好了多个版本的python,系统自带的是3.9.2,另外使用pyenv安装了3.12.0。
使用pyenv global 3.12.0将bash 默认使用的python版本设置为了3.12.0。

现在想用1Panel的计划任务定时执行一个任务,但发现运行python命令之后,python还是调用的系统自带的3.9.2,这说明.bashrc配置文件没有正确加载。

找了很久产生这个问题的原因,最终发现是因为.bashrc文件中使用了$HOME变量,而在1Panel计划任务中运行任务时的bash中$HOME变量为空,导致配置文件没有正确加载,所以python的版本还是系统自带的。

可以通过以下脚本进行测试:

#!/bin/bash

python3 --version    # 默认输出为系统自带的版本:3.9.2
whereis pyenv        # 查找不到pyenv命令,输出为空

echo "\$HOME: $HOME" # $HOME变量为空
echo "~: " ~         # 显示当前账户根目录,输出:/root 
    
HOME=$(echo ~)       # 将HOME变量设置为用户根目录

echo "\$HOME: $HOME" # 显示$HOME变量: /root

source ~/.bashrc     # 重新加载bash配置文件

python3 --version    # 输出使用pyenv托管的python版本: 3.12.0
whereis pyenv        # 输出pyenv命令目标:pyenv: /root/.pyenv/bin/pyenv

执行以上任务之后,任务输出如下:

Python 3.9.2
pyenv:
$HOME: 
~:  /root
$HOME: /root
Python 3.12.7
pyenv: /root/.pyenv/bin/pyenv

Steps to Reproduce

要解决以上问题,只需要设置好任务执行时调用的bash中的$HOME环境变量为root账户的根目录。
临时使用的话,可以在任务命令中自行设置$HOME变量为~( $HOME=~),再重新加载.bashrc配置文件(source .bashrc),即可。

建议:1Panel管理面板在执行计划任务时,自动设置$HOME变量为~,这样就不用在脚本代码里面显示重新加载bash配置文件了。

The expected correct result

No response

Related log output

Additional Information

No response

@wanghe-fit2cloud wanghe-fit2cloud changed the title [Bug] 计划任务中$HOME环境变量为空 [Bug] $HOME environment variable is empty in the scheduled task Mar 25, 2025
@wanghe-fit2cloud
Copy link
Member

感谢反馈,我们先在本地环境复现下上述问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants