Solved

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

Posted on 2010-11-19
13
453 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
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 500 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS- KeepAlived notify script not working 23 83
Ubuntu Apache Webserver - File Permissions 5 42
linux 13 50
Quickest way to query Windows Event ID from a Linux Device 3 33
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

777 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