質問

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 ?

役に立ちましたか?

解決

This works for me. Which should answer questions 1 and 2. Although it's not entirely clear what your actual issue is. Sounds like there might be pathing issue as well?

/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

It wouldn't hurt to redirect STDERR and STDOUT to your log file.

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

他のヒント

  • What makes you think that this is related to perl?
  • What makes you think that cron changed UIDs on you?
  • What makes you think that this is a permission issue?

My guess is that from your shell, you are using a different make binary than from your cron job. You have the PATH used in your cron job figured out, so please try the following:

$ which make

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

If you get two different results, you will need to edit your cron tab so it can use the PATH you are using in your shell.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top