Solved

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

Posted on 2010-11-19
13
457 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 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
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.

688 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