I need to know how to kill all processes for a logged in user except the first one in linux redhat 6.

for example, AZain user is logged in multiple times from different machines so I need to allow only the first process and kill all others for this user:

who -u
AZain    pts/2        2016-06-23 08:34   .         27191 (localhost)
AZain    pts/4        2016-06-23 09:01   .         28885 (localhost)
root     pts/0        2016-06-23 08:14   .         25962 (
AZain    pts/1        2016-06-23 08:34 00:27       27169 (
AZain    pts/3        2016-06-23 09:01   .         28867 (

Edit: I am trying to do so as I am having an application in which consumes licenses for users. So every new session for the same user would consume more license. If I could prevent any new logins for the same user that would be great.

  • Specifically interactive shell sessions? Or do you mean "processes"? – Kusalananda Jun 23 '16 at 6:44
  • 1
    It's not entirely reliable, but since PIDs are generally assigned in ascending order, you could look for the lowest shell PID and kill everything other than it owned by the user in question. – DopeGhoti Jun 23 '16 at 6:46
  • @DopeGhoti "Not entirely reliable"?! With PID wrap-around this is terrible advice, especially on a multi-user machine. Also, although the OP states "Linux", as a general solution, this would definitely not work on e.g. OpenBSD (random PIDs). – Kusalananda Jun 23 '16 at 6:51
  • 1
    @Kusalananda Ok I need to do this as I am having an application that consume licenses for users. So multiple logins for the same user would consume more licenses and I need to allow 1 session per user only so that only one could be logged in by same username. – Ahmed Zein Jun 23 '16 at 7:28
  • 1
    Just logging in causes a license to be used in this application. – Ahmed Zein Jun 23 '16 at 9:02

This is a classic XY Problem - you're trying to limit the number of instances of licensed software being run per user, but you're focused on limiting shell logins. This is the wrong problem to solve.

A better solution is:

  • Rename the program, e.g. from /usr/local/bin/foo to /usr/local/bin/foo.real

  • Write a wrapper script with the same name and path as the original program (e.g. /usr/local/bin/foo) to check if the user is already running it.

    If yes, exit with an appropriate error message. If not, run the original program (/usr/local/bin/foo.real)

A very simple example:

#! /bin/sh

if pgrep -u "$USER" foo.real >/dev/null 2>&1 ; then
  echo "You are already running foo" >&2
  exit 1

Note: This works for well-behaved users who understand the licensing issue and don't mind following the rules. It will not stop a sneaky user from running foo.real themselves. You could stop sneaky but not very bright users by adding alias foo.real=foo to /etc/profile or /etc/bash.bashrc.

You could run a root cron job which detected instances of foo.real being run that did not have a shell script called foo as the parent process. The cron job could kill any such instances and/or email you a warning (to avoid spamming yourself, keep track of the process IDs for foo.real and don't mail yourself multiple warnings about the same PID).

BTW, if your licensed software comes with some kind of license manager, check its documentation to see if it can restrict the number of instances per user.

  • The OP says "Just logging in causes a license to be used in this application." But you're right, there might be something in the license manager that can be tweaked. – Kusalananda Jun 24 '16 at 8:18
  • @Kusalananda - yes, he did say that, I didn't notice that until you pointed it out. However, given the confusion of the OP and his inability to clearly express what he means and what he wants, I'm going to assume that he's mistaken on that point. In any case, this answer is still useful for applications with either no license limitations (but a wish to limit resource usage) or less insane limits - merely logging in shouldn't trigger use of a license, actually running the app should. – cas Jun 24 '16 at 8:34
  • It could be that he means "logging into" the application, i.e. starting it and entering some credentials or something. Anyway, XY. – Kusalananda Jun 24 '16 at 8:36
  • yeah, that's what i assumed he meant. – cas Jun 24 '16 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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