Advertisement
LittleFox94

Fetchmail for all users

Jan 28th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.98 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This script runs fetchmail for every user who has a .fetchmailrc in his home folder
  4. # and whose home-folder is in /home (so not system accounts).
  5. # fetchmail is started as the user, so running this script as root isn't a problem.
  6. # I have it in my crontab to run every 5 minutes.
  7. # If a user already has a running fetchmail-instance, the user is skipped this time.
  8.  
  9. for fetchmailrc in /home/*/.fetchmailrc
  10. do
  11.         user=${fetchmailrc/\/home\/}
  12.         user=${user/\/.fetchmailrc}
  13.         ps=$(ps -u "$user" | grep fetchmail)
  14.  
  15.         if [ -z "$ps" ]
  16.         then
  17.                 echo "Doing fetchmail for user '$user' ..."
  18.                 su -c "cd; fetchmail > .fetchmail.log" "$user"
  19.                 echo
  20.                 echo "Output of fetchmail:"
  21.                 cat "/home/$user/.fetchmail.log"
  22.         else
  23.                 echo "fetchmail already running for user '$user'"
  24.                 echo "ps output: $ps"
  25.         fi
  26.  
  27.         echo
  28.         echo
  29. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement