Linux – How to automatically delete Linux users after fixed period

linuxusers

I have a script that programmatically creates users, but we'd like to delete the users after 48 hours, as well as any files they own.

I know you can set an expiration with the useradd command, but I don't know how to take the next step of deleting the user and its associated files on expiration.

Is there an easy way to do this?

Thank you!

Best Answer

Assuming you have a user creation script that sets $username, add this:

cat <<EOF | at now + 2 days
userdel -f -r $username
EOF

It also pays to set an expiration on the account when it is added, as extra insurance.