LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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



Reply
 
Search this Thread
Old 02-13-2013, 06:44 PM   #1
Squerl101
Member
 
Registered: Nov 2011
Location: Muskego, WI
Posts: 34

Rep: Reputation: 0
Question 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!
 
Old 02-13-2013, 08:17 PM   #2
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Can I ask why you would want to do this? Why not just lock or even delete the acct?
 
Old 02-13-2013, 08:19 PM   #3
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,591

Rep: Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244
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 08:31 PM. Reason: Removed answer to homework question.
 
Old 02-13-2013, 08:21 PM   #4
Squerl101
Member
 
Registered: Nov 2011
Location: Muskego, WI
Posts: 34

Original Poster
Rep: Reputation: 0
Thanks. I am actually doing this for a project in class.
 
Old 02-13-2013, 08:33 PM   #5
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,591

Rep: Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244Reputation: 1244
Quote:
Originally Posted by Squerl101 View Post
Thanks. I am actually doing this for a project in class.
Ohh, homework question. Kind of ironic given your signature.

Evo2.
 
Old 02-13-2013, 09:25 PM   #6
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
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 09:28 PM.
 
Old 02-14-2013, 02:16 AM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
What sundialsvcs said

Now we know why, it makes more sense (sort of)..

Anyway, I suggest you bookmark/read these
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
http://www.adminschoice.com/crontab-quick-reference
 
Old 02-14-2013, 08:12 PM   #8
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
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.
 
Old 02-18-2013, 11:57 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,344

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
And consider what you might have to do in the presence of a fork bomb...

Killing multiple processes cannot be an atomic action.
 
Old 02-18-2013, 06:19 PM   #10
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,624
Blog Entries: 1

Rep: Reputation: Disabled
Code:
pkill -KILL -u $user
DO NOT USE THIS ON root AND
DO NOT ASK HOW I KNOW THIS.


"May" require sudo.
 
Old 02-18-2013, 07:45 PM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,344

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
Quote:
Originally Posted by Habitual View Post
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 07:47 PM.
 
Old 02-18-2013, 08:10 PM   #12
Habitual
Senior Member
 
Registered: Jan 2011
Distribution: Undecided
Posts: 3,624
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 02-19-2013, 08:09 AM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 2,344

Rep: Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595Reputation: 595
Quote:
Originally Posted by Habitual View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Kill script kills too many processes? MaxM Programming 15 10-21-2010 03:05 PM
Processes running while we create a new user and the user logs in rahulchandrak Linux From Scratch 3 05-08-2010 12:09 AM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 08:35 AM
Starting processes as a specific user? Loonyjuice Linux - Newbie 6 01-04-2007 03:17 AM
Priority of processes (using nice) started from within a script podollb Linux - Software 1 01-08-2006 12:44 PM


All times are GMT -5. The time now is 03:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration