Shell Script to end process help


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell Script to end process help
# 1  
Old 04-04-2009
Shell Script to end process help

I'm trying to write shell script that when invoke, has 2 arguements, or 1.
If there's one arguement, then that's the process name. If there's 2, then one is the timeout.
USAGE: Assign1.sh <process> <timeout>
If no timeout was set, then the default timeout is 15 minutes.

So after it read the process and time out, then it check how long the process has been running, then check with the current time to see if it's been 15 minutes. If it has, then it kill the process, if it hasn't, then it exit.

Here's my script:

Code:
#!/bin/sh
#
#
if test $# -eq 2; then
    timeout=$2
elif test $# -eq 1; then
    timeout=15
else
echo "\tUsage: $0 <process> <timeout>"
fi
echo $timeout
USERNAME=`who am i | cut -f1 -d" "`
STIME=`ps -u $USERNAME -f | grep $1 | awk -F" " '{print$5}'`
STIME=`echo $STIME | awk -F" " '{print $2}'`
STIME=`$STIME | awk -F: '{print $1*60+$2}'`
PID=`ps -u $USERNAME -f | grep $1 | awk -F" " '{print$2}'`
PID=`echo $PID | awk -F" " '{print $2}'`
current_time=`date | awk -F" " '{print $4}'`
current_time=`$current_time | awk -F: '{print $1*60+$2}'`
timediff=`$current_time-$STIME`
if test $timediff -lt $timeout; then
        echo "$1 has not ran for $timeout yet."
else
echo "Kill $PID"

No idea why it's not working! :-(
Any help would be greatly appreciated
# 2  
Old 04-04-2009
try running it by sh -x scriptname
post the error if you get one
# 3  
Old 04-04-2009
Quote:
Originally Posted by NoobieBoobie
I'm trying to write shell script that when invoke, has 2 arguements, or 1.
If there's one arguement, then that's the process name. If there's 2, then one is the timeout.
USAGE: Assign1.sh <process> <timeout>
If no timeout was set, then the default timeout is 15 minutes.

So after it read the process and time out, then it check how long the process has been running, then check with the current time to see if it's been 15 minutes. If it has, then it kill the process, if it hasn't, then it exit.

Here's my script:

Code:
#!/bin/sh
#
#
if test $# -eq 2; then
    timeout=$2
elif test $# -eq 1; then
    timeout=15
else
echo "\tUsage: $0 <process> <timeout>"
fi
echo $timeout
USERNAME=`who am i | cut -f1 -d" "`
STIME=`ps -u $USERNAME -f | grep $1 | awk -F" " '{print$5}'`
STIME=`echo $STIME | awk -F" " '{print $2}'`
STIME=`$STIME | awk -F: '{print $1*60+$2}'`
PID=`ps -u $USERNAME -f | grep $1 | awk -F" " '{print$2}'`
PID=`echo $PID | awk -F" " '{print $2}'`
current_time=`date | awk -F" " '{print $4}'`
current_time=`$current_time | awk -F: '{print $1*60+$2}'`
timediff=`$current_time-$STIME`
if test $timediff -lt $timeout; then
        echo "$1 has not ran for $timeout yet."
else
echo "Kill $PID"

No idea why it's not working! :-(
Any help would be greatly appreciated


STIME=`echo $STIME | awk -F" " '{print $2}'` # what is the purpose of this??
STIME=`$STIME | awk -F: '{print $1*60+$2}'` # you have to put as echo $STIME before piping it to awk..
# 4  
Old 04-04-2009
I fixed the echo $STIME thing.
The second one was to pick the second time. I noticed sometime it output two or three times... like there was two or three process of the same name running. I chose the second one because it always seem to have a lower time, meaning it's been running the longest.

Here's what I get when I run "sh -x assign1.sh bash 20"
Code:
 sh -x assign1.sh bash 20
+ test 2 -eq 2
timeout=20
+ echo 20
20
+ who am i
+ cut -f1 -d
USERNAME=std11
+ ps -u std11 -f
+ grep bash
+ awk -F  {print$5}
STIME=14:37:31
14:44:55
14:44:55
+ echo 14:37:31 14:44:55 14:44:55
+ awk -F  {print $2}
STIME=14:44:55
+ echo 14:44:55
+ awk -F: {print $1*60+$2}
STIME=884
+ ps -u std11 -f
+ grep bash
+ awk -F  {print$2}
PID=6304
6438
6428
+ echo 6304 6438 6428
+ awk -F  {print $2}
PID=6438
+ date
+ awk -F  {print $4}
current_time=14:44:54
+ 14:44:54
assign1.sh: 14:44:54: not found
+ awk -F: {print $1*60+$2}
current_time=
+ -884
assign1.sh: -884: not found
timediff=
assign1.sh: syntax error at line 26: `end of file' unexpected

It looks like my current_time became negative somehow???
# 5  
Old 04-04-2009
I lost the plot on what you are attempting to do.

Quote:
assign1.sh: 14:44:54: not found
Explanation of the error message: Your script is attempting to execute a command called 14:44.54 between the backticks here.

Quote:
current_time=`$current_time | awk -F: '{print $1*60+$2}'`
Without examining the syntax too closely you probably meant to get the contents of $current_time onto the pipe rather than execute $current_time :

Code:
current_time=`echo $current_time | awk -F: '{print $1*60+$2}'`


Quote:
STIME=`ps -u $USERNAME -f | grep $1 | awk -F" " '{print$5}'`
Beware of fuzzy matching with grep. In this line the parameter $1 could match any like number in the "ps -fu $USERNAME" output. For example the parent PID of a subprocess or say some part of the cpu usage. In this form of grep it would find "12" in "61234" or in "12:46".
Tip: "ps -fp${process_id}" would give you the status of that one process.


Sorry to be unhelpful but I believe that this script design is fundamentally flawed because "ps" changes the date format as processes get older and this changes the positional parameters presented to awk.
Login as root have a look at "ps -ef" on a system which has been up for more than 24 hours.

Last edited by methyl; 04-04-2009 at 10:09 PM.. Reason: tags displaced
# 6  
Old 04-06-2009
I got it to work!
Thanks for the help everything!

the "sh -x" REALLY HELPS, it's like a debug almost? run one line at a time and shows me the output and everything. Very neat to know.

I realized that the STIME and CURRENT_TIME were both saved as strings, so they cant be compute... when they do, the output was null, so it never worked.

I changed them to int with typeset -i and it worked. Woo!
Probably not very good coding... but it works... I'll figure out a different approach later.
# 7  
Old 04-06-2009
Quote:
Originally Posted by NoobieBoobie
I got it to work!
Thanks for the help everything!

the "sh -x" REALLY HELPS, it's like a debug almost? run one line at a time and shows me the output and everything. Very neat to know.

I realized that the STIME and CURRENT_TIME were both saved as strings, so they cant be compute... when they do, the output was null, so it never worked.

I changed them to int with typeset -i and it worked. Woo!
Probably not very good coding... but it works... I'll figure out a different approach later.
good you understand the imp of "sh -x"
way to go buddy!!!
regards,
vidya
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Spooling File to My Desktop From Back-end using shell script

Hello all, I am trying to spool a SQL query output file from back-end to desktop in a certain path but it didn't work,the query writes the output in a file in the same directory at the back-end. note : i use the same query in sql developper and file was created in the desired path fine code... (1 Reply)
Discussion started by: bmaksoud
1 Replies

2. Shell Programming and Scripting

Shell script to check line end not ending with comma

I have several line in a text file. for example I like apple; I like apple I like orange; Output: I like apple I try to useif grep -q "!\;$"; then (Not work) Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (1 Reply)
Discussion started by: cmdcmd
1 Replies

3. Shell Programming and Scripting

How does a shell script recognize the end of a line?

Hi friends , I want to know how does a shell script recognize the end of a line? . i have hunddres of proccedure to test where i want to ingnore the comments which starts with "--" .. it can start from the middle of the lines also. for example:: select * from table1; -- getting... (5 Replies)
Discussion started by: neelmani
5 Replies

4. Shell Programming and Scripting

Shell script menu for end user - difficult challenge 2

Hello, I need to make shell script menu for my end users. There is like 100 scripts in system, and they need to run that scripts true one main script with user friendly menu. Example, when user will run main menu script, it will get something like this on his screen:... (1 Reply)
Discussion started by: waso
1 Replies

5. Shell Programming and Scripting

Shell script menu for end user - difficult challenge

Hello, I need to make shell script menu for my end users. There is like 100 scripts in system, and they need to run that scripts true one main script with user friendly menu. Example, when user will run main menu script, it will get something like this on his screen:... (3 Replies)
Discussion started by: waso
3 Replies

6. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

7. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

8. Shell Programming and Scripting

Help with shell script: moving end of line character

Hello. I have a file (old.txt) that I need to copy into another file (new.txt). Each line on old.txt ends with CR/LF but the position of CR/LF varies from one record to another. I need to copy each line of record to new.txt and move CR/LF in pos 165. Can I use awk to achieve this? How?... (8 Replies)
Discussion started by: udelalv
8 Replies

9. Shell Programming and Scripting

How to add notes at the end of shell script

i was wondering how to add a notes section for the user to enter in a shell script, as my shell script is a menu based on configurations of hardware and software help is appreciated (4 Replies)
Discussion started by: zuberk
4 Replies

10. Shell Programming and Scripting

end of file problem with shell script

My shell script was working ok with K shell. But all of sudden, it started acting crazy. it keeps saying this: syntax error at line 70 : `"' unmatched I don't even have 70 lines in my file or this : syntax error at line 69: `end of file' unexpected Can this file got... (5 Replies)
Discussion started by: whatisthis
5 Replies
Login or Register to Ask a Question