crontab PATH和USER [英] crontab PATH and USER

查看:94
本文介绍了crontab PATH和USER的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新调度cron和crontab的任务。我试图安排一个任务的执行,好像我已经登录,打开一个终端,并自己执行。

I am new to scheduling tasks with cron and crontab. I am trying to schedule execution of a task as if I had logged on, opened a terminal, and executed it myself.

但是,我安排了一个任务,以帮助我观察计划任务正在执行什么$ USER和$ PATH,这是我发现的:

However, I scheduled a task to help me observe what $USER and $PATH a scheduled task is executing with, and this is what I found:

$ crontab -l
41 11 * * * echo "USER: $USER" > ~/Desktop/cron_env.log; echo "PATH: $PATH" >> ~/Desktop/cron_env.log
$ cat ~/Desktop/cron_env.log
USER:
PATH: /usr/bin:/bin

似乎$ USER未设置,$ PATH是非常基本的和/或默认的。相反,这是我看到当我打开一个终端(登录),并回声相同的信息:

It appears as though $USER is not set, and $PATH is something very basic and/or default. On the contrary, this is what I see when I open a terminal (logged in) and echo this same information:

USER: aschirma
PATH: /usr/lib/jvm/java-6-sun/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/pkg/icetools/bin:/pkg/hwtools/bin:/pkg/netscape/bin:/pkg/gnu/bin

我需要做什么使我的crontab任务按我想要的方式运行。

What do I need to do to make my crontab tasks run the way I want?

推荐答案

根据man 5 crontab,你可以在crontab中设置环境变量,通过在cron线之前写它们。

According to "man 5 crontab" you can set environment variables in your crontab, by writing them before your cron lines.

a crontab,所以你只需要复制/粘贴它:

There is also an example of a crontab so you just have to copy/paste it :

$ man 5 crontab | grep -C5 PATH | tail 
# and files in /etc/cron.d. These files also have username fields,
# 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 usercommand
17 * * * *  root  cd / && run-parts --report /etc/cron.hourly
25 6 * * *  root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7  root  test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )

你可以调整你的PATH或任何环境变量任何你想要的。但这个例子对于典型的情况来说已经足够了。

So you can adjust your PATH or any environment variable to whatever you want. But this example seems enough for typical cases.

这篇关于crontab PATH和USER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆