Operating System - HP-UX
1833756 Members
2594 Online
110063 Solutions
New Discussion

using at command in script fails

 
SOLVED
Go to solution

using at command in script fails

Hi!

I want to kill some procesess using the at command in a script. The at command sort of works, but the kill command does not. I would very much like a comment on this problem.

I have these processes that I want to kill one each minute:

ps -ef | grep APP | grep -v grep
demo 27796 1 27 10:03:54 pts/ta 13:46 /home/root/hogs/APP1/APP1_CPU 25
demo 27794 1 24 10:03:51 pts/ta 13:47 /home/root/hogs/APP1/APP1_CPU 25
demo 27799 1 27 10:03:57 pts/ta 13:45 /home/root/hogs/APP1/APP1_CPU 25

I have made this script;
TID=1
PID=$(ps -ef | grep APP | grep -v grep | awk '{ print $2 }')
for i in ${PID[@]}
do
at now +$TID minutes echo "PID = $i"
#/usr/bin/kill $i
((TID+=1))
done


The script creates the at jobs (at -l)


If I put a set -x in the script, I get this output. It looks like the kill command is not parsed the way I want, but if I do a echo of the whole line it appears riht. Also, if I type it at the shell prompt it works fine.

$ ./kill_APP.sh
+ TID=1
+ + ps -ef
+ grep APP
+ awk { print $2 }
+ grep -v grep
PID=27796
27794
27799
+ at now +1 minutes 27796
+ 0< /usr/bin/kill
job 1110362861.a at Wed Mar 9 11:07:41 2005
+ echo PID = 27796
PID = 27796
+ (( TID+=1 ))
+ at now +2 minutes 27794
+ 0< /usr/bin/kill
job 1110362921.a at Wed Mar 9 11:08:41 2005
+ echo PID = 27794
PID = 27794
+ (( TID+=1 ))
+ at now +3 minutes 27799
+ 0< /usr/bin/kill
job 1110362981.a at Wed Mar 9 11:09:41 2005
+ echo PID = 27799
PID = 27799
+ (( TID+=1 ))


The error message I get in my mail referes to a line that I don't have;

Date: Wed, 9 Mar 2005 11:09:41 +0100 (MET)
Subject: at

sh[19]: Syntax error at line 19 : `(' is not expected.


Cheers,
Lars C Nilsen
10 REPLIES 10
Victor Fridyev
Honored Contributor
Solution

Re: using at command in script fails

Hi,

Use the following:
echo "kill $PID" | at now + 3 minutes

HTH
Entities are not to be multiplied beyond necessity - RTFM
harry d brown jr
Honored Contributor

Re: using at command in script fails

what does the "<" do on this line?
at now +$TID minutes
live free or die
harry d brown jr
Live Free or Die
Peter Godron
Honored Contributor

Re: using at command in script fails

Lars,
I suspect the command terminator for the kill is CNTL D as you don't specify an alternate input stream.
If you want to use the < for input redirect, you would have to put the kill command into a file and call at with -f .

Also ensure your environment for the at command to run is correctly set up!

Check line 19 in your /etc/profile etc..

Regards

Re: using at command in script fails

other posibility with sleep

TID=1
PID=$(ps -ef | grep APP | grep -v grep | awk '{ print $2 }')
for i in ${PID[@]}
do
sleep $TID
/usr/bin/kill $i
echo "PID = $i"
#/usr/bin/kill $i
((TID+=1))
done
RolandH
Honored Contributor

Re: using at command in script fails

Do this it is the better way to do this:

For example your script is named kill_pid.sh and is located in /usr/local/bin

#/usr/bin/ksh
PID=$(ps -ef | grep APP | grep -v grep | awk '{ print $2 }')
for i in `echo ${PID[@]}|tr " " "\n"
do
/usr/bin/kill $i
sleep 60
done


Regards Roland
Sometimes you lose and sometimes the others win
MarkSyder
Honored Contributor

Re: using at command in script fails

Roland,

Doesn't he want to do the sleep 60 before the kill $i?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing

Re: using at command in script fails

Thank you all!

It was the

echo "" | at now +1 minutes

that did the trick for me.

Cheers,
Lars C
RolandH
Honored Contributor

Re: using at command in script fails

@ Mark

Does it any matter if it is before or after the kill command? NO

He just said that he want wait 1 minute before the next kill command. So he can place the sleep before and after the kill command in the for loop. The only different is if he set the sleep before the kill the loop ist waiting 1 minute before the kill will be done and if you place the sleep after the kill command it will wait 1 minute before the next loop is started.

Roland
Sometimes you lose and sometimes the others win
Bill Hassell
Honored Contributor

Re: using at command in script fails

Just a note about using grep with ps. As a general rule, using grep to find a process by name is very unstable. This is because grep cannot match a specific part of the ps line (such as the name of the process). However, ps does offer it's own version of process name matching (-C) and is always much more accurate than grep. Using the UNIX95 variable temporarily, you can also create your own version of ps output with -o. For your script, you could change the ps line to read:

PID=$(UNIX95= ps -C APP1_CPU -o pid | grep -v PID)

The -C option is an EXACT match to the basename of the process. In your example, APP1 is found in the parent directory as well as the process. -C matches only the process name and will not match a partial string (APP1 will not match APP1_CPU). There is no option in ps to remove the header line so grep -v PID is needed. But the only output from ps for each matching process will be a PID (from the -o option), this works reliably. And having just the pids from ps means awk isn't needed. Note also that -C allows for multiple process names so if you have something like APP1_CPU1 APP1_CPU2 APP1_CPU3, then use: -C APP1_CPU1,APP1_CPU2,APP1_CPU3

Also, to make the PID variable into an array, the syntax should be:

set -A PID $(UNIX95= ps -C APP1_CPU -o pid | grep -v PID)

Now array references such as ${PID[2]} will work as expected. But an array isn't needed since PID as a simple variable will have the list of pid numbers ready to use in the for/done loop. The reason your script sort of works is that ${PID[@]} is equivalent to $PID in the POSIX shell (or ksh or bash..) So your script can be simplified to:

TID=1
for MYPID in $(UNIX95= ps -C APP1_CPU -o pid | grep -v PID)
do
echo "at now + $TID minutes /usr/bin/kill $MYPID"
let TID=TID+1
done

I use echo on the "at now..." line to debug the script quickly. Remove echo and the quotes when it's ready to run.


Bill Hassell, sysadmin
MarkSyder
Honored Contributor

Re: using at command in script fails

Roland,

I don't know whether or not it makes a difference, but in his original script he had the sleep command before the kill. On the basis that there may have been a good reason for this I thought it best not to suggest reversing these.

Mark
The triumph of evil requires only that good men do nothing