If I suspend a running job in bash using ctrl-z is there a way to copy it. So that I now have two processes that are the same.

I am running a python script that loads several files and does some analysis on them, it then reaches an interactive stage that I want to run some tests on the analysed data. The idea is that the initial analysis that takes 20min or so can be done once and then the interactive stage can be tested from scratch several times.

  • It might help if you could explain why you want to copy the process instead of just re-invoking the command, but I don't think this is possible (or desirable). Feb 19 '15 at 11:31
  • You probably want to copy whole memory used by the process and paste it to another, empty(?) memory space. I'm not sure if this is possible, but sounds like rather not trivial task.
    – jimmij
    Feb 19 '15 at 13:16
  • No this is not doable directly with bash. Feb 19 '15 at 16:26
  • 2
    A better way of doing what you want (though what you want is probably possible) is to dump state at the end of the non-interactive part, and then load into the interactive part. One good way to do this is to use a relational database. A more more quick and dirty way to do this is serialization (e.g. pickling). However, this approach has nothing to do with nix, it's a straight programming issue. Feb 19 '15 at 23:11
  • ... or have your process fork() and then the child handle the interactive process. Repeat as many times as needed. But that's also something you'd program into it.
    – derobert
    Feb 19 '15 at 23:20

There's a tool called cryopid that lets you snapshot a running process so you can resume it later. I haven't tried, but I see no reason why you couldn't "resume" the snapshotted process while the original is still running. As terdon's comment implied, there's a lot of things to consider when snapshotting a process, so cryopid can be finicky; the best solution in your case would be to modify the original process so it stores the analysis once complete and loads it on future runs.

  • What is the status of the code? The current release seems to work fine now on Debian Lenny and CentOS 5.4. Newer Linux kernels have changed the way that processes are organized. Unfortunately, earlier developers of CryoPID have not had the non-trivial amounts of time for tracking the internals of each new kernel version. Newer (and older) releases no longer work correctly. We are actively trying to close this gap.
    – mikeserv
    Feb 20 '15 at 6:55

You should be able to grab it with the jobs-command:

➜  ~  top
[1]  + 55990 suspended  top
➜  ~  jobs -l
[1]  + 55990 suspended  top

I think probably you're looking for how to list the files in the /proc/$pid/ directory of the process in which you are interested (at least, this would be the case on a linux machine). Within that directory you can find practically all of the information you need to duplicate/emulate the state of one process within another.

You'll want to pay close attention to process groups, mount propagation flags, numa affinity, dirty pages, hugepages, shared-mem, memmaps, fd offsets, everything in cgroups, status, ns/*, fd/*, oom*, io, cmdline, environ, maps, and... well, just pay close attention.

Anyway, provided the correct permissions, you can list all of these files like...

ls -R /proc/"${pid_of_interest}"/

If you build your 3.3+ kernel with the CONFIG_CHECKPOINT_RESTORE option, the kernel will also populate a /proc/$pid/mem_files/ directory for you, which will contain a symbolic link (ala /proc/$pid/fd/*) for every mmap()'d file your process currently claims - and also any which it shares with any other process.

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.