prgrm prgrm - 4 months ago 10
Bash Question

Cron not executing my system() statement in PERL

I am working on a PERL script that adds users from a database and then deletes the database. The code works fine when I execute it.

use DBI;
use strict;
my $database = "database";
my $hostname = "123.4.56.78";
my $port = "3306";
my $user = "user";
my $password = "password";
my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
my $dbh = DBI->connect($dsn,$user,$password) or die "Can't connect to database: ", $DBI::errstr, "\n";


my $sql = 'SELECT * FROM somewhere';
my $sth = $dbh->prepare($sql);
$sth->execute();
while (my @row = $sth->fetchrow_array) {
system("useradd -g mygroup -d /home/somewhere/$row[0] -s /sbin/nologin $row[0]");
system("echo $row[0]:$row[1] | chpasswd");
}
my $sql_delete = 'TRUNCATE somewhere';
my $delete = $dbh->prepare($sql_delete);
$delete->execute();


However when I execute it through the Crontab, the system() statements are never executed, and the user are not added. They are deleted from the databse itself though.

* * * * * perl /var/perl/myperl.pl


The log says every minute:

Jul 26 15:42:01 ex40 CROND[7028]: (root) CMD (perl /var/perl/myperl.pl)


Any ideas?

Answer

Include the full path to useradd in the system() call.

Crontab does not get a copy of the environment, therefore the PATH environment variable isn't set.