Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am writing a script to create users and put them in the correct groups for FTP etc. Part of the script allows you to set an expiration date.

Could someone help me out with creating a script that would be able to run via cron each night and delete expired accounts and their home dirs?

Thanks. Pete.

share|improve this question
3  
"I can haz teh c0d3s" type questions are typically not regarded well here. Please give more information about your situation and give some evidence that you have given this a try yourself before coming here. – EEAA Jul 3 '12 at 12:54
And you store this expiration date where? How are you going to retrieve it later to do deletions? – voretaq7 Jul 3 '12 at 15:25
Shell? Perl? PHP? Python? – Justin Pearce Jul 3 '12 at 19:10

closed as not a real question by EEAA, mailq, WesleyDavid, Scott Pack, mgorven Jul 4 '12 at 4:01

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

Simple, stupid implementation in Ruby you'll probably have to improve.

#!/usr/bin/env ruby
require 'time'
$stdin.lines.each do |line|
  time, account = line.split ','
  home_directory = %x[getent passwd #{account}].split(':')[-2] # useless?
  if Time.now.to_i > Time.parse(time).to_i
    %x[rm -fr #{home_directory}] # useless?
    %x[userdel #{account}]
  end

Note that most modern userdel implementations have a -r switch that will take care of removing the home directory and spool for you. By using it you can get rid of the lines marked with # useless?.

Call this /usr/sbin/remove-expired and call it with /usr/sbin/remove-expired < /etc/accounts, where each line in /etc/accounts contains an expiration date for the account and the account name, separated by a colon.

For example:

2012-10-06:jack
May 12, 2015:john
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.