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.
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?
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 ?