According to this question and its accepted answer, following script should suspend and resume the btrfs send process:


get_ms () {
    echo $(($(date +%s%N)/1000000))


echo "starting btrfs send to file"
btrfs send -p rootfs/rootfs.20170429T2001 rootfs/rootfs.20170430T0140 -f test.snapshot &
echo "PID is: $PID"

mkdir -p /sys/fs/cgroup/freezer/0
echo $PID > /sys/fs/cgroup/freezer/0/tasks

pause () {
    echo "pausing process $PID"
    echo FROZEN > /sys/fs/cgroup/freezer/0/freezer.state

resume () {
    echo "resuming process $PID"
    echo THAWED > /sys/fs/cgroup/freezer/0/freezer.state

is_alive () {
    if [[ "$(ps -o pid= -p $PID)" == "$PID" ]]; then 
        return 0
        return 1

on_exit () {
    kill -9 $PID

trap on_exit INT

while is_alive; do 
    echo "$PID is still alive..."
    sleep 0.1
    read -p "Press [Enter] key to continue..."

echo "Took $(($(get_ms) - $START_TIME)) ms..."

However, if we commend out pause to resume lines in the loop, this process takes 1.5 seconds and generates a 244MB file output.

If we use the script as is, the output file size stops at a reasonable size, when we hit Enter button, it grows and stops at a higher reasonable size; but it won't stop growing (I just killed the script after +380MB)

How can I stop (freeze) the process and then continue?

  • Are you sure mount|grep freezer shows cgroup on /sys/fs/cgroup/freezer .... – meuh May 1 '17 at 17:14
  • yes, it shows: cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer) – ceremcem May 1 '17 at 19:34

In general (I don't know about btrfs or how it works under the hood with a process that does extreme amounts of disk IO) one may temporarily stop a process whose PID is $pid with

kill -TSTP "$pid"

The process may then be resumed with

kill -CONT "$pid"
  • There lots of questions about suspending and resuming a linux process and this answer is given on those questions, right. I did try to use these signals en the first hand but result is same at minimum, application (btrfs) continued writing data to the output file after killing(???) at most... (then I killed btrfs with ps -ef | grep btrfs then kill -9 xxx) – ceremcem May 1 '17 at 19:01
  • @ceremcem If the kernel has started to write on the behalf of the btrfs process, that writing will not stop when you stop the process (that would seriously lock the kernel and disk up). On a side note: "killing" with kill just means sending a signal. It will not necessarily terminate the target process, depending on what signal is sent and what the process does when it receives it. – Kusalananda May 1 '17 at 19:11
  • my last resort is setting up a virtual machine, running btrfs (or any program I need to suspend) in that VM, and suspend the whole VM. That would be the silver bullet. – ceremcem May 1 '17 at 19:38
  • @ceremcem You could try that, but it will be a similar problem between the VMM running on the host and the host disk (and kernel). – Kusalananda May 1 '17 at 19:41

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.