12

What I do is:

sleep 5 

and immediately CTRL-Z so when I open jobs I see:

[1]+  Stopped                 sleep 5

next when I do fg %1 sleep process is no more running, it's done so that means it was running those 5 ( maybe 4? ) seconds while it was Stopped. Why?

2
  • 4
    Huh. I'm guessing sleep works by noting the time it was started, then polling the current time and checking if enough time has passed for it to exit. Although you stopped the process, you didn't stop the clock, so as soon as you bring it back to the foreground, it checks the time, realizes enough time has passed and exits. I am only guessing though.
    – terdon
    Commented Jul 18, 2021 at 21:28
  • 1
    For the Linux implementation what terdon wrote is handled directly by restart_syscall(2) without even involving specific code in the sleep process.
    – A.B
    Commented Jul 18, 2021 at 21:46

3 Answers 3

5

The utility calls xnanosleep() which in its turn calls the linux kernel nanosleep() system call. It works regardless of an application running/stopped state but when the application is stopped it cannot exit which means it does that when you unpause it.

https://linux.die.net/man/2/nanosleep

As for Ctrl + Z it sends a keyboard stop signal which can be intercepted by an application but in this case it's not done as the nanosleep() again works regardless.

/usr/include/bits/signum-arch.h

#define SIGSTOP         19      /* Stop, unblockable.  */
#define SIGTSTP         20      /* Keyboard stop.  */
1
  • 1. there is no SIGSTOP involved when you press Ctrl-Z. 2. the sleep command doesn't mind the time it was launched, it just calls nanosleep(), which is using the realtime clock.
    – user313992
    Commented Jul 19, 2021 at 9:24
1

Why sleep command process is still running in the background while stopped with CTRL-Z?

It doesn't. It sits in the job queue until you resume it with fg, only then it terminates:

pi@raspberrypi:~ $ sleep 5
^Z                                     # stop the job
[1]+  Stopped                 sleep 5
pi@raspberrypi:~ $ jobs
[1]+  Stopped                 sleep 5
pi@raspberrypi:~ $ sleep 5             # 5 more seconds have elapsed,
pi@raspberrypi:~ $ jobs                # but your job is still there
[1]+  Stopped                 sleep 5
pi@raspberrypi:~ $ fg                  # job successfully resumed
sleep 5
pi@raspberrypi:~ $

Of course, the system time doesn't stop because a single job is stopped, so the delay expires if the job is stopped for long enough.

If you actually want to stop the countdown while the job is stopped, you can make a loop in which you sleep in short increments. In this case, at most one sleep increment would expire while the job is stopped.

-2

Short Answer:

In general (in linux world), the Ctrl+Z command suspends the process (just puts it into background, doesn't terminate) and returns the promt to user, while Ctrl+C terminates/kills the process and returns the prompt to user.

In your case, the process got suspended, but it was still running in the background. Once its wait time interval exausted, it got completed and exited.

If you wanted to end it, you have to do Ctrl+C - it will terminate the sleep process immediately.

Detailed Answer:

In the background Ctrl+C sends SIGINT (Singal Interrupt) to the application to abort it immediately. Though its upto the application to decide whether to abort it or continue the process execution. Ctrl+C - Forcefull termination (request to application)

Similarly Ctrl+Z sends SIGSUSP (Signal Suspend) to the application to keep executing it into background and return the prompt. This is useful in case we want to switch from one application to another and let first process continue executing into background for some time. Ctrl+Z - Move the process into background (process will exit only on completion)

2
  • Hello, this is a fairly good answer, but I don't think it runs in the background. Trying a loop with time and output to a file should show if it's background running or suspended with Ctrl-Z. Typing bg to background or fg to foreground will allow it to keep running. Commented Jul 21, 2021 at 11:22
  • 1
    "suspended, but it was still running". That's not possible. A process can't be both suspended and running at the same time, those are two different process states.
    – Barmar
    Commented Jul 21, 2021 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .