Solved

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

Posted on 2010-11-19
13
451 Views
Last Modified: 2012-08-13
Start a sleep process and kill it woulthout using any intermediate files
0
Comment
Question by:sobeservices2
  • 6
  • 6
13 Comments
 
LVL 4

Expert Comment

by:trythisone
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
I meen something like

sleep 100
sleep 100
then kill it
0
 
LVL 8

Expert Comment

by:LunarNRG
Comment Utility
"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
Comment Utility
#!/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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:sobeservices2
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
what do you mean by give some_condition a value?
0
 
LVL 8

Expert Comment

by:LunarNRG
Comment Utility
As specified in the most recent example code ...

some_condition=false

Open in new window

0
 

Author Comment

by:sobeservices2
Comment Utility
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
Comment Utility
Was good
0
 
LVL 8

Expert Comment

by:LunarNRG
Comment Utility
$! 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now