LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-09-2006, 03:52 PM   #1
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Rep: Reputation: 0
crontab to add a user


how can I use crontab to add a user?
currently I'm calling a perl script which reads the username from mysql...and does a useradd $username...but it's not working any thoughts?...suggestions?
 
Old 11-09-2006, 04:04 PM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
that should work fine. but you've not told us in what way it's not working, so we can't help you fix what you're currently doing.
 
Old 11-09-2006, 04:06 PM   #3
hockeyman_102
Member
 
Registered: Apr 2006
Location: Washington
Distribution: Suse, CentOS, Ubuntu
Posts: 124

Rep: Reputation: 15
may i ask why you want to continually add a user with crontab? A perl loop wouldn't work? Can you post your perl?
 
Old 11-09-2006, 04:11 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
i would assume a number of users are subbmitted during the day and added in bulk after moderation or suchlike.
 
Old 11-09-2006, 04:32 PM   #5
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Original Poster
Rep: Reputation: 0
well...it's just a simple perl script.... I'm not that good at perl so some of you might laugh..but here it goes...this script adds users fine when I run it from command line..but not from crontab...

#!/usr/bin/perl

use strict;
use DBI;
use Data:umper;

########################
# connect to db #
########################
my $db = "db";
my $dbuser= "user";
my $dbpassword = "";
my $hostname = "localhost";
my $dbh = DBI->connect("DBI:mysql:$db:$hostname", $dbuser, $dbpassword);
#lets read the db to see if there's stuff in it
my @arr;
my $query = "select * from user_to_add";
my $sth = $dbh->prepare($query);
$sth->execute();
while(my $hash = $sth->fetchrow_hashref()){
my $x = `userdd $$hash{'username'}`;
push(@arr, $$hash{'username'});
}
$sth->finish();
foreach my $name (@arr){
my $query2 = "delete from user_to_add where username = '$name'";
my $sth2 = $dbh->prepare($query2);
$sth2->execute();
$sth2->finish();
}
$dbh->disconnect();
close(FILE);

Last edited by sizzlingdesi; 11-09-2006 at 04:40 PM.
 
Old 11-09-2006, 05:34 PM   #6
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
My question would be, what does your crontab look like? If the script works fine by manually running it, it's not your script.

So it's a problem with either your entry to crontab or crond isn't running, unless you have other cron jobs running without problems.
 
Old 11-09-2006, 05:46 PM   #7
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by trickykid
My question would be, what does your crontab look like? If the script works fine by manually running it, it's not your script.

So it's a problem with either your entry to crontab or crond isn't running, unless you have other cron jobs running without problems.
* * * * /var/www/cgi-bin/lib/user_to_add.pl[/email]
* * * * * sleep 30; /var/www/cgi-bin/lib/user_to_add.pl

cause I want this crontab to run every 30 seconds.... and I checked it does execute the script..I printed some crap to STDERR...
 
Old 11-09-2006, 05:55 PM   #8
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by sizzlingdesi
* * * * /var/www/cgi-bin/lib/user_to_add.pl[/email]
* * * * * sleep 30; /var/www/cgi-bin/lib/user_to_add.pl

cause I want this crontab to run every 30 seconds.... and I checked it does execute the script..I printed some crap to STDERR...
You have the same script running twice from what it appears, one every second, the other every 30 seconds with your sleep 30 command. You know, you could just make cron run it every 30 seconds

And I guess I don't understand why you would have this run twice from cron, one missing the day of week..
 
Old 11-09-2006, 06:05 PM   #9
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Original Poster
Rep: Reputation: 0
k I fixed my cront(thanks a lot)...now why does it not create the user ?
 
Old 11-09-2006, 06:19 PM   #10
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by sizzlingdesi
k I fixed my cront(thanks a lot)...now why does it not create the user ?
I don't know, is there any errors output to your logs? Do any other cronjobs run without any problems? Is crond even running?
 
Old 11-10-2006, 12:39 AM   #11
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Original Poster
Rep: Reputation: 0
no there isn't any other errors in the log...and the crond's running too...is there a restriction that cron cannot create a user or something?....maybe not right permission or args?
 
Old 11-10-2006, 12:54 AM   #12
sizzlingdesi
LQ Newbie
 
Registered: Nov 2006
Posts: 11

Original Poster
Rep: Reputation: 0
figured out the problem...I needed to say /usr/sbin/useradd in my perl script...thankx a lot for your time
 
Old 11-10-2006, 03:07 AM   #13
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by sizzlingdesi
figured out the problem...I needed to say /usr/sbin/useradd in my perl script...thankx a lot for your time
Heh, always use the full path for commands in scripts.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
user crontab bujecas Linux - General 1 09-15-2006 05:49 AM
crontab --u user file binary_0011 Linux - Newbie 2 05-06-2006 05:14 AM
Crontab not working for user mago Slackware 5 04-03-2006 09:48 PM
problems setting the user in crontab p.gaic Linux - Software 1 07-14-2005 11:07 PM
how to add java file in crontab ??? kirti Linux - General 1 12-08-2004 04:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration