prgrm prgrm - 1 year ago 84
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 = "";
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);
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);

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/

The log says every minute:

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

Any ideas?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download