LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-16-2009, 03:48 AM   #1
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Rep: Reputation: 30
Seperating lines in a shell script


Greetings all,
Due to another issue that I'm having with firefox, specifically the flash plugin which keeps causing it to freeze, I'm forced rather frequently to stop firefox with "kill -9" and restart it. I decided to try to write a shell script to automate this.

At this point, I have this
Code:
ps -A | grep firefox-bin | cut -c -6
Which lists all running processes, selects only the lines for firefox, then cuts off everything except for the process numbers. The only problem that I'm having is that most of the time there are two firefox-bin processes, and I get two different numbers on two different lines. How can I run kill -9 on one of these at a time?

I'd like to be able to automate it such that I run just one command and it kills firefox, then restarts it for me.

Thanks in advance,
-Primux
 
Old 10-16-2009, 03:54 AM   #2
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
You were nearly there...

Try this:

Code:
$ ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {}
To get it to start FireFox after killing it, add the following:

Code:
$ ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {} && /path/to/firefox/executable
See the man page for xargs for info:

Code:
$ man xargs
Happy hacking

Last edited by rizhun; 10-16-2009 at 03:56 AM. Reason: Added line to restart firefox after kill
 
Old 10-16-2009, 03:59 AM   #3
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 528

Rep: Reputation: 129Reputation: 129
Quote:
Originally Posted by slackwarefan View Post
Code:
ps -A | grep firefox-bin | cut -c -6
Which lists all running processes, selects only the lines for firefox, then cuts off everything except for the process numbers. The only problem that I'm having is that most of the time there are two firefox-bin processes, and I get two different numbers on two different lines. How can I run kill -9 on one of these at a time?
to kill the first process insert
Code:
| head -n1
after your grep so that you have
Code:
ps -A | grep ...| head -n1 | cut ...
 
Old 10-16-2009, 04:02 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
You probably want to look into pidof.

Code:
pidof <whatever> | xargs kill -9 && <whatever else>
 
Old 10-16-2009, 06:24 PM   #5
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Does Slackware have the "killall" command? If so this might be another way to do it.
 
Old 10-16-2009, 07:38 PM   #6
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Original Poster
Rep: Reputation: 30
Yes, slackware does have the killall command; however when firefox hangs/freezes as it does due to the flash plugin, I need to use
Code:
kill -9
in order to kill it, kill or killall alone cannot do it, I need "-9" or the force parameter to successfully do it.

I think based upon what the others have posted though, that I should be able to pull it off. I'm not at my computer now, but I'm going to look into the xargs command, and and see what I can do with that.

Also, I can't believe that I forgot about the "head" command, silly me. :-p

Thanks everybody,
-Primux
 
Old 10-17-2009, 12:02 AM   #7
Elv13
Member
 
Registered: Apr 2006
Location: Montreal,Quebec
Distribution: Gentoo
Posts: 825

Rep: Reputation: 129Reputation: 129
killall -9 will work

but the right way or doing a line per line script is:
Code:
#/bin/bash
#^^It is important, this time to add this line or it may not work
IFS=`echo -en "\n\b"`
for LINE in `ps`; do
   echo My line is $LINE
done
 
Old 10-17-2009, 07:41 AM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
How complicated!

Code:
pkill -9 firefox-bin
pkill greps for the processes containing this name and kills them.

jlinkels
 
Old 10-17-2009, 10:35 PM   #9
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by rizhun View Post
You were nearly there...

Try this:

Code:
$ ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {}
To get it to start FireFox after killing it, add the following:

Code:
$ ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {} && /path/to/firefox/executable
See the man page for xargs for info:

Code:
$ man xargs
Happy hacking
Thank you very much for the idea. The only problem with this, which comes not from your contribution, but from my original code, is that if I use cut with the -6 option, and the process is only 5 digits long, I get
Code:
primux@slaxor:~$ ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {}
kill: can't find process "13043 "
Now if I change the code to "cut -c -5", it works fine. I would just change the code, but sometimes the process numbers do get up to six digits.

Should I set it up to try it with 6 first, then run the same string of commands again with -5? Or should I try to figure out how to make the script figure out how many digits the process number is, and then make the 5 or 6 arguments for cut be variables? Would this be possible? *head scratch*

I'm pretty sure it'd work if I just wrote it like this
Code:
ps -A | grep firefox-bin | cut -c -5 | xargs -i kill -9 {}
ps -A | grep firefox-bin | cut -c -6 | xargs -i kill -9 {}
But it's kind of brutish, and I really don't like to program in that style. I'd like to make something elegant that just works perfectly, but I'm not quite sure if it's possible.

It is kind of aggravating that when you run kill through xargs, that it recognizes the space as part of the process number. Oh! (I just got an idea while typing), is it possible to make xargs ignore that space? Or rather to ignore that space without affecting the way it reads the input from STDIN? I read the man page, and I know that you can change the way that it decides what counts as a separator, but I don't know if it would change the way it takes the input or executes kill.

/me starts reading more about bash scripting
Thanks everybody!
-Primux
 
Old 10-18-2009, 01:21 AM   #10
slackwarefan
Member
 
Registered: Oct 2003
Location: Florida
Distribution: Slackware
Posts: 273

Original Poster
Rep: Reputation: 30
Ok, I got it to work. If anyone else has this problem, here's the code I used . . .

Code:
#! /bin/bash

var=`ps -A | grep firefox-bin | cut -c -6`
var+=1
letter_seperator=" "
if $var | grep "$letter_seperator"
then
cutnum=5
else
cutnum=6
fi

ps -A | grep firefox-bin | cut -c -"$cutnum" | xargs -i kill -9 {}

/path/to/firefox &

exit 0
For some reason, killall does not work when firefox freezes because of the flash plugin.

I'm not sure about pkill, I wish I'd heard of/remembered that before I thought up all of this.

Anyways, thanks to everybody that gave me ideas. I hope this can help someone else. I mean, am I the only one to whom this happens? *shrug*

Thanks again everybody,
-Primux
 
Old 10-18-2009, 07:08 AM   #11
rizhun
Member
 
Registered: Jun 2005
Location: England
Distribution: Ubuntu, SLES, AIX
Posts: 268

Rep: Reputation: 47
You could also use 'awk' to get the first "field", then it doesn't matter how many digits are in the PID:

Code:
$ ps -ef | grep firefox-bin | awk '{printt $1}' | xargs -i kill -9 {}
Glad I could help.

 
Old 10-18-2009, 07:50 AM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Heloooo... did any read this? Or trying to do it difficult even if it can be done the easy way? Or is pkill a Debian speciality?


Quote:
Originally Posted by jlinkels View Post
How complicated!

Code:
pkill -9 firefox-bin
pkill greps for the processes containing this name and kills them.

jlinkels
 
Old 10-18-2009, 03:10 PM   #13
heiko
LQ Newbie
 
Registered: Oct 2009
Posts: 3

Rep: Reputation: 0
No, on my Slackware 13 there is pgrep as well as pkill. I think one could do the task with these commands conveniently.


Quote:
Originally Posted by jlinkels View Post
Heloooo... did any read this? Or trying to do it difficult even if it can be done the easy way? Or is pkill a Debian speciality?
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Align lines in shell script jeesun Linux - General 5 10-16-2009 12:29 AM
Looking for a shell script that prints a file only if it has more than 10 lines djfog Linux - Newbie 8 07-01-2008 12:23 PM
SHELL Script to insert lines after certain Intervals rahulruns Linux - General 9 02-26-2008 01:21 AM
Shell script to jump over lines horacioemilio Programming 8 12-02-2007 05:44 PM
insert lines in a function using shell script shyamdey Programming 1 08-30-2006 07:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:16 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
Open Source Consulting | Domain Registration