Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Start a sleep process and kill it woulthout using any intermediate files

Posted on 2010-11-19
13
Medium Priority
?
460 Views
Last Modified: 2012-08-13
Start a sleep process and kill it woulthout using any intermediate files
0
Comment
Question by:sobeservices2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 4

Expert Comment

by:trythisone
ID: 34175904
Not exactly sure what you are asking, but you can use a ping command to act as a sleep command by just pinging your localhost
0
 
LVL 8

Accepted Solution

by:
LunarNRG earned 2000 total points
ID: 34175915
Something like this perhaps? If not, please clarify your question.  

#!/bin/bash

sleep 300 &
sleep_pid=$!

# ... do something ...

kill -TERM $sleep_pid

Open in new window


Or maybe ...

#!/bin/bash

sleep 300 & 
sleep_pid=$!

if $some_condition; then 
  # wait for sleep to end
  until ! $(kill -0 $sleep_pid &> /dev/null); do
    sleep 1
  done
  # do_something
else
  # terminate sleep process
  kill -TERM $sleep_pid
  # do_something_else
fi 

Open in new window

0
 

Author Comment

by:sobeservices2
ID: 34175988
I meen something like

sleep 100
sleep 100
then kill it
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 8

Expert Comment

by:LunarNRG
ID: 34176229
"then kill it"

What do you mean by "it", exactly? The first sleep process, the second, the enclosing script?

You might want to give us more to work with, perhaps explain what problem you're attempting to solve, and what you've already tried.

0
 

Author Comment

by:sobeservices2
ID: 34184331
#!/bin/bash

sleep 300 & 
sleep_pid=$!

if $some_condition; then
  # wait for sleep to end
  until ! $(kill -0 $sleep_pid &> /dev/null); do
    sleep 1
  done
  # do_something
else
  # terminate sleep process
  kill -TERM $sleep_pid
  # do_something_else
fi



Did not work
0
 
LVL 8

Expert Comment

by:LunarNRG
ID: 34185055
Going to need more to go on than "Did not work", unfortunately. What did not work? Did you receive an error message?

IMHO, the following answers your original question ...

#!/bin/bash

sleep 300 &
sleep_pid=$!

# ... do something ...

kill -TERM $sleep_pid

Open in new window


... as it starts "a sleep process" and kills it without "using any intermediate files". If you disagree, please clarify your question.
0
 

Author Comment

by:sobeservices2
ID: 34185070
Ok what I did was cut an past your code into a blank script

so  you code was the only thing in there
save it as test1

then I ran it and got this nothing
even chmod +x test1
I had to do a control break
 to stop it
no error message was given
0
 
LVL 8

Assisted Solution

by:LunarNRG
LunarNRG earned 2000 total points
ID: 34185258
OK, got it.

The second example, was just that, an example -- and one possible (if not recommended or common) reason for wanting to manipulate a sleep process in a script. It assumes you will assign a value to the variable 'some_condition', based on the needs of your particular script. If the first example works for your purposes, it is safe to ignore the second.

However, if you want to play with the second example, give some_condition a value -- and add echo statements to expose the flow of the program. As in,

#!/bin/bash

sleep 300 &
sleep_pid=$!


some_condition=false
# uncomment the following to wait for the sleep process to end
#some_condition=true

if $some_condition; then
  echo "Waiting for sleep to end normally."
  until ! $(kill -0 $sleep_pid &> /dev/null); do
    sleep 1
  done
  # do_something
else
  echo "Terminating the sleep process."
  kill -TERM $sleep_pid
  # do_something_else
fi

Open in new window



HTH
0
 

Author Comment

by:sobeservices2
ID: 34185267
what do you mean by give some_condition a value?
0
 
LVL 8

Expert Comment

by:LunarNRG
ID: 34185276
As specified in the most recent example code ...

some_condition=false

Open in new window

0
 

Author Comment

by:sobeservices2
ID: 34188431
Ok I did you're first one and that worked for me.

Plane and simple

The other just got a little to harry and but I tried.

Better just keep it simple for now

sleep 300 &
sleep_pid=$!

thanks

Oh  what's the $! mean?
0
 

Author Closing Comment

by:sobeservices2
ID: 34188452
Was good
0
 
LVL 8

Expert Comment

by:LunarNRG
ID: 34188570
$! is the built-in bash variable representing the pid of the last process run in the background.

You'll find more information on this topic here:
  http://tldp.org/LDP/abs/html/internalvariables.html

The intro tutorial is also very good:
  http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question