【发布时间】:2013-05-23 20:04:03
【问题描述】:
我有一个 Perl 脚本,它作为 cron 作业(每分钟)运行。现在,我在我的文件中进行某些计算和执行。每当作业运行时,它都无法访问文件系统中的某些文件夹。
例如,我有一个名为 /opt/tinyos-2.x/apps/ 的文件夹。我已将此文件夹公开(具有 777 权限)。我在我的 Perl 脚本中将目录更改为该文件夹并尝试在那里执行文件。但它没有运行,而是我收到错误消息:
make: *** No rule to make target `telosb'. Stop.
每当我转到该文件夹并以 sudo 执行文件时,我都可以复制此错误。
所以,我假设我的 cron 不知何故变成了“root”(我的 cron 与上述文件夹所有者在同一个用户下)。那么遇到这种问题我该怎么办呢?
我觉得如果我可以获取当前正在该 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)