Puppet - removing user who is logged in?

According to this post, you can disconnect users using pkill -STOP -u USERNAME.
You can create a resource definition something this in puppet:

define kill_and_delete {
    exec { "killing $title":
        command => "pkill -STOP -u $title",
        onlyif  => "grep '^$title' /etc/passwd",
        before => User[$title],
    }
    user { $title: ensure => absent}
}

Afterwards, you use it like this:
kill_and_delete {'art': }
Note: I didn't test this.
see resource ordering - before and require and type reference - exec.


You terminate a session by killing its parent process, called the session leader. Find out which process it is with:

ps -dN|grep pts/3

or

As mentioned by @Nitz

pkill -9 -u username

And for reference check this link:

How can I force other users to log out?


I stumbled upon the same issue when deleting a user where processes were running. I have tested my solution on our production server and therefore I can provide a valid solution where I create users in a separate class using "ensure" variables. And based upon the variable I decide whether to call the process stop sequence or not as follows:

define your_class::user (
  $user   = $name,
  $ensure = 'present',
){

  # only call when user gets removed
  if $ensure == 'absent' {
    exec {
      "killing ${user}":
        command => "pkill -9 -u ${user}",
        # need to check if user exists and processes are running
        # otherwise command would fail with no processes
        onlyif  => "grep '^${user}' /etc/passwd && ps -u ${user}",
        # run before user gets removed
        before  => User[$user];
    }
  }

  # create/remove user with managed home
  user {
    $user:
      ensure     => $ensure,
      home       => "/home/${user}",
      managehome => true,
      shell      => '/bin/bash',
  }
}