Sponsored Content
Top Forums Shell Programming and Scripting How to track and later kill a process in a script Post 302398925 by Narnie on Friday 26th of February 2010 12:42:43 AM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

10. 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
XDG-SCREENSAVER(1)														XDG-SCREENSAVER(1)

NAME
xdg-screensaver - command line tool for controlling the screensaver SYNOPSIS
xdg-screensaver suspend WindowID xdg-screensaver resume WindowID xdg-screensaver {activate lock reset status} xdg-screensaver {--help --manual --version} DESCRIPTION
xdg-screensaver provides commands to control the screensaver. xdg-screensaver is for use inside a desktop session only. It is not recommended to use xdg-screensaver as root. COMMANDS
suspend WindowID Suspends the screensaver and monitor power management. WindowID must be the X Window ID of an existing window of the calling application. The window must remain in existance for the duration of the suspension. WindowID can be represented as either a decimal number or as a hexadecimal number consisting of the prefix 0x followed by one or more hexadecimal digits. The screensaver can be suspended in relation to multiple windows at the same time. In that case screensaver operation is only restored once the screensaver has been resumed in relation to each of the windows resume WindowID Resume the screensaver and monitor power management after being suspended. WindowID must be the same X Window ID that was passed to a previous call of xdg-screensaver suspend activate Turns the screensaver on immediately. This may result in the screen getting locked, depending on existing system policies. lock Lock the screen immediately. reset Turns the screensaver off immediately. If the screen was locked the user may be asked to authenticate first. status Prints enabled to stdout if the screensaver is enabled to turn on after a period of inactivity and prints disabled if the screensaver is not enabled. OPTIONS
--help Show command synopsis. --manual Show this manualpage. --version Show the xdg-utils version information. EXIT CODES
An exit code of 0 indicates success while a non-zero exit code indicates failure. The following failure codes can be returned: 1 Error in command line syntax. 3 A required tool could not be found. 4 The action failed. EXAMPLES
xdg-screensaver suspend 0x1c00007 Causes the screensaver to be disabled till xdg-screensaver resume 0x1c00007 is called. 0x1c00007 must be the X Window ID of an existing window. AUTHOR
Bryce Harrington. <bryce@osdl.org> xdg-utils 1.0 06/24/2007 XDG-SCREENSAVER(1)
All times are GMT -4. The time now is 06:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy