| Author |
Message |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Fri 01 Jan 2010, 12:34 Post subject:
Ok in rxvt but not in script Subject description: Why the inconsistancy? |
|
This works perfectly in rxvt but fails in a script.....it seems to get mixed up over spaces.
| Code: | | ps | grep 'wget -c' | grep -v grep | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status |
line from ps...
9613 root 1404 S wget -c -o /tmp/tart.test -O /mnt/hda2/test.iso http://distro.ibiblio.org/pub/linux/distributions/puppy
expected result
test.iso
gives full line...breaking down the command truncation happens in the wrong places.
yet...
| Code: | | ps | grep 'cclive -c' | grep -v grep | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status |
working on
9684 root 1876 S cclive -c -f mp4 -O /mnt/hda2/test.mp4 http://www.youtube.com/watch?v=Sv5iEK-IEzw
gives expected
test.mp4
confused
mike
ok ..the eventual workaround was to turn around the wget commant to give
11061 root 1400 S wget -c -O /mnt/hda2/test.iso -o /tmp/tart.fhsgjsgfjs http://distro.ibiblio.org/pub/linux/distributions/pup
but I would like to know why there was a difference between the command line and script.
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: Fri 01 Jan 2010, 13:18 Post subject:
|
|
The output from ps, as with many other commands is formatted with TABs, so you need to grep for the line you need, then echo that and pipe through sed. The echo'ing converts tabs to spaces, eliminates duplicates and eliminates any leading or trailing spaces:
STATUS=$(ps | grep 'wget -c' | grep -v grep)
echo $STATUS | sed 's%.*/\(.*\)http.*%\1%' >> /tmp/tart.status
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Fri 01 Jan 2010, 17:17 Post subject:
|
|
Ah ok..thanks for the quick answer.
I tend to build up the command line to get the desired result then copy over to geany ...so basically I need to split apart to get consistant results .
I guess I've been running on luck rather than design
Ok I will implement (I don't like workarounds) and go forth
regards
mike
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Fri 01 Jan 2010, 17:30 Post subject:
|
|
Nope same behaviour...I will try on something else ...seems like a bash problem
mike
|
|
Back to top
|
|
 |
Patriot

Joined: 15 Jan 2009 Posts: 734
|
Posted: Mon 04 Jan 2010, 16:01 Post subject:
|
|
Hmmm .....
mikeb,
Could it be that the ps shell script is causing this for you? ... I've always try to go direct to ps-FULL or busybox ps or when I'm sure of it, specify options, ie: ps ax ...
Rgds
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Mon 04 Jan 2010, 16:55 Post subject:
|
|
A possibility indeed...well I've got a meeting coming up Wednesday so I have to prepare including persuading some accounting software to install to a local web server...oh joy...so I will pick this one up in a while.
When I broke it down I would get results like
test.i or test.iso h at the end.
Something unpleasent from ps could be a possibility.
regards and a fun New Year
mike
|
|
Back to top
|
|
 |
MinHundHettePerro

Joined: 05 Feb 2009 Posts: 831 Location: SE
|
Posted: Mon 04 Jan 2010, 17:06 Post subject:
|
|
Your sed-search-word "http" in most cases (with the reasonably short log- and output- file names you gave above) ends up around the truncation point of grep's. Try so as not to exclude your search-word. Doesn't explain your experienced difference between cli and script, though, unless there was an extra space or something ...
EDIT: Seems like (at least busybox 1.6.1) ps, without the wide-switch, only pipes 80 char's ...
fwiw /
MHHP
_________________ Celeron 2.8 GHz, 1 GiB RAM, i82845 graphics, many partitions, Pupmode 12 (13)
Mostly running Slacko & 214X
Nämen, vaf.... ln -s /dev/null MHHP
Last edited by MinHundHettePerro on Mon 04 Jan 2010, 18:22; edited 1 time in total
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Mon 04 Jan 2010, 18:07 Post subject:
|
|
Thanks for the input....I seem to be running on the busybox ps without any options....I will check behaviour with the full version.
Indeed I might take a different approach to tracking created processes anyway...
regards
mike
|
|
Back to top
|
|
 |
Shel
Joined: 11 Apr 2009 Posts: 102 Location: Seattle, WA, USA, or Southern France
|
Posted: Tue 05 Jan 2010, 07:02 Post subject:
|
|
IIRC, the IRIX version of Unix includes a program called pgrep which reports whether a program is currently running. I stole that idea and implemented it as a shell script in Solaris, and it proved very handy. I'd give you the source, but all my Solaris machines are gone, and the tape archives of their contents are 8,000 miles away.
In general, though, it parsed the output of ps, as you're doing, and terminated with the appropriate exit code (and returned the job number[s] on stdout) if it found the program requested. As a stand-alone program, it could then be called by other programs to be sure I didn't run more than one copy of something, etc.
-Shel
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Tue 05 Jan 2010, 10:38 Post subject:
|
|
| Quote: | | In general, though, it parsed the output of ps, as you're doing, and terminated with the appropriate exit code (and returned the job number[s] on stdout) if it found the program requested. As a stand-alone program, it could then be called by other programs to be sure I didn't run more than one copy of something, etc. |
ooo that sounds better and it does seem it has been ported to linux....I do find this all a bit clumsy.....tracking processes when gtkdialog is used is a challenge and using text files is just plain messy...why have variables if everything has to be stored on the hard drive....ooo ranting again
mike
|
|
Back to top
|
|
 |
Shel
Joined: 11 Apr 2009 Posts: 102 Location: Seattle, WA, USA, or Southern France
|
Posted: Wed 06 Jan 2010, 01:48 Post subject:
|
|
| mikeb wrote: | | I do find this all a bit clumsy..... |
The classical Unix programming philosophy is a bit different from what you might find in other systems; the emphasis is on small programs that do one thing well, and pass their output along is a way that makes them easy to use in various combinations. Typically, you then use all these little programs as "commands" in larger programs, which become "commands" in still larger programs, etc.
Once you have your "pgrep" program built, either as your own script or something ported from another system, you have it, and it becomes another building block. It's Lego for grownups.
-Shel
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Wed 06 Jan 2010, 08:40 Post subject:
|
|
| Quote: | | It's Lego for grownups. |
now that sounds more appealing.
I quite like using bash it was just this particular function that was a bit thorny.
Thanks for the input
mike
|
|
Back to top
|
|
 |
Shel
Joined: 11 Apr 2009 Posts: 102 Location: Seattle, WA, USA, or Southern France
|
Posted: Wed 06 Jan 2010, 09:05 Post subject:
|
|
I decided to implement my own pgrep, and found that various ps commands act differently from the command line than from a script. It's a real WTF, but I think that's what started this whole thread!
In any case, here's what I'm using ...
| Code: |
ps -e | grep " ${1}$" | grep -v ' grep ' | grep -v '<defunct>' | awk '{print $1}'
|
... all one one line.
I'm sure it could be done more elegantly, but this works, is reasonably fast, and finds the exact match.
The first grep finds the word given as the argument to the script, with a space in front and a newline after; the other two greps just get rid of stuff you don't want, while the awk prints the job number to stdout.
-Shel
|
|
Back to top
|
|
 |
mikeb

Joined: 23 Nov 2006 Posts: 4378
|
Posted: Wed 06 Jan 2010, 13:50 Post subject:
|
|
| Quote: | | and found that various ps commands act differently from the command line than from a script. |
so I was not going barmy then...oh too late
Well I'm free to play again with this cheers
mike
|
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 1757
|
Posted: Wed 06 Jan 2010, 14:49 Post subject:
|
|
ps -e |grep "${1}$" | egrep -v '( grep |<defunct>)' | awk '{print $1}'
|
|
Back to top
|
|
 |
|