- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: using at command in script fails
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 09:05 PM
03-08-2005 09:05 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 09:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 09:17 PM
03-08-2005 09:17 PM
Re: using at command in script fails
at now +$TID minutes
live free or die
harry d brown jr
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 09:25 PM
03-08-2005 09:25 PM
Re: using at command in script fails
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 09:47 PM
03-08-2005 09:47 PM
Re: using at command in script fails
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 10:47 PM
03-08-2005 10:47 PM
Re: using at command in script fails
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 10:49 PM
03-08-2005 10:49 PM
Re: using at command in script fails
Doesn't he want to do the sleep 60 before the kill $i?
Mark Syder (like the drink but spelt different)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 10:51 PM
03-08-2005 10:51 PM
Re: using at command in script fails
It was the
echo "
that did the trick for me.
Cheers,
Lars C
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2005 11:27 PM
03-08-2005 11:27 PM
Re: using at command in script fails
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2005 12:47 AM
03-09-2005 12:47 AM
Re: using at command in script fails
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2005 01:02 AM
03-09-2005 01:02 AM
Re: using at command in script fails
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