我的crontab中有这个:
* * * * * cd /etc && . ./cron.sh>>cron.log
Run Code Online (Sandbox Code Playgroud)
在我的cron.sh(可执行文件)中,我具有:
#!/bin/sh
echo "hello world"
export MyVar="abcd"
Run Code Online (Sandbox Code Playgroud)
它既可以通过cron手动运行,也可以手动运行,但是只有在使用以下命令手动运行时,才设置环境变量:
. ./cron.sh
Run Code Online (Sandbox Code Playgroud)
谁能帮忙。我知道它与源有关,但我无法弄清楚。
这也不起作用:
* * * * * cd /etc && sh ./cron.sh>>cron.log
Run Code Online (Sandbox Code Playgroud)
. will export variables in the current shell, which is the one spawned by the cron, not yours.
If you want to add an extra variable to your shells, use the ~/.profile et al (specifically the /etc/profile that is shared by all users).