1

I have the following (redacted) script:

#!/bin/bash

set -x

if ps -lf | grep cata |  perl -ane '($h,$m,$s) = split /:/,$F +[13]; kill 9, $F[3] if ($h > 1); then
   ...    
else
   ...
fi
set +x

When running this script I'm getting this error:

/bin/kill.sh: line 8: unexpected EOF while looking for matching `''
/bin/kill.sh: line 20: syntax error: unexpected end of file

What is wrong?

  • try: ... perl -ane '($h,$m,$s) = split /:/,$F +[13]; kill 9, $F[3] if ($h > 1)'; .... I added an "'" before the ";". – Alex Stragies Oct 18 '15 at 15:38
  • BY the way, $F+[13] is really not what you want. Presumably, you meant $F[13]. – terdon Oct 18 '15 at 17:05
  • I think it's kind of ridiculous that only one person has [nearly] simplified their answer and/or comment to "you're missing a terminating single-quote ('), which is obviously causing the error in question." – rubynorails Oct 18 '15 at 20:37
  • @rubynorails that is just one of many, many errors. The Perl script is also completely wrong. Fixing the closing quote will not correct the problem. – terdon Oct 20 '15 at 11:50
4

You haven't closed the perl command. It needs to be perl 'foo' and you have perl 'foo instead. Just change your script to:

if ps -lf | grep cata |  
    perl -ane '($h,$m,$s) = split /:/,$F +[13]; kill 9, $F[3] if ($h > 1)'; then

That will take care of the syntax error. However, your script will still not work as you expect. First of all, when you run ps | grep foo, you will always get at least one result: the grep process itself:

$  ps -lf | grep cata
0 S terdon    6400  4889  0  80   0 -  3183 -      19:03 pts/0    00:00:00 grep --color cata

Second, you aren't telling your perl script to exit with a different status depending on whether it matched. Therefore, your if condition will always be true since the perl script will always run, irrespective of whether it is doing anything useful.


More to the point, you are reinventing the wheel. If I understand correctly, you want to kill the cata process if it has been running for more than an hour. At least I think that's what you expect your Perl script to do (it doesn't, but never mind). If so, all you need is:

kill -9 $(ps -o pid=,etimes= -p $(pgrep cata) | awk '$2>3600{print $1}')

That might need some explaining. The ps command can be told to list only specific PIDs with the -p option and to print only specific fields of output with the -o. Here, I am telling it to print the PID and the elapsed time in seconds. The pgrep command prints any PIDs mathing the pattern given. So, ps -p $(pgrep cata) will run ps on any PID whose process matches cata.

Since the -o flag is telling ps to only print the PID and the elapsed time, the awk will check whether the elapsed time (the second field, $2) is greater than an hour (3600 seconds) and, if so, it will print the first field, the PID. So, the result of the ps ... | awk will be the list of PID of cata processes that have been running for more than an hour. This list is then given to the kill -9 command.

  • 2
    That's also reinventing the pkill wheel... – Wouter Verhelst Oct 18 '15 at 17:46
  • @WouterVerhelst how do you get pkill to only kill processes that have been running for more than an hour? – terdon Oct 18 '15 at 18:15
  • this results in usage error from kill if there are no cata processes that have been running for more than an hour. use | xargs -r kill -9 instead. also OP should supply full name of process so it can be found with pc -C. – cas Oct 18 '15 at 21:09
  • that's a pretty neat trick. dont forget etimes does leading colon divided components for day old processes – mikeserv Dec 5 '15 at 4:50
3

Add an ' before ; you have actually forgotten it:

if ps -lf | grep cata | perl -ane '($h,$m,$s) = split /:/,$F +[13]; kill 9, $F[3] if ($h > 1)'; then
  • I didn't want @terdon 2 modify my answer but give me a suggestion comment! – EKons Oct 20 '15 at 11:40
  • Huh? I just fixed the formatting and removed the excessive exclamation marks. Editing answers is very much a part of how this site works. – terdon Oct 20 '15 at 11:47
  • @terdon From a meme??? – EKons Oct 24 '15 at 19:48
  • The meme was an attempt at humor. The main point is that this site tries, even succeeds sometimes, to have clear, grammatically and syntactically correct questions and answers. So, excessive punctuation and textspeak should be avoided. I think we're having trouble communicating. It might be easier to have a chat about it. Αν είναι θέμα γλώσας, τα λέμε και στα ελληνικά. Κάποια παρεξήγηση παίζει. – terdon Oct 24 '15 at 21:52
  • @terdon hahahahahaha εντάξει έβαλα άνω-κάτω τελεία. If it's a matter of language, I put a colon instead of an !. – EKons Oct 31 '15 at 12:26

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.