4

I run python scripts on my macbook air that process data from external API's and often take several hours or occasionally even days.

However, sometimes I need to suspend my laptop in the middle of running a script so I can go to work or go home or similar.

How can I simply pause/resume these scripts in the middle of their for loops?

Is there something very simple that I can add at the script level that just listens for a particular key stroke to stop/start? Or something I can do at the *nix process management level?

I'm well aware of Pickle but I'd rather not deal with the hassle of serializing/unserializing my data--since all I'm doing is hibernating the mac, I'm hoping if the script gets paused and then I hibernate, that OS X will handle saving the RAM to disk and then restoring back to RAM when I reopen the computer. At that point, I can hit a simple keystroke to continue the python script.

Since I'm switching between different wifi networks, not sure if the different IPs will cause problems when my script tries to access the internet to reach the 3rd party APIs.

Jeff Widman
  • 22,014
  • 12
  • 72
  • 88
  • CTRL+z will suspend the process until you decide to resume it again with `fg`. You could also simply close the lid (sleep your macbook), which will suspend the process for you (this is a little risky though, depending on the API you use) – inspectorG4dget Oct 26 '13 at 01:04
  • CTRL+z and `fg` sounds like what i was looking for--will they work even when my computer gets new IP addresses? not sure if Python restarts the network connections... External APIs are not a problem--each call only takes a max of 2-3 seconds for them to return a response and close the connection on their end. – Jeff Widman Oct 26 '13 at 01:17
  • The IP issues shouldn't cause any issues with PYTHON. The APIs might take issue with IP changes, but you say that shouldn't happen. So looks like this is what you want. I'm going to add this as an answer, as it seems to be what you are looking for – inspectorG4dget Oct 26 '13 at 01:39

1 Answers1

12

This was originally a comment, but it seems to be what OP wants, so I'm reposting it as an answer

I would use ctrl+z to suspend your live, running process. This will leave you with a PID, which you can later resume with a call to fg: fg <job-number>.
This shouldn't have any implications with changed network settings (like IP, etc), at least as far as python is concerned. I can't speak to whether the API will freak out, though

James
  • 325
  • 1
  • 11
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • One question - is `` accurate or should that be job number? [Other](http://www.thegeekstuff.com/2010/05/unix-background-job/) [resources](http://commandlinemac.blogspot.com/2008/12/bash-job-control-fg-bg-jobs-and-ctrl-z.html) refer to job number for this case. – James Oct 05 '15 at 20:11