-1

I have a server I lease from Digital Ocean. I access it using Putty. I want to run my python script in the background so that I can still do other things on the machine. What is a terminal command I can use to have it run in the background so that I can still use the machine?

Random information you might need:

-Using Ubuntu 14.04

-Python3.4 script

-My favorite dessert is cheesecake (buy me cheesecake)

Thanks for the help!

MoreFoam
  • 624
  • 1
  • 6
  • 25
  • [How do I run a Python script in the background](http://askubuntu.com/questions/175751/how-do-i-run-a-python-script-in-the-background-and-restart-it-after-a-crash) – lonewaft Jul 21 '16 at 19:18
  • Possible duplicate of [How to run process as background and never die?](http://stackoverflow.com/questions/4797050/how-to-run-process-as-background-and-never-die) – smac89 Jul 21 '16 at 19:20
  • Depending on the use case, you might consider using `nohup [script] &` or `tmux`/`screen` – Eli Korvigo Jul 21 '16 at 19:20
  • Hey, thanks for the quick answers. I'll check out those links and see if they help. – MoreFoam Jul 21 '16 at 19:23
  • Just use & after your call – Simon Jul 21 '16 at 19:25
  • 1
    He didn't ask to run it forever - just has one console and wants to be able to do other things, too. "&" is the easiest answer. – Fhaab Jul 21 '16 at 19:25
  • @Simon creating a subshell with `&` won't work, if the OP wants the script to keep on running even when the parent shell gets closed (though this is not specified). One should combine `nohup` and `&` to create a daemon processes. – Eli Korvigo Jul 21 '16 at 19:27
  • What's to stop you running another putty window? – cdarke Jul 21 '16 at 19:30
  • I think opening a 2nd Putty window works. Not sure why I didn't try that sooner. Would still be nice to know how to run things in the background, though. Just for the sake of knowing. – MoreFoam Jul 21 '16 at 19:37
  • Other options would be `screen` or `tmux`... – twalberg Jul 21 '16 at 19:38

1 Answers1

1

It's more of a unix/linux question than python, but you can do it in a couple of different ways:

% python myprogram.py &

The "&" says to run it in the background.

If you forgot, and just ran it, then you can type "^z" (control-Z) to suspend your program, then type:

% bg

To start it running again in the background. And, just for fun, "fg" would put it back in the foreground. You can have many processes running in the background - if you're doing more than one, you can use "%n" to explicitly say which one (i.e. "bg" and "bg %1" would both work, if you only had one background job), and for more fun, if you have multiple jobs running in the background:

% jobs

Will list them for you.

Fhaab
  • 351
  • 1
  • 2
  • 8
  • This answer was nice, because it helped expand my baby Linux knowledge, but I still can't use my terminal after running program with these commands. :( – MoreFoam Jul 21 '16 at 19:34
  • 1
    Well, I use PuTTY from Windows to connect to my linux servers, and unless your host (Digital Ocean) is somehow prohibiting it, that's how it's done. Control-Z might not work well through PuTTY (never tried it), but starting something with the "&" at the end of the line certainly should. – Fhaab Jul 21 '16 at 19:37
  • I'll try it again. Thanks for the help. :> – MoreFoam Jul 21 '16 at 19:38