Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
02-13-2013, 05:44 PM
|
#1
|
Member
Registered: Nov 2011
Location: Muskego, WI
Posts: 39
Rep:
|
How to create a script that kills all processes started by a specific user?
Hey guys, I was wondering if someone can help me create a script that runs every 5 minutes and kills all processes started by a specific user. I know nothing about scripting, so any help would be appreciated!
|
|
|
02-13-2013, 07:17 PM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
Can I ask why you would want to do this? Why not just lock or even delete the acct?
|
|
|
02-13-2013, 07:19 PM
|
#3
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Hi,
are you sure this is really what you want to do? If you give some information about the underlying problem we can probably find a better solution.
Anyway in answer to your question, an easy way to do this would be with killall and a crontab entry.
Evo2.
Last edited by evo2; 02-13-2013 at 07:31 PM.
Reason: Removed answer to homework question.
|
|
|
02-13-2013, 07:21 PM
|
#4
|
Member
Registered: Nov 2011
Location: Muskego, WI
Posts: 39
Original Poster
Rep:
|
Thanks. I am actually doing this for a project in class.
|
|
|
02-13-2013, 07:33 PM
|
#5
|
LQ Guru
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,726
|
Quote:
Originally Posted by Squerl101
Thanks. I am actually doing this for a project in class.
|
Ohh, homework question. Kind of ironic given your signature.
Evo2.
|
|
|
02-13-2013, 08:25 PM
|
#6
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,358
|
I'm sorta thinking that you should be somehow able to use the ps command to find the list of PIDs, then pipe that output into xarg which executes a kill.
I'm sorta thinking, also, that you can take it from there... mmm?
And you really do need to master this particular "fundamental Linux/Unix skill," because you're going to use it a lot. These operating systems give you a set of fairly primitive fundamental commands, most with quite a few options, plus the ability to string those commands together such that "the whole is greater than the sum of its parts." You need to spend some serious time exploring these possibilities, and I daresay that this is a lot of what your instructor has in mind. (Ask him or her.)
In fact ... you can even handle the entire assignment this way, if you ponder the list of available commands long enough ... (he said, cryptically).
Last edited by sundialsvcs; 02-13-2013 at 08:28 PM.
|
|
|
02-14-2013, 01:16 AM
|
#7
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
|
|
|
02-14-2013, 07:12 PM
|
#8
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,358
|
Yeah, and let us repeat, Squerl101 ... take full advantage of this very "boots-on-the-ground important" exercise! Pay close attention to what can be done using these techniques, and learn them well. Ask your instructor about it, and listen to what he or she has to say. If you've ever wondered "what all the fuss was about," with regard to (originally...) "Unix in the 1970's," this is it.
|
|
|
02-18-2013, 10:57 AM
|
#9
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
And consider what you might have to do in the presence of a fork bomb...
Killing multiple processes cannot be an atomic action.
|
|
|
02-18-2013, 05:19 PM
|
#10
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
Code:
pkill -KILL -u $user
DO NOT USE THIS ON root AND
DO NOT ASK HOW I KNOW THIS.
"May" require sudo.
|
|
|
02-18-2013, 06:45 PM
|
#11
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
Quote:
Originally Posted by Habitual
Code:
pkill -KILL -u $user
DO NOT USE THIS ON root AND
DO NOT ASK HOW I KNOW THIS.
"May" require sudo.
|
Does not work against a fork bomb. Fork bombs fork faster than pkill can find processes to kill...
Try this one: (NOTE THIS WILL CAUSE YOU PROBLEMS)
Code:
#include <unistd.h>
void main(void)
{
while (1) fork();
}
In the time it takes pkill to kill one process, this application will fork several hundred, each new process will then fork several hundred while pkill kills the next (plus the several hundred generated by each of previous processes...)
It will eventually hang your system UNLESS you have reasonable ulimits set. (I have 1024 concurrent processes...).
For me to run it then there would be 1023 maximum number of processes (after that, the fork fails).
Then, for each process that pkill manages to kill, any one of the other 1022 processes will replace it...
So... how do you stop it?
It isn't a single step operation, and works best when there are reasonable ulimits to prevent the system from hanging.
1. pkill -SIGSTOP -u <user>
This will put all of the users processes in the suspended state. And that means they will no longer be running. It also means that new processes cannot be started (the ulimit has been reached).
2. pkill -SIGKILL -u <user>
Now that the user processes are stopped, pkill can find each one and kill it.
Note: pkill may not exist on all systems (linux yes, Solaris, should be, but AIX/BSD... don't know)
See also: killall
There are also other strategies available - look at the fuser command where you know the file being used to hold the fork bomb.
Last edited by jpollard; 02-18-2013 at 06:47 PM.
|
|
|
02-18-2013, 07:10 PM
|
#12
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
It was a possible hint at the OP's topic opener " a script that runs every 5 minutes and kills all processes started by a specific user".
How fork bombs got put in the mix, I don't know. 
|
|
|
02-19-2013, 07:09 AM
|
#13
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
Quote:
Originally Posted by Habitual
It was a possible hint at the OP's topic opener " a script that runs every 5 minutes and kills all processes started by a specific user".
How fork bombs got put in the mix, I don't know. 
|
Such a script doesn't work when the user process forks faster.
|
|
|
All times are GMT -5. The time now is 12:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|