Script to start background process and then kill process


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Script to start background process and then kill process
# 1  
Old 06-08-2010
Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed.

The script successfully launches the script. But how do I check to see if the job exists before I kill it?

I know my problem is mostly failure to understand parameter expansion and conditionals.

Best Regards
Steve.

Code:
#!/bin/bash

operation="go"
while [ "$operation" != "quit" ]; do
    echo -n 'Enter Operation '
    read operation    

    case $operation in
        "start testing" )
##Start a process
            echo "starting testing"
            ./testing.scr &
                    ;;
        "stop testing" )
##Stop a process

#check to see if process exists. kill if exists, warn if it does not
            if [ "$(ps | grep testing.scr)" -ne "" ]; then
            kill $(ps | grep testing.scr | cut -d' ' -f 2)
                else
                    echo "job does not exist"
            fi            
                    ;;                    
                    
        * )
            echo "error keyed"
        ;;
    esac
done

# 2  
Old 06-09-2010
1. List all process, may be with "ps ax"

2. And exclude the current grep as

Code:
 
ps ax | grep testing.scr | grep -v grep

# 3  
Old 06-09-2010
you could try something like:

Code:
if [ `/bin/pidof -x testing.scr | wc -l` = 1 ]
then
    do your thing
fi

That will test if there is a process for testing.scr - you could test for no process with = 0 rather than = 1

I've also done this in a script:

Code:
declare -a pids
pids=`ps aguwx | grep run.sh | awk '{print $2}' | xargs`
for thisPid in "${pids[@]:0}"
do
	printf "killing $thisPid \n"
	kill -9 $thisPid
done;

This works when I know there are multiple run.sh processes (each run.sh always has an associated child process so that's why the awk is in there), so it saves the pid's and child pids to an array, then iterates through the array and kills them. It's probably not the most elegant of solutions but it does work.
# 4  
Old 06-10-2010
Ok. You all have given me some ideas. Thanks.
Steve.
# 5  
Old 06-10-2010
You can also use pgrep(1).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

2. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

3. Shell Programming and Scripting

grep the process id and kill all the filtered process

Hi I want to write a shell script which can find the process id's of all the process and kill them eg: ps ax | grep rv_ 3015 ? S 0:00 /home/vivek/Desktop/rv_server 3020 ? S 0:00 /home/vivek/Desktop/rv_gps 3022 ? S 0:00 /home/vivek/Desktop/rv_show ... (7 Replies)
Discussion started by: vivek_naragund
7 Replies

4. Linux

How to kill background process?

Hi I have one file one.sh. In one.sh file their are 5 perl script 1.pl, 2.pl, 3.pl,4.pl, 5.pl. I ran the one.sh file in background and now I want to kill the one.sh file after 5 min. but its not killing the processs. Please let me know how to kill the background process. (2 Replies)
Discussion started by: lakshmikant
2 Replies

5. Shell Programming and Scripting

Script to Kill a Process by Name...

Hello all... new to these forums and a bit of a newbie with linux aswell. I need to figure out how to write a shell script to kill a process by name as given to the script as an argument. I've got that part working OK, but i need to make sure that the script does not allow processes that are... (6 Replies)
Discussion started by: cannon1707
6 Replies

6. 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

7. Shell Programming and Scripting

How to kill background process

Hi, I am new to scripting and learning. please share your knowledge with me. I have a scenario, that i need to trace whether the background script is still running or not? if it was running i need to kill it. i am using the below scripts , but it is not working i dont know why :confused:.... (4 Replies)
Discussion started by: G.K.K
4 Replies

8. Shell Programming and Scripting

Script - How to automatically start another process when the previous process ends?

Hi all, I'm doing automation task for my team and I just started to learn unix scripting so please shed some light on how to do this: 1) I have 2 sets of datafiles - datafile A and B. These datafiles must be loaded subsequently and cannot be loaded concurrently. 2) So I loaded datafile A... (10 Replies)
Discussion started by: luna_soleil
10 Replies

9. Shell Programming and Scripting

how to start a process and make it sleep for 5 mins and then kill that process

how to start a process and make it sleep for 5 mins and then kill that process (6 Replies)
Discussion started by: shrao
6 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question