1

Shell script runs two functions as child processes (via &).
Both of them went to sleep (via sleep command).

Is it possible to wake them?

I have their pids.

P.s.

I don't want to run sleep with & and wait

1

A rather barbaric way to do this would be to kill the sleep process (not the whole subshell in which your script/function runs). Consider the following shell script:

#!/bin/bash
sleep 20
echo "Done!"

or, with a function:

#!/bin/bash

function gotosleep()
{
    sleep 20
    echo "Done!"
}

gotosleep &
sleep 60 # Not really necessary, keeps the script in foreground.

Then, find the sleep process:

$ ps -ef | grep sleep
you    PID    PGID  ... sleep 20

And kill it:

$ kill PID

You script will then output (at least in bash):

./script.sh: line 2: PID Terminated    sleep 20
Done!

Since you don't want to wait properly, you'll have to do with Bash's little message. If you change your mind, have a look at this question:

sleep 20 &
if wait $! 2>/dev/null; then
    # Keep working
    continue
else
    echo "Done!"
    return # from function
fi
  • I don't like the idead of sleeping in a new subprocees ( at least not explicitly) – yuris May 8 '16 at 14:42
  • @yuris That's not what I'm doing here. I believe my sleep process runs in the same "shell" the function does (if you use a function). The only time I used & was to send the function to background, which is exactly what you did. – John WH Smith May 8 '16 at 14:44
  • I see, the issue will be greping the correct pid... – yuris May 8 '16 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.