我正试图让cron调用正确的PATH.当我从shell运行Python脚本时,脚本运行正常,因为它使用了bashrc中设置的PATH,但是当我使用cron时,所有的PATH都没有在bashrc中使用.有没有一个文件我可以输入PATHs为cron像bashrc或从bashrc调用PATHs的方法?
对不起我不认为我说的正确,我可以得到正确的脚本运行(意味着crontab中脚本的PATH不是问题),只是当脚本运行时我运行一个构建,这使用了PATHs设置.bashrc.当我登录时运行脚本时,.bashrcPATH会被拉入.由于cron不会在shell中运行,因为它不会拉入.bashrc.有没有办法在不必编写bash脚本包装的情况下将其拉入?
chr*_*ley 159
我用过/etc/crontab.我使用vi并输入了我需要的PATH到这个文件中并以root身份运行它.普通的crontab会覆盖您已设置的PATH.关于如何做到这一点的好教程.
系统范围的cron文件如下所示:
This has the username field, as used by /etc/crontab.
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file.
# This file also has a username field, that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
42 6 * * * root run-parts --report /etc/cron.daily
47 6 * * 7 root run-parts --report /etc/cron.weekly
52 6 1 * * root run-parts --report /etc/cron.monthly
01 01 * * 1-5 root python /path/to/file.py
Run Code Online (Sandbox Code Playgroud)
joe*_*ler 47
最有可能的是,cron在非常稀疏的环境中运行.通过附加一个转储env到如下文件的虚拟作业来检查cron正在使用的环境变量:
* * * * * env > env_dump.txt
Run Code Online (Sandbox Code Playgroud)
将其与env普通shell会话中的输出进行比较.
您可以通过在crontab顶部定义它们,将自己的环境变量添加到本地crontab.
这是一个快速修复前置$PATH到当前的crontab:
# echo PATH=$PATH > tmp.cron
# echo >> tmp.cron
# crontab -l >> tmp.cron
# crontab tmp.cron
Run Code Online (Sandbox Code Playgroud)
生成的crontab看起来类似于chrissygormley的答案,在crontab规则之前定义了PATH.
Dou*_*der 21
你应该把完整的路径放在你的crontab.这是最安全的选择.
如果您不想这样做,可以在程序周围放置一个包装器脚本,并在其中设置PATH.
例如
01 01 * * * command
Run Code Online (Sandbox Code Playgroud)
变为:
01 01 * * * /full/path/to/command
Run Code Online (Sandbox Code Playgroud)
任何调用的东西都cron应该对它运行的程序非常小心,并且可能为PATH变量设置自己的选择.
编辑:
如果您不知道which <command>要从shell 执行命令的位置,它会告诉您路径.
EDIT2:
因此,一旦您的程序运行,它应该做的第一件事是设置PATH和任何其他所需的变量(例如LD_LIBRARY_PATH)到脚本运行所需的值.
基本上没有考虑如何修改cron环境以使其更适合您的程序/脚本 - 让脚本处理它给出的环境,通过在启动时设置适当的环境.
小智 14
在我的crontab命令行之前设置PATH对我有用:
* * * * * PATH=$PATH:/usr/local/bin:/path/to/some/thing
Run Code Online (Sandbox Code Playgroud)
Art*_*tan 14
让你的变量适合你,这将允许访问t
在/etc/profile.d/*.sh中定义PATH
无论何时输入bash登录shell(例如,从控制台登录或通过ssh登录),以及桌面会话加载时DisplayManager,都会执行/etc/profile.d目录中具有.sh扩展名的文件.
例如,您可以创建文件/etc/profile.d/myenvvars.sh并设置如下变量:
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0
export PATH=$PATH:$JAVA_HOME/bin
Run Code Online (Sandbox Code Playgroud)
使用登录选项执行crontab!
CRONTAB使用Environment Variables运行脚本或命令
0 9 * * * cd /var/www/vhosts/foo/crons/; bash -l -c 'php -f ./download.php'
0 9 * * * cd /var/www/vhosts/foo/crons/; bash -l -c download.sh
Run Code Online (Sandbox Code Playgroud)
Tre*_*iño 12
使用正确的值将PATH定义添加到用户crontab中将有助于...我只用以下内容填充我的:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Run Code Online (Sandbox Code Playgroud)
这足以使我的所有脚本都运行...如果需要,可以在那里包含任何自定义路径.
bra*_*ech 11
当您从控制台运行它但脚本失败时,您的脚本会起作用.
你的crontab没有正确的路径变量(可能还有shell)
添加当前shell和路径crontab
#!/bin/bash
#
# Date: August 22, 2013
# Author: Steve Stonebraker
# File: add_current_shell_and_path_to_crontab.sh
# Description: Add current user's shell and path to crontab
# Source: http://brakertech.com/add-current-path-to-crontab
# Github: hhttps://github.com/ssstonebraker/braker-scripts/blob/master/working-scripts/add_current_shell_and_path_to_crontab.sh
# function that is called when the script exits (cleans up our tmp.cron file)
function finish { [ -e "tmp.cron" ] && rm tmp.cron; }
#whenver the script exits call the function "finish"
trap finish EXIT
########################################
# pretty printing functions
function print_status { echo -e "\x1B[01;34m[*]\x1B[0m $1"; }
function print_good { echo -e "\x1B[01;32m[*]\x1B[0m $1"; }
function print_error { echo -e "\x1B[01;31m[*]\x1B[0m $1"; }
function print_notification { echo -e "\x1B[01;33m[*]\x1B[0m $1"; }
function printline {
hr=-------------------------------------------------------------------------------------------------------------------------------
printf '%s\n' "${hr:0:${COLUMNS:-$(tput cols)}}"
}
####################################
# print message and exit program
function die { print_error "$1"; exit 1; }
####################################
# user must have at least one job in their crontab
function require_gt1_user_crontab_job {
crontab -l &> /dev/null
[ $? -ne 0 ] && die "Script requires you have at least one user crontab job!"
}
####################################
# Add current shell and path to user's crontab
function add_shell_path_to_crontab {
#print info about what's being added
print_notification "Current SHELL: ${SHELL}"
print_notification "Current PATH: ${PATH}"
#Add current shell and path to crontab
print_status "Adding current SHELL and PATH to crontab \nold crontab:"
printline; crontab -l; printline
#keep old comments but start new crontab file
crontab -l | grep "^#" > tmp.cron
#Add our current shell and path to the new crontab file
echo -e "SHELL=${SHELL}\nPATH=${PATH}\n" >> tmp.cron
#Add old crontab entries but ignore comments or any shell or path statements
crontab -l | grep -v "^#" | grep -v "SHELL" | grep -v "PATH" >> tmp.cron
#load up the new crontab we just created
crontab tmp.cron
#Display new crontab
print_good "New crontab:"
printline; crontab -l; printline
}
require_gt1_user_crontab_job
add_shell_path_to_crontab
Run Code Online (Sandbox Code Playgroud)

小智 8
我发现的最简单的解决方法如下所示:
* * * * * root su -l -c command
Run Code Online (Sandbox Code Playgroud)
此示例su以 root 用户身份调用,并使用用户的完整环境(包括 $PATH)启动 shell,设置就像他们已登录一样。它在不同发行版上的工作方式相同,比源 .bashrc 更可靠(这对于me)并避免对特定路径进行硬编码,如果您提供示例或设置工具并且不知道用户系统上的发行版或文件布局,这可能会成为问题。
su如果您想要与 root 不同的用户,您也可以在后面指定用户名,但您可能应该在命令root之前保留该参数su,因为这可以确保su有足够的权限切换到您指定的任何用户。