I have a crontab running every hour. The user running it has environment variabless in the .bash_profile
that work when the user runs the job from the terminal, however, obviously these don't get picked up by crontab when it runs.
我每小时都有一个crontab。当用户从终端运行作业时,运行它的用户在.bash_profile中有环境变量,但是很明显,当它运行时,这些不会被crontab选中。
I've tried setting them in .profile
and .bashrc
but they still don't seem to get picked up. Does anyone know where I can put environment vars that crontab can pick up?
我试过将它们设置为。profile和。bashrc,但它们似乎仍然没有被选中。有人知道我可以把crontab拾取到的环境vars放在哪里吗?
10 个解决方案
#1
72
Have 'cron' run a shell script that sets the environment before running the command.
使用“cron”运行一个shell脚本,该脚本在运行该命令之前设置环境。
Always.
总是这样。
# @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
# Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min Hour Day Month Weekday Command
#-----------------------------------------------------------------------------
0 * * * * /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1 1 * * * /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23 1 * * 1-5 /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2 3 * * 0 /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21 3 1 * * /usr/bin/ksh /work1/jleffler/bin/Cron/monthly
The scripts in ~/bin/Cron are all links to a single script, 'runcron', which looks like:
~/bin/Cron中的脚本都是链接到单个脚本“runcron”的链接,看起来像:
: "$Id: runcron.sh,v 2.1 2001/02/27 00:53:22 jleffler Exp $"
#
# Commands to be performed by Cron (no debugging options)
# Set environment -- not done by cron (usually switches HOME)
. $HOME/.cronfile
base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base
if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi
exec $cmd ${@:+"$@"}
(Written using an older coding standard - nowadays, I'd use a shebang '#!' at the start.)
(使用旧的编码标准编写——现在,我将使用shebang '#!“开始时)。
The '~/.cronfile' is a variation on my profile for use by cron - rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment - you can pretend it is the same as $HOME.)
“~ /。cronfile是我的个人资料中的一个变体,用于cron——严格的非交互式的,没有回音,因为它是嘈杂的。您可以安排执行.profile等等。(REAL_HOME是我所处环境的人工制品——你可以假装它和$HOME是一样的。)
So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my 'weekday' command looks like:
因此,该代码读取适当的环境,然后从我的主目录执行命令的非cron版本。例如,我的“工作日”命令如下:
: "@(#)$Id: weekday.sh,v 1.10 2007/09/17 02:42:03 jleffler Exp $"
#
# Commands to be done each weekday
# Update ICSCOPE
n.updics
The 'daily' command is simpler:
“每日”命令更简单:
: "@(#)$Id: daily.sh,v 1.5 1997/06/02 22:04:21 johnl Exp $"
#
# Commands to be done daily
# Nothing -- most things are done on weekdays only
exit 0
#2
189
You can define environment variables in the crontab itself when running crontab -e
from the command line.
当从命令行运行crontab -e时,可以在crontab本身中定义环境变量。
LANG=nb_NO.UTF-8
LC_ALL=nb_NO.UTF-8
# m h dom mon dow command
* * * * * sleep 5s && echo "yo"
This feature is only available to certain implementations of cron. Ubuntu and Debian currently use vixie-cron which allows these to be declared in the crontab file (also GNU mcron).
该特性只适用于cron的某些实现。Ubuntu和Debian目前使用vixie-cron,它允许在crontab文件(也就是GNU mcron)中声明它们。
Archlinux and RedHat use cronie which does not allow environment variables to be declared and will throw syntax errors in the cron.log. Workaround can be done per-entry:
Archlinux和RedHat使用cronie,它不允许声明环境变量,并且会在cron.log中抛出语法错误。每个项目都可以进行变通:
# m h dom mon dow command
* * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo"
#3
108
I got one more solution for this problem:
我还有一个解决方法:
0 5 * * * . $HOME/.profile; /path/to/command/to/run
In this case it will pick all the environment variable defined in your $HOME/.profile file.
在这种情况下,它将选择$HOME/中定义的所有环境变量。概要文件。
Of course $HOME is also not set, you have to replace it with the full path of your $HOME.
当然$HOME也没有设置,您必须用$HOME的完整路径替换它。
#4
26
Setting vars in /etc/environment
also worked for me in Ubuntu. As of 12.04, variables in /etc/environment are loaded for cron.
在/etc/environment中设置vars在Ubuntu中也很有用。从12.04开始,cron将加载/etc/environment中的变量。
#5
18
Expanding on @carestad example, which I find easier, is to run the script with cron and have the environment in the script.
扩展@carestad示例(我觉得更容易)是使用cron运行脚本并在脚本中包含环境。
In crontab -e file:
在crontab - e文件:
SHELL=/bin/bash
*/1 * * * * $HOME/cron_job.sh
In cron_job.sh file:
在cron_job。sh文件:
#!/bin/bash
source $HOME/.bash_profile
some_other_cmd
Any command after the source of .bash_profile will have your environment as if you logged in.
在.bash_profile源之后的任何命令都将具有您的环境,就像您登录一样。
#6
12
For me I had to set the environment variable for a php application. I resloved it by adding the following code to my crontab.
对于我来说,我必须为php应用程序设置环境变量。通过向crontab中添加以下代码,我对它产生了厌恶。
$ sudo crontab -e
crontab:
定时任务:
ENVIRONMENT_VAR=production
* * * * * /home/deploy/my_app/cron/cron.doSomethingWonderful.php
and inside doSomethingWonderful.php I could get the environment value with:
里面doSomethingWonderful。我可以用:
<?php
echo $_SERVER['ENVIRONMENT_VAR']; # => "production"
I hope this helps!
我希望这可以帮助!
#7
9
Whatever you set in crontab
will be available in the cronjobs, both directly and using the variables in the scripts.
无论您在crontab中设置什么,都可以在cronjobs中使用,包括直接使用脚本中的变量。
Use them in the definition of the cronjob
You can configure crontab
so that it sets variables that then the can cronjob use:
您可以配置crontab,以便它设置变量,然后可以使用cronjob:
$ crontab -l
myvar="hi man"
* * * * * echo "$myvar. date is $(date)" >> /tmp/hello
Now the file /tmp/hello
shows things like:
现在文件/tmp/hello显示如下内容:
$ cat /tmp/hello
hi man. date is Thu May 12 12:10:01 CEST 2016
hi man. date is Thu May 12 12:11:01 CEST 2016
Use them in the script run by cronjob
You can configure crontab
so that it sets variables that then the scripts can use:
您可以配置crontab,使其设置脚本可以使用的变量:
$ crontab -l
myvar="hi man"
* * * * * /bin/bash /tmp/myscript.sh
And say script /tmp/myscript.sh
is like this:
/ tmp / myscript说脚本。上海是这样的:
echo "Now is $(date). myvar=$myvar" >> /tmp/myoutput.res
It generates a file /tmp/myoutput.res
showing:
它生成一个文件/tmp/myoutput。res展示:
$ cat /tmp/myoutput.res
Now is Thu May 12 12:07:01 CEST 2016. myvar=hi man
Now is Thu May 12 12:08:01 CEST 2016. myvar=hi man
...
#8
4
Expanding on @Robert Brisita has just expand , also if you don't want to set up all the variables of the profile in the script, you can select the variables to export on the top of the script
扩展@Robert Brisita刚刚展开,如果您不想在脚本中设置概要文件的所有变量,您可以选择要在脚本顶部导出的变量
In crontab -e file:
在crontab - e文件:
SHELL=/bin/bash
*/1 * * * * /Path/to/script/script.sh
In script.sh
在script.sh
#!/bin/bash
export JAVA_HOME=/path/to/jdk
some-other-command
#9
3
Instead of
而不是
0 * * * * sh /my/script.sh
Use bash -l -c
使用bash - l - c
0 * * * * bash -l -c 'sh /my/script.sh'
#10
2
Another way - inspired by this this answer - to "inject" variables is the following (fcron example):
另一种“注入”变量的方式——受到这个答案的启发——是以下(fcron示例):
%daily 00 12 \
set -a; \
. /path/to/file/containing/vars; \
set +a; \
/path/to/script/using/vars
From help set
:
从帮助设定:
-a Mark variables which are modified or created for export.
-为导出而修改或创建的标记变量。
Using + rather than - causes these flags to be turned off.
使用+而不是-使这些标志被关闭。
So everything in between set -
and set +
gets exported to env
and is then available for other scripts, etc. Without using set
the variables get sourced but live in set
only.
所以set -和set +之间的所有东西都导出到env,然后可以用于其他脚本,等等。
Aside from that it's also useful to pass variables when a program requires a non-root account to run but you'd need some variables inside that other user's environment. Below is an example passing in nullmailer vars to format the e-mail header:
除此之外,当程序需要非根帐户运行时,传递变量也是很有用的,但是在其他用户的环境中需要一些变量。下面是一个通过nullmailer vars来格式化电子邮件头的示例:
su -s /bin/bash -c "set -a; \
. /path/to/nullmailer-vars; \
set +a; \
/usr/sbin/logcheck" logcheck
#1
72
Have 'cron' run a shell script that sets the environment before running the command.
使用“cron”运行一个shell脚本,该脚本在运行该命令之前设置环境。
Always.
总是这样。
# @(#)$Id: crontab,v 4.2 2007/09/17 02:41:00 jleffler Exp $
# Crontab file for Home Directory for Jonathan Leffler (JL)
#-----------------------------------------------------------------------------
#Min Hour Day Month Weekday Command
#-----------------------------------------------------------------------------
0 * * * * /usr/bin/ksh /work1/jleffler/bin/Cron/hourly
1 1 * * * /usr/bin/ksh /work1/jleffler/bin/Cron/daily
23 1 * * 1-5 /usr/bin/ksh /work1/jleffler/bin/Cron/weekday
2 3 * * 0 /usr/bin/ksh /work1/jleffler/bin/Cron/weekly
21 3 1 * * /usr/bin/ksh /work1/jleffler/bin/Cron/monthly
The scripts in ~/bin/Cron are all links to a single script, 'runcron', which looks like:
~/bin/Cron中的脚本都是链接到单个脚本“runcron”的链接,看起来像:
: "$Id: runcron.sh,v 2.1 2001/02/27 00:53:22 jleffler Exp $"
#
# Commands to be performed by Cron (no debugging options)
# Set environment -- not done by cron (usually switches HOME)
. $HOME/.cronfile
base=`basename $0`
cmd=${REAL_HOME:-/real/home}/bin/$base
if [ ! -x $cmd ]
then cmd=${HOME}/bin/$base
fi
exec $cmd ${@:+"$@"}
(Written using an older coding standard - nowadays, I'd use a shebang '#!' at the start.)
(使用旧的编码标准编写——现在,我将使用shebang '#!“开始时)。
The '~/.cronfile' is a variation on my profile for use by cron - rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment - you can pretend it is the same as $HOME.)
“~ /。cronfile是我的个人资料中的一个变体,用于cron——严格的非交互式的,没有回音,因为它是嘈杂的。您可以安排执行.profile等等。(REAL_HOME是我所处环境的人工制品——你可以假装它和$HOME是一样的。)
So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my 'weekday' command looks like:
因此,该代码读取适当的环境,然后从我的主目录执行命令的非cron版本。例如,我的“工作日”命令如下:
: "@(#)$Id: weekday.sh,v 1.10 2007/09/17 02:42:03 jleffler Exp $"
#
# Commands to be done each weekday
# Update ICSCOPE
n.updics
The 'daily' command is simpler:
“每日”命令更简单:
: "@(#)$Id: daily.sh,v 1.5 1997/06/02 22:04:21 johnl Exp $"
#
# Commands to be done daily
# Nothing -- most things are done on weekdays only
exit 0
#2
189
You can define environment variables in the crontab itself when running crontab -e
from the command line.
当从命令行运行crontab -e时,可以在crontab本身中定义环境变量。
LANG=nb_NO.UTF-8
LC_ALL=nb_NO.UTF-8
# m h dom mon dow command
* * * * * sleep 5s && echo "yo"
This feature is only available to certain implementations of cron. Ubuntu and Debian currently use vixie-cron which allows these to be declared in the crontab file (also GNU mcron).
该特性只适用于cron的某些实现。Ubuntu和Debian目前使用vixie-cron,它允许在crontab文件(也就是GNU mcron)中声明它们。
Archlinux and RedHat use cronie which does not allow environment variables to be declared and will throw syntax errors in the cron.log. Workaround can be done per-entry:
Archlinux和RedHat使用cronie,它不允许声明环境变量,并且会在cron.log中抛出语法错误。每个项目都可以进行变通:
# m h dom mon dow command
* * * * * export LC_ALL=nb_NO.UTF-8; sleep 5s && echo "yo"
#3
108
I got one more solution for this problem:
我还有一个解决方法:
0 5 * * * . $HOME/.profile; /path/to/command/to/run
In this case it will pick all the environment variable defined in your $HOME/.profile file.
在这种情况下,它将选择$HOME/中定义的所有环境变量。概要文件。
Of course $HOME is also not set, you have to replace it with the full path of your $HOME.
当然$HOME也没有设置,您必须用$HOME的完整路径替换它。
#4
26
Setting vars in /etc/environment
also worked for me in Ubuntu. As of 12.04, variables in /etc/environment are loaded for cron.
在/etc/environment中设置vars在Ubuntu中也很有用。从12.04开始,cron将加载/etc/environment中的变量。
#5
18
Expanding on @carestad example, which I find easier, is to run the script with cron and have the environment in the script.
扩展@carestad示例(我觉得更容易)是使用cron运行脚本并在脚本中包含环境。
In crontab -e file:
在crontab - e文件:
SHELL=/bin/bash
*/1 * * * * $HOME/cron_job.sh
In cron_job.sh file:
在cron_job。sh文件:
#!/bin/bash
source $HOME/.bash_profile
some_other_cmd
Any command after the source of .bash_profile will have your environment as if you logged in.
在.bash_profile源之后的任何命令都将具有您的环境,就像您登录一样。
#6
12
For me I had to set the environment variable for a php application. I resloved it by adding the following code to my crontab.
对于我来说,我必须为php应用程序设置环境变量。通过向crontab中添加以下代码,我对它产生了厌恶。
$ sudo crontab -e
crontab:
定时任务:
ENVIRONMENT_VAR=production
* * * * * /home/deploy/my_app/cron/cron.doSomethingWonderful.php
and inside doSomethingWonderful.php I could get the environment value with:
里面doSomethingWonderful。我可以用:
<?php
echo $_SERVER['ENVIRONMENT_VAR']; # => "production"
I hope this helps!
我希望这可以帮助!
#7
9
Whatever you set in crontab
will be available in the cronjobs, both directly and using the variables in the scripts.
无论您在crontab中设置什么,都可以在cronjobs中使用,包括直接使用脚本中的变量。
Use them in the definition of the cronjob
You can configure crontab
so that it sets variables that then the can cronjob use:
您可以配置crontab,以便它设置变量,然后可以使用cronjob:
$ crontab -l
myvar="hi man"
* * * * * echo "$myvar. date is $(date)" >> /tmp/hello
Now the file /tmp/hello
shows things like:
现在文件/tmp/hello显示如下内容:
$ cat /tmp/hello
hi man. date is Thu May 12 12:10:01 CEST 2016
hi man. date is Thu May 12 12:11:01 CEST 2016
Use them in the script run by cronjob
You can configure crontab
so that it sets variables that then the scripts can use:
您可以配置crontab,使其设置脚本可以使用的变量:
$ crontab -l
myvar="hi man"
* * * * * /bin/bash /tmp/myscript.sh
And say script /tmp/myscript.sh
is like this:
/ tmp / myscript说脚本。上海是这样的:
echo "Now is $(date). myvar=$myvar" >> /tmp/myoutput.res
It generates a file /tmp/myoutput.res
showing:
它生成一个文件/tmp/myoutput。res展示:
$ cat /tmp/myoutput.res
Now is Thu May 12 12:07:01 CEST 2016. myvar=hi man
Now is Thu May 12 12:08:01 CEST 2016. myvar=hi man
...
#8
4
Expanding on @Robert Brisita has just expand , also if you don't want to set up all the variables of the profile in the script, you can select the variables to export on the top of the script
扩展@Robert Brisita刚刚展开,如果您不想在脚本中设置概要文件的所有变量,您可以选择要在脚本顶部导出的变量
In crontab -e file:
在crontab - e文件:
SHELL=/bin/bash
*/1 * * * * /Path/to/script/script.sh
In script.sh
在script.sh
#!/bin/bash
export JAVA_HOME=/path/to/jdk
some-other-command
#9
3
Instead of
而不是
0 * * * * sh /my/script.sh
Use bash -l -c
使用bash - l - c
0 * * * * bash -l -c 'sh /my/script.sh'
#10
2
Another way - inspired by this this answer - to "inject" variables is the following (fcron example):
另一种“注入”变量的方式——受到这个答案的启发——是以下(fcron示例):
%daily 00 12 \
set -a; \
. /path/to/file/containing/vars; \
set +a; \
/path/to/script/using/vars
From help set
:
从帮助设定:
-a Mark variables which are modified or created for export.
-为导出而修改或创建的标记变量。
Using + rather than - causes these flags to be turned off.
使用+而不是-使这些标志被关闭。
So everything in between set -
and set +
gets exported to env
and is then available for other scripts, etc. Without using set
the variables get sourced but live in set
only.
所以set -和set +之间的所有东西都导出到env,然后可以用于其他脚本,等等。
Aside from that it's also useful to pass variables when a program requires a non-root account to run but you'd need some variables inside that other user's environment. Below is an example passing in nullmailer vars to format the e-mail header:
除此之外,当程序需要非根帐户运行时,传递变量也是很有用的,但是在其他用户的环境中需要一些变量。下面是一个通过nullmailer vars来格式化电子邮件头的示例:
su -s /bin/bash -c "set -a; \
. /path/to/nullmailer-vars; \
set +a; \
/usr/sbin/logcheck" logcheck