4

I have run sleep command in background as follow , How can I know the status of execution ..?

Command

sleep 2d ; find /home/disk1/ -exec touch {} \; &

How can I know that its executed for 1 day of sleep or is executing findcommand right now.

  • 1
    No, you've run sleep in the foreground. And when it completes find will run in the background. – PM 2Ring Jan 16 '15 at 13:02
  • if I run sleep in background would I have access to terminal? Cause I am able to access terminal right after executing the above command, and jobs state that its been executed. – JigarGandhi Jan 16 '15 at 14:14
  • With the above command you have type-ahead access to the terminal but anything you type won't get executed until the sleep is finished and the find starts running in the background. If you run sleep in the background you would have (almost) immediate access to the terminal. To run a set of commands in the background you can enclose the command in { and } with an & at the end of the line, as illustrated in Costa's answer. – PM 2Ring Jan 17 '15 at 6:22
  • Alternatively, you can enclose the commands in ( and ) instead of { and } (still with & at the end of the line), which runs the commands in a subshell. See Compound Commands in the bash man page for further details. – PM 2Ring Jan 17 '15 at 6:31
2
{ touch /tmp/sleep.flag; sleep 2d ; rm /tmp/sleep.flag; find /home/disk1/ -exec touch {} \; ; } &

Everything what you need just check /tmp/sleep.flag file existance

[ -f /tmp/sleep.flag ] && echo "Running sleep..."
  • you mean that after execution of sleep after 2days the file will be deleted. right! – JigarGandhi Jan 16 '15 at 14:15
  • @JigarGandhi: Yes. At the end of two days the temporary file /tmp/sleep.flag will be deleted and the find command will start running. The whole group of commands (everything enclosed by { and }) is run in the background so you get access to the terminal. – PM 2Ring Jan 17 '15 at 6:26
1

Try using ps to look for the sleep command:

{ sleep 20; find ...; } &
parent=$!
if ps --ppid $parent | grep sleep
then
    echo Sleep is running
fi

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.