crontab 路径和用户 [英] crontab PATH and USER

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

问题描述

我是使用 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.

还有一个 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 路径和用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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