How to track and later kill a process in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to track and later kill a process in a script
# 1  
Old 02-26-2010
How to track and later kill a process in a script

Hello,

I am trying to write a script that turns off the screensaver for a certain period of time, then come back on. I have had it up and running for a while, but then I decided to refactor it a bit for my family members that are less computer savvy.

I am starting a subshell for the "meat" of the off script

Code:
(sleep $STIME ; sson ;exit) &

I want to be able to find this sleep command and kill the command. This allows the finishing of the script calling my sson script and then exiting.

I could always do a "ps ux|grep sleep|awk '{print $2}'|xargs kill", but if I happen to any other script with a sleep running that is not associated with sleeping the screesaver, this will kill that one too.

I was hoping to use the $! ability of bash on the subshell and kill that, but is many of you probably already knew, killing the bash doesn't kill the sleep. As I'm typing this, I thought of this:

Code:
(
sleep $STIME &
PID=$!
if [ ! -f /tmp/sleep.pid ] ; then
    touch /tmp/sleep.pid
else
    echo $PID >> /tmp/sleep.pid
fi
wait $PID
sson
exit
) &

I do the whole touch and append thing because I would like to keep a log of the pids and kill any that might have been run (or kill all previous before) in case my ssoff script is invoked twice.

Plus, I just want to do an exercise for my own learning on PID tracking.

Is this the way it should be done, or is there a better way of coding it?

With thanks,
Narnie
# 2  
Old 02-26-2010
You can kill a process inside a script by using $! variable.
The use of $! variable is containing the process id of the last background process.

Here,is my simple script which tracks the background process in the script and kills it.

sleep 100 &
echo "Background Process ID:$!"
kill -9 $!
# 3  
Old 02-27-2010
Quote:
Originally Posted by vivekraj
You can kill a process inside a script by using $! variable.
The use of $! variable is containing the process id of the last background process.

Here,is my simple script which tracks the background process in the script and kills it.

sleep 100 &
echo "Background Process ID:$!"
kill -9 $!
If you'll notice, that is the variable that I assigned to PID. I was just wondering if there might be a better way to do this. Also, it will be another script that would be canceling the process, hence my logging of it. My sson (screen saver on) script will be modified to kill all "ssoff" processes in case my family has more than one running. Or, I might add it as a feature of the ssoff script (or both).

My script example worked, just wondering if there were a better way to script it.
# 4  
Old 02-27-2010
Consider the pstree command - you want the PPID for the process that is running your script name.
# 5  
Old 02-28-2010
Quote:
Originally Posted by jim mcnamara
Consider the pstree command - you want the PPID for the process that is running your script name.
Nice, never knew of that command.

Thanks.

Loving to learn,
Narnie

---------- Post updated at 10:49 PM ---------- Previous update was at 10:34 PM ----------

Quote:
Originally Posted by jim mcnamara
Consider the pstree command - you want the PPID for the process that is running your script name.
Cool, so if I wanted to use my original script, I could do this:

Code:
woodnt@toshiba-laptop ~ $ pstree -p 20855| sed  's/.*sleep(\([0-9][0-9]*\))/\1/'
20856

and just modify that for my script. That is very nice.

Thanks for the learning op

Narnie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script does not kill the java process

Hi We have script to stop our application, all our application use this standard script to stop however in one of our application this script does not work properly. It does not kill the process, thats why we always use the manual process to kill which is to kill the process id (kill -9... (1 Reply)
Discussion started by: kaibiganmi
1 Replies

2. Shell Programming and Scripting

Script to monitor a process and track status in a file

i have a scenario where i need a script that monitors a process "Monitor" based on process id... there can be any number of instances of this running... i start this across 4 servers in NFS. Now i need a file which has the process ids of the process that are currently in execution at any... (9 Replies)
Discussion started by: niteesh_!7
9 Replies

3. Shell Programming and Scripting

Cannot kill hacker process with my script

I want to kill a process of xterm that is run by hacker with my login name. So, I write a shell script to do my goal. I run 2 xterm and then I run my script on a first xterm. it should kill the process of a second xterm but it doesn't.Why? Here is my code : #!/bin/ksh myps=$(ps -f|grep... (7 Replies)
Discussion started by: thsecmaniac
7 Replies

4. Shell Programming and Scripting

Script to kill the specific process

Hi I have the process to kill regulary, but the PSID is dymatic change and not sure how to kill the specific process ID Check the tradekast_rvd is running , if such process, kill the els process id ps -e f |grep tradekast_rvd ps -ef |grep els then I kill els process id ... (2 Replies)
Discussion started by: linux_user
2 Replies

5. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: holocene
4 Replies

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

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

Track and kill the PIDS

I have a script that conducts some SSH calls and I would like to capture the child info so that I can do a sleep and then a cleanup to make sure they do not stay out there as ghosts. I was told I could do something like this... #!/bin/sh for m = job1, job2, job3 x=1... (4 Replies)
Discussion started by: LRoberts
4 Replies

9. Shell Programming and Scripting

Script to kill process...

hello Bros, I need to write some script that i can put it on crontab which checks for a process X if running. If the process X is ruuning then take the PID and kill it or display message that says process X is not running. I am using AIX 5.3 Thanks guys.:b: (2 Replies)
Discussion started by: malcomex999
2 Replies

10. Shell Programming and Scripting

Script to kill process

Hello guys, I have a process named monitoreo, with 'monitoreo start' my process start until i kill them, now i want to do 'monitoreo stop' to kill them. After 'monitoreo start' i have this process running: ps -af UID PID PPID C STIME TTY TIME CMD ati 10958 1495 ... (5 Replies)
Discussion started by: Lestat
5 Replies
Login or Register to Ask a Question