1

I have a Perl script which is running as cron job (every minute). Now, I do certain computation and execution in my file. Whenever the job is running, it does not get access to some folders in the file system.

For example, I have folder named /opt/tinyos-2.x/apps/. I have kept this folder public (with 777 permission). I change directory to this folder in my Perl script and try to execute files there. But it does not run, instead I get error saying that:

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

I can replicate this error whenever I go to that folder and execute files as sudo.

  1. So, I am assuming that my cron somehow becomes "root" (I have my cron under the same user as above folder owner). So what should I do with this kind of problem?

  2. I feel that if I can get user who is currently running in that Perl script, I might find it's user id . Could anyone tell me how can I get the user id in my Perl script? After checking at various places, I found this solution to know user id - print $ENV{"LOGNAME"}. Is this the correct way to know the user in Perl script run by cron job ?

Here is what I have: I have a user 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

LoadPrograms.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;

When my job is running in cron, I don't see any output or anything happening. I get mail at user id location /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.

This means that It is not running my code at the location I mentioned: /opt/tinyos-2.x/apps/Null.

Can anybody help me with this ?

4

2 回答 2

0
  • 是什么让您认为这与 perl 有关?
  • 是什么让你认为 cron 改变了你的 UID?
  • 是什么让您认为这是一个权限问题?

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

$ which make

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

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

于 2013-05-24T08:27:32.110 回答
0

这对我有用。哪个应该回答问题 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

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

* * * * * /usr/bin/perl /var/www/web/myfolder/LoadPrograms.pl >> /var/www/web/log.txt 2>&1
于 2013-05-23T20:51:18.530 回答