【问题标题】:User id in perl cron jobperl cron作业中的用户ID
【发布时间】:2013-05-23 20:04:03
【问题描述】:

我有一个 Perl 脚本,它作为 cron 作业(每分钟)运行。现在,我在我的文件中进行某些计算和执行。每当作业运行时,它都无法访问文件系统中的某些文件夹。

例如,我有一个名为 /opt/tinyos-2.x/apps/ 的文件夹。我已将此文件夹公开(具有 777 权限)。我在我的 Perl 脚本中将目录更改为该文件夹并尝试在那里执行文件。但它没有运行,而是我收到错误消息:

make: *** No rule to make target `telosb'.  Stop. 

每当我转到该文件夹​​并以 sudo 执行文件时,我都可以复制此错误。

  1. 所以,我假设我的 cron 不知何故变成了“root”(我的 cron 与上述文件夹所有者在同一个用户下)。那么遇到这种问题我该怎么办呢?

  2. 我觉得如果我可以获取当前正在该 Perl 脚本中运行的用户,我可能会找到它的用户 id。谁能告诉我如何在我的 Perl 脚本中获取用户 ID?在各个地方检查后,我发现了这个知道用户ID的解决方案-print $ENV{"LOGNAME"}。这是在 cron 作业运行的 Perl 脚本中了解用户的正确方法吗?

这是我所拥有的: 我有一个用户 ID“abc”。

abc@mymachine:crontab -e

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

* * * * * /usr/bin/perl /var/www/web/myfolder/LoadPrograms.pl >> /var/www/web/log.txt

加载程序.pl

#!/usr/bin/perl
use DBI;
use strict;
use Thread;
use File::Path;

$nullPath = "/opt/tinyos-2.x/apps/Null";

chdir($nullPath) or die "cant chnge directory to null directory\n";
my $nullProgramCommand = "make telosb install.1 bsl,/dev/ttyUSB0";

my $output.=`$nullProgramCommand`;
print $output;

当我的作业在 cron 中运行时,我看不到任何输出或发生任何事情。我在用户 ID 位置 /var/mail/abc 收到邮件:

Content-Type: text/plain; charset=ANSI_X3.4-1968
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/abc>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=abc>

make: *** No rule to make target `telosb'.  Stop.

这意味着它没有在我提到的位置运行我的代码:/opt/tinyos-2.x/apps/Null。

有人可以帮我吗?

【问题讨论】:

  • 在命令中添加一些调试输出:pwd;env;ls;id;make telosb install.1 bsl,/dev/ttyUSB0 然后在终端中运行相同的序列并比较结果。并且可能添加 -d 标志来制作,但要注意它会产生 lot 的输出。
  • 您能否向我们展示您希望您的 make 命令使用的 Makefile 的内容,并告诉我们它的位置?我认为您没有 Perl 问题或权限问题 - 看起来很像未找到 Makefile。
  • 这是 Makefile 的内容:COMPONENT=NullAppC include $(MAKERULES)

标签: perl cron crontab userid


【解决方案1】:

这对我有用。哪个应该回答问题 1 和 2。尽管尚不完全清楚您的实际问题是什么。听起来可能还有路径问题?

/etc/crontab:

* * * * * cblack /home/cblack/Misc/Tests/test.pl

test.pl:

my $username = $ENV{LOGNAME} || $ENV{USER} || getpwuid($<);
open my $fh, ">>", "testfile";
print $fh scalar(localtime) . "-- testing for $username\n";
close $fh;

/home/cblack/testfile

Thu May 23 13:48:01 2013-- testing for cblack

STDERRSTDOUT 重定向到您的日志文件不会有什么坏处。

* * * * * /usr/bin/perl /var/www/web/myfolder/LoadPrograms.pl >> /var/www/web/log.txt 2>&1

【讨论】:

  • 嗨@chrsblck,检查我上面的编辑。我打印了用户 ID,它向我显示了我正在运行 cron 作业的相同用户 ID。但我的问题是 - 我需要在某个位置执行一个文件,而 cron 不允许我这样做。如果我手动去那个位置并运行上面提到的命令,(make telosb install ...),它工作正常。但是在 cron 下它不起作用.. :(
  • @JModi 好的,那么这一定是我在回答中提到的路径问题。
【解决方案2】:
  • 是什么让您认为这与 perl 有关?
  • 是什么让您认为 cron 更改了您的 UID?
  • 是什么让您认为这是权限问题?

我的猜测是,在您的 shell 中,您使用的 make 二进制文件与您的 cron 作业中的不同。您已经确定了您的 cron 作业中使用的 PATH,因此请尝试以下操作:

$ which make

$ PATH=/usr/bin:/bin which make

如果您得到两个不同的结果,您将需要编辑您的 cron 选项卡,以便它可以使用您在 shell 中使用的 PATH

【讨论】:

  • 这让我想到了所有这些,因为我走上了一条死路。我不知道我在做什么有什么问题。所以,我尝试了所有的可能性。无论如何,我发现了问题所在。问题出在 cron 上。当您作为 cron 运行任何作业时,它不知道环境变量。因此,您必须在发出“系统命令”之前显式导出环境变量。
猜你喜欢
  • 1970-01-01
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2013-07-05
  • 2012-03-02
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
相关资源
最近更新 更多