how to redirect stdout for htpasswd in script

Posted on 2004-11-08
Last Modified: 2013-12-15


I've got an htpasswd authentication file that needs to be updated with the usernames and passwords from a database every day.

I've written a cron script that gets the values of the database and then calls htpasswd to update the authentication file.  The output from the cron job gets emailed to me every day - usually, just a lot of "Updating password for user **username**" lines.  I'd rather only get the cron email if there's a problem - a missing password or something.

My first thought was to just redirect stdout to /dev/null in the crontab, like this:

0 19 * * * /usr/bin/perl /home/user/bin/ >/dev/null

but it doesn't seem to work.   I still get the output from the htpasswd command emailed to me.  Is it getting printed on stderr?  if so, I don't want to redirect stderr to /dev/null because then I'll miss any errors that occur.

Am I doing anything wrong?  Anyone had any experience like this?

Question by:gothamww
    LVL 40

    Accepted Solution

    > Is it getting printed on stderr?


    Since you are using perl to update the auth file why not directly manuipulate the file with the Perl script. It is pretty easy to generate the encrypted password within Perl. For DES encrypted passwords you can use:

    sub descrypt()
      my($pw) = @_;
      $salts  = "0123456789";
      $salts .= "abcdefghijklmnopqrstuvwxyz";
      $nsalts = length($salts);
      $i = int(rand $nsalts);
      $ii = int(rand $nsalts);
      $salt = substr($salts, $i, 1).substr($salts, $ii, 1);
      $epw = crypt($pw, $salt);

    which is used like:

    $epw = descrypt($passwd);

    $passwd holds the plain text password and $epw the DES encrypted result.

    Author Comment


    so, the authentication file that htpasswd generates is just an ordinary text file that can be edited by any means, as long as the password is "DES" encrypted and each record is in the format:

    LVL 40

    Expert Comment

    Yep, assuming that you are using the flat file rather than a DB. If that's the case you can use any text editor to examine the auth file to see what it looks like. On a Linux system the encrypted passwords in the auth file can be encrypted with DES or MD5. The format of the encrypted password will auto-select the correct encryption method. See 'man 3 crypt' for details.

    Author Comment

    Thanks a lot jlevie

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now